Lines
100.00%
31 / 31
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| OrganisationList | 100.00% | 31 / 31 | 100.00% | 4 / 4 | 15 | 100.00% | 1 / 1 | |||
| getByDepartmentId | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| withAccess | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | |||||
| sortByName | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| withMatchingDepartments | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 5 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsentities\Collection; | |
| 4 | ||
| 5 | /** | |
| 6 | * @extends Base<\BO\Zmsentities\Organisation> | |
| 7 | */ | |
| 8 | class OrganisationList extends Base | |
| 9 | { | |
| 10 | public const string ENTITY_CLASS = '\BO\Zmsentities\Organisation'; | |
| 11 | ||
| 12 | public function getByDepartmentId(mixed $departmentId): self | |
| 13 | { | |
| 14 | $organisationList = new self(); | |
| 15 | foreach ($this as $entity) { | |
| 16 | $organisation = new \BO\Zmsentities\Organisation($entity); | |
| 17 | if ($organisation->hasDepartment($departmentId)) { | |
| 18 | $organisationList->addEntity($organisation); | |
| 19 | } | |
| 20 | } | |
| 21 | return $organisationList; | |
| 22 | } | |
| 23 | ||
| 24 | public function withAccess(\BO\Zmsentities\Useraccount $useraccount): static | |
| 25 | { | |
| 26 | $list = new static(); | |
| 27 | foreach ($this as $organisation) { | |
| 28 | $organisation = clone $organisation; | |
| 29 | if ($useraccount->hasPermissions(['department'])) { | |
| 30 | $organisation->departments = $organisation->getDepartmentList(); | |
| 31 | } else { | |
| 32 | $organisation->departments = $organisation->getDepartmentList()->withAccess($useraccount); | |
| 33 | } | |
| 34 | if ($organisation->hasAccess($useraccount)) { | |
| 35 | $list[] = $organisation; | |
| 36 | } | |
| 37 | } | |
| 38 | return $list; | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * @return static | |
| 43 | */ | |
| 44 | #[\Override] | |
| 45 | public function sortByName() | |
| 46 | { | |
| 47 | parent::sortByName(); | |
| 48 | foreach ($this as $organisation) { | |
| 49 | if ($organisation->departments instanceof DepartmentList) { | |
| 50 | $organisation->departments->sortByName(); | |
| 51 | } | |
| 52 | } | |
| 53 | return $this; | |
| 54 | } | |
| 55 | ||
| 56 | public function withMatchingDepartments(DepartmentList $departmentList): static | |
| 57 | { | |
| 58 | $list = new static(); | |
| 59 | foreach ($this as $organisation) { | |
| 60 | $entity = clone $organisation; | |
| 61 | $entity->departments = new DepartmentList(); | |
| 62 | $departmentMatchList = $organisation->getDepartmentList(); | |
| 63 | foreach ($departmentList as $department) { | |
| 64 | if ($departmentMatchList->hasEntity($department->id)) { | |
| 65 | $entity->departments->addEntity($department); | |
| 66 | } | |
| 67 | } | |
| 68 | if ($entity->departments->count()) { | |
| 69 | $list->addEntity($entity); | |
| 70 | } | |
| 71 | } | |
| 72 | return $list; | |
| 73 | } | |
| 74 | } |