IdentityCollection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 10 2
A getElements() 0 3 1
A find() 0 16 4
A findByName() 0 9 3
A findById() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Blackmine\Collection;
6
7
use Blackmine\Model\Identity;
8
use Blackmine\Model\NamedIdentity;
9
use Blackmine\Tool\Inflect;
10
use Doctrine\Common\Collections\ArrayCollection;
11
12
class IdentityCollection extends ArrayCollection
13
{
14
    public function toArray(): array
15
    {
16
        $ret = [];
17
18
        $elements = parent::toArray();
19
        foreach ($elements as $identity) {
20
            $ret[] = $identity->toArray();
21
        }
22
23
        return $ret;
24
    }
25
26
    public function find(Identity $identity): ?Identity
27
    {
28
        if ($this->isEmpty()) {
29
            return null;
30
        }
31
32
        if (!$identity->isPersisted()) {
33
            return null;
34
        }
35
36
37
        if ($identity instanceof NamedIdentity) {
38
            return $this->findByName($identity->getName());
39
        }
40
41
        return $this->findById($identity->getId());
42
    }
43
44
    public function findByName(string $name): ?Identity
45
    {
46
        foreach ($this->getElements() as $identity) {
47
            if ($identity->getName() === $name) {
48
                return $identity;
49
            }
50
        }
51
52
        return null;
53
    }
54
55
    public function findById(int $id): ?Identity
56
    {
57
        foreach ($this->getElements() as $identity) {
58
            if ($identity->getId() === $id) {
59
                return $identity;
60
            }
61
        }
62
63
        return null;
64
    }
65
66
    public function getElements(): array
67
    {
68
        return parent::toArray();
69
    }
70
}
71