Lines 100.00% 7 / 7
Methods 100.00% 5 / 5
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromPatterns 100.00% 1 / 1 100.00% 1 / 1 1
 allowingEverything 100.00% 1 / 1 100.00% 1 / 1 1
 allows 100.00% 3 / 3 100.00% 1 / 1 3
 listUnmatchedIncludes 100.00% 1 / 1 100.00% 1 / 1 1
13final readonly class EntityInclusionRules
14{
15    private function __construct(
16        public SelectorCollection $include,
17        public SelectorCollection $exclude,
18    ) {}
19
20    /**
21     * @param list<string> $include
22     * @param list<string> $exclude
23     * @throws SelectorException
24     */
25    public static function fromPatterns(array $include = [], array $exclude = []): self
26    {
27        return new self(SelectorCollection::fromSpecs(...$include), SelectorCollection::fromSpecs(...$exclude));
28    }
29
30    public static function allowingEverything(): self
31    {
32        return new self(SelectorCollection::empty(), SelectorCollection::empty());
33    }
34
35    public function allows(EntityId $entityId): bool
36    {
37        if ($this->exclude->anyMatches($entityId->value)) {
38            return false;
39        }
40
41        return $this->include->isEmpty() || $this->include->anyMatches($entityId->value);
42    }
43
44    public function listUnmatchedIncludes(EntityStateCollection $states): SelectorCollection
45    {
46        return $this->include->filter(static fn(Selector $selector): bool => $states->filterBySelector($selector)->isEmpty());
47    }
48}