| Total Complexity | 13 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | class IdentityCollection extends ArrayCollection |
||
| 13 | { |
||
| 14 | public function toArray(): array |
||
| 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 |
||
| 69 | } |
||
| 70 | } |
||
| 71 |