Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SignerIpGeolocationPolicyValue
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 normalize
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 defaults
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\Policy\Provider\SignerIpGeolocation;
10
11use OCA\Libresign\Enum\SignerIpGeolocationMode;
12
13final class SignerIpGeolocationPolicyValue {
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 = SignerIpGeolocationMode::tryFrom((string)($rawValue['mode'] ?? ''));
30        if ($mode === null) {
31            $mode = SignerIpGeolocationMode::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' => SignerIpGeolocationMode::DISABLED->value,
45        ];
46    }
47
48    public static function getMode(mixed $rawValue): SignerIpGeolocationMode {
49        $normalized = self::normalize($rawValue);
50        return SignerIpGeolocationMode::from($normalized['mode']);
51    }
52
53    public static function isEnabled(mixed $rawValue): bool {
54        return self::getMode($rawValue) === SignerIpGeolocationMode::ENABLED;
55    }
56}