Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ConfigService | |
96.67% |
29 / 30 |
|
85.71% |
6 / 7 |
13 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isEnabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setEnabled | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| getLevel | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
| setLevel | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getConfig | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getAvailableLevels | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 7 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | */ |
| 9 | |
| 10 | namespace OCA\Libresign\Service\DocMdp; |
| 11 | |
| 12 | use OCA\Libresign\Enum\DocMdpLevel; |
| 13 | use OCA\Libresign\Service\Policy\PolicyService; |
| 14 | use OCA\Libresign\Service\Policy\Provider\DocMdp\DocMdpPolicy; |
| 15 | use OCP\IL10N; |
| 16 | |
| 17 | /** |
| 18 | * @psalm-import-type LibresignDocMdpConfig from \OCA\Libresign\ResponseDefinitions |
| 19 | * @psalm-import-type LibresignDocMdpLevelOption from \OCA\Libresign\ResponseDefinitions |
| 20 | */ |
| 21 | class ConfigService { |
| 22 | private const DEFAULT_LEVEL = DocMdpLevel::NOT_CERTIFIED; |
| 23 | |
| 24 | public function __construct( |
| 25 | private PolicyService $policyService, |
| 26 | private IL10N $l10n, |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | public function isEnabled(): bool { |
| 31 | return $this->getLevel()->isCertifying(); |
| 32 | } |
| 33 | |
| 34 | public function setEnabled(bool $enabled): void { |
| 35 | if ($enabled) { |
| 36 | if (!$this->getLevel()->isCertifying()) { |
| 37 | $this->setLevel(DocMdpLevel::CERTIFIED_FORM_FILLING); |
| 38 | } |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | $this->setLevel(DocMdpLevel::NOT_CERTIFIED); |
| 43 | } |
| 44 | |
| 45 | public function getLevel(): DocMdpLevel { |
| 46 | $storedValue = $this->policyService->getSystemPolicy(DocMdpPolicy::KEY)?->getValue(); |
| 47 | |
| 48 | if ($storedValue instanceof DocMdpLevel) { |
| 49 | return $storedValue; |
| 50 | } |
| 51 | |
| 52 | if (is_string($storedValue) && preg_match('/^\d+$/', $storedValue) === 1) { |
| 53 | $storedValue = (int)$storedValue; |
| 54 | } |
| 55 | |
| 56 | if (is_int($storedValue)) { |
| 57 | return DocMdpLevel::tryFrom($storedValue) ?? self::DEFAULT_LEVEL; |
| 58 | } |
| 59 | |
| 60 | return self::DEFAULT_LEVEL; |
| 61 | } |
| 62 | |
| 63 | public function setLevel(DocMdpLevel $level): void { |
| 64 | $allowChildOverride = $this->policyService->getSystemPolicy(DocMdpPolicy::KEY)?->isAllowChildOverride() ?? false; |
| 65 | $this->policyService->saveSystem(DocMdpPolicy::KEY, $level->value, $allowChildOverride); |
| 66 | } |
| 67 | |
| 68 | /** @return LibresignDocMdpConfig */ |
| 69 | public function getConfig(): array { |
| 70 | return [ |
| 71 | 'enabled' => $this->isEnabled(), |
| 72 | 'defaultLevel' => $this->getLevel()->value, |
| 73 | 'availableLevels' => $this->getAvailableLevels(), |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | /** @return list<LibresignDocMdpLevelOption> */ |
| 78 | private function getAvailableLevels(): array { |
| 79 | return array_map( |
| 80 | fn (DocMdpLevel $level) => [ |
| 81 | 'value' => $level->value, |
| 82 | 'label' => $level->getLabel($this->l10n), |
| 83 | 'description' => $level->getDescription($this->l10n), |
| 84 | ], |
| 85 | DocMdpLevel::cases() |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | } |