Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
OrganisationList
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
15
100.00% covered (success)
100.00%
1 / 1
 getByDepartmentId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 withAccess
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 sortByName
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 withMatchingDepartments
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace BO\Zmsentities\Collection;
4
5/**
6 * @extends Base<\BO\Zmsentities\Organisation>
7 */
8class OrganisationList extends Base
9{
10    public const ENTITY_CLASS = '\BO\Zmsentities\Organisation';
11
12    public function getByDepartmentId($departmentId)
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)
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    #[\Override]
42    public function sortByName()
43    {
44        parent::sortByName();
45        foreach ($this as $organisation) {
46            if ($organisation->departments instanceof DepartmentList) {
47                $organisation->departments->sortByName();
48            }
49        }
50        return $this;
51    }
52
53    public function withMatchingDepartments(DepartmentList $departmentList)
54    {
55        $list = new static();
56        foreach ($this as $organisation) {
57            $entity = clone $organisation;
58            $entity->departments = new DepartmentList();
59            $departmentMatchList = $organisation->getDepartmentList();
60            foreach ($departmentList as $department) {
61                if ($departmentMatchList->hasEntity($department->id)) {
62                    $entity->departments->addEntity($department);
63                }
64            }
65            if ($entity->departments->count()) {
66                $list->addEntity($entity);
67            }
68        }
69        return $list;
70    }
71}