Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
20 / 22 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| LegalInformationFilePolicyApplier | |
90.91% |
20 / 22 |
|
60.00% |
3 / 5 |
9.06 | |
0.00% |
0 / 1 |
| apply | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
| sync | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 | |||
| supportsCoreFlowSync | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOverrides | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| assertOverrideAllowed | |
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\LegalInformation\FilePolicy; |
| 10 | |
| 11 | use OCA\Libresign\Db\File as FileEntity; |
| 12 | use OCA\Libresign\Service\Policy\AbstractFilePolicyApplier; |
| 13 | use OCA\Libresign\Service\Policy\Model\ResolvedPolicy; |
| 14 | use OCA\Libresign\Service\Policy\Provider\LegalInformation\LegalInformationPolicy; |
| 15 | use OCP\IUser; |
| 16 | |
| 17 | class LegalInformationFilePolicyApplier extends AbstractFilePolicyApplier { |
| 18 | |
| 19 | #[\Override] |
| 20 | public function apply(FileEntity $file, array $data): void { |
| 21 | $user = ($data['userManager'] ?? null) instanceof IUser ? $data['userManager'] : null; |
| 22 | $requestOverrides = $this->getOverrides($data); |
| 23 | $activeContext = $this->extractActiveContext($data); |
| 24 | $resolvedPolicy = $activeContext === null |
| 25 | ? $this->policyService->resolveForUser(LegalInformationPolicy::KEY, $user, $requestOverrides) |
| 26 | : $this->policyService->resolveForUser(LegalInformationPolicy::KEY, $user, $requestOverrides, $activeContext); |
| 27 | $this->assertOverrideAllowed($requestOverrides, $resolvedPolicy); |
| 28 | $this->storePolicySnapshot($file, $resolvedPolicy); |
| 29 | } |
| 30 | |
| 31 | #[\Override] |
| 32 | public function sync(FileEntity $file, array $data): void { |
| 33 | $requestOverrides = $this->getOverrides($data); |
| 34 | $activeContext = $this->extractActiveContext($data); |
| 35 | $resolvedPolicy = $activeContext === null |
| 36 | ? $this->policyService->resolveForUserId(LegalInformationPolicy::KEY, $file->getUserId(), $requestOverrides) |
| 37 | : $this->policyService->resolveForUserId(LegalInformationPolicy::KEY, $file->getUserId(), $requestOverrides, $activeContext); |
| 38 | $this->assertOverrideAllowed($requestOverrides, $resolvedPolicy); |
| 39 | $metadataBeforeUpdate = $file->getMetadata() ?? []; |
| 40 | $this->storePolicySnapshot($file, $resolvedPolicy); |
| 41 | $metadataChanged = ($file->getMetadata() ?? []) !== $metadataBeforeUpdate; |
| 42 | |
| 43 | if ($metadataChanged) { |
| 44 | $this->fileService->update($file); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[\Override] |
| 49 | public function supportsCoreFlowSync(): bool { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** @return array<string, string> */ |
| 54 | private function getOverrides(array $data): array { |
| 55 | return $this->extractSinglePolicyOverride($data, LegalInformationPolicy::KEY); |
| 56 | } |
| 57 | |
| 58 | /** @param array<string, string> $requestOverrides */ |
| 59 | private function assertOverrideAllowed(array $requestOverrides, ResolvedPolicy $resolvedPolicy): void { |
| 60 | $this->assertRequestOverrideAllowed($requestOverrides, $resolvedPolicy, 'Legal information override is blocked by %s.'); |
| 61 | } |
| 62 | } |