Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| SignerGeolocationPolicyValue | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| normalize | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| defaults | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getMode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isEnabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Service\Policy\Provider\SignerGeolocation; |
| 10 | |
| 11 | use OCA\Libresign\Enum\SignerGeolocationMode; |
| 12 | |
| 13 | final class SignerGeolocationPolicyValue { |
| 14 | /** |
| 15 | * @return array{mode: string} |
| 16 | */ |
| 17 | public static function normalize(mixed $rawValue): array { |
| 18 | if (is_string($rawValue)) { |
| 19 | $decoded = json_decode($rawValue, true); |
| 20 | if (is_array($decoded)) { |
| 21 | $rawValue = $decoded; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | if (!is_array($rawValue)) { |
| 26 | return self::defaults(); |
| 27 | } |
| 28 | |
| 29 | $mode = SignerGeolocationMode::tryFrom((string)($rawValue['mode'] ?? '')); |
| 30 | if ($mode === null) { |
| 31 | $mode = SignerGeolocationMode::DISABLED; |
| 32 | } |
| 33 | |
| 34 | return [ |
| 35 | 'mode' => $mode->value, |
| 36 | ]; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return array{mode: string} |
| 41 | */ |
| 42 | public static function defaults(): array { |
| 43 | return [ |
| 44 | 'mode' => SignerGeolocationMode::DISABLED->value, |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | public static function getMode(mixed $rawValue): SignerGeolocationMode { |
| 49 | $normalized = self::normalize($rawValue); |
| 50 | return SignerGeolocationMode::from($normalized['mode']); |
| 51 | } |
| 52 | |
| 53 | public static function isEnabled(mixed $rawValue): bool { |
| 54 | return self::getMode($rawValue) !== SignerGeolocationMode::DISABLED; |
| 55 | } |
| 56 | } |