Total Complexity | 10 |
Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | final class Collection implements \Countable, \IteratorAggregate |
||
8 | { |
||
9 | private string $classType; |
||
10 | |||
11 | /** |
||
12 | * @var \RemotelyLiving\Doorkeeper\Identification\IdentificationInterface[] |
||
13 | */ |
||
14 | private $identifications = []; |
||
15 | |||
16 | public function __construct(string $classType, array $identifications = []) |
||
17 | { |
||
18 | $this->classType = $classType; |
||
19 | |||
20 | foreach ($identifications as $identification) { |
||
21 | $this->add($identification); |
||
22 | } |
||
23 | } |
||
24 | |||
25 | public function getIterator(): \ArrayIterator |
||
26 | { |
||
27 | return new \ArrayIterator($this->identifications); |
||
28 | } |
||
29 | |||
30 | public function count(): int |
||
31 | { |
||
32 | return count($this->identifications); |
||
33 | } |
||
34 | |||
35 | public function add(IdentificationInterface $identification): void |
||
36 | { |
||
37 | if (get_class($identification) !== $this->classType) { |
||
38 | throw new \InvalidArgumentException("Identification must be a {$this->classType}"); |
||
39 | } |
||
40 | |||
41 | $this->identifications[$identification->getIdentifier()] = $identification; |
||
42 | } |
||
43 | |||
44 | public function remove(IdentificationInterface $identification): void |
||
45 | { |
||
46 | if ($this->has($identification)) { |
||
47 | unset($this->identifications[$identification->getIdentifier()]); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | public function get(string $id): ?IdentificationInterface |
||
54 | } |
||
55 | |||
56 | public function has(IdentificationInterface $identification): bool |
||
57 | { |
||
58 | return isset($this->identifications[$identification->getIdentifier()]); |
||
59 | } |
||
60 | } |
||
61 |