Collection::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\Doorkeeper\Identification;
6
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
52
    {
53
        return $this->identifications[$id] ?? null;
54
    }
55
56
    public function has(IdentificationInterface $identification): bool
57
    {
58
        return isset($this->identifications[$identification->getIdentifier()]);
59
    }
60
}
61