Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SiblingPolicyEffectiveBoolReader
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEffectiveBool
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
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\Helper;
10
11use OCA\Libresign\Service\Policy\Model\PolicyContext;
12use OCA\Libresign\Service\Policy\Runtime\DefaultPolicyResolver;
13use OCA\Libresign\Service\Policy\Runtime\PolicyRegistry;
14use OCA\Libresign\Service\Policy\Runtime\PolicySource;
15
16/**
17 * Reads a sibling policy's effective boolean in the same PolicyContext used to
18 * build the current resolved state, without recursively nesting meta resolution
19 * when two policies inspect each other.
20 */
21class SiblingPolicyEffectiveBoolReader {
22    private int $depth = 0;
23    private DefaultPolicyResolver $resolver;
24
25    public function __construct(
26        private PolicyRegistry $registry,
27        PolicySource $source,
28    ) {
29        $this->resolver = new DefaultPolicyResolver($source);
30    }
31
32    public function getEffectiveBool(string $policyKey, PolicyContext $context): bool {
33        if ($this->depth > 0) {
34            return false;
35        }
36
37        $this->depth++;
38        try {
39            return $this->resolver
40                ->resolve($this->registry->get($policyKey), $context)
41                ->getEffectiveValueAsBool();
42        } finally {
43            $this->depth--;
44        }
45    }
46}