Lines 100.00% 9 / 9
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
EntitySelector 100.00% 9 / 9 100.00% 2 / 2 7 100.00% 1 / 1
 selectEntities 100.00% 8 / 8 100.00% 1 / 1 4
 isHiddenAway 100.00% 1 / 1 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Codegen\Entity;
6
7use Stewart\Client\Registry\EntityRegistryEntry;
8use Stewart\Codegen\Snapshot\Snapshot;
9use Stewart\Contracts\Entity\Collection\EntityIdCollection;
10use Stewart\Contracts\State\Collection\EntityStateCollection;
11
12final readonly class EntitySelector
13{
14    public function selectEntities(Snapshot $snapshot, EntityInclusionRules $inclusionRules): EntitySelection
15    {
16        $generated = [];
17        $ignored = [];
18
19        foreach ($snapshot->states as $state) {
20            if ($this->isHiddenAway($snapshot->registry->find($state->entityId)) || !$inclusionRules->allows($state->entityId)) {
21                $ignored[] = $state->entityId;
22
23                continue;
24            }
25
26            $generated[] = $state;
27        }
28
29        return new EntitySelection(EntityStateCollection::keyedByEntityId($generated), EntityIdCollection::fromList($ignored));
30    }
31
32    private function isHiddenAway(?EntityRegistryEntry $entry): bool
33    {
34        return $entry !== null && ($entry->isDisabled() || $entry->isHidden());
35    }
36}