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
5class OrganisationList extends Base
6{
7    public const ENTITY_CLASS = '\BO\Zmsentities\Organisation';
8
9    public function getByDepartmentId($departmentId)
10    {
11        $organisationList = new self();
12        foreach ($this as $entity) {
13            $organisation = new \BO\Zmsentities\Organisation($entity);
14            if ($organisation->hasDepartment($departmentId)) {
15                $organisationList->addEntity($organisation);
16            }
17        }
18        return $organisationList;
19    }
20
21    public function withAccess(\BO\Zmsentities\Useraccount $useraccount)
22    {
23        $list = new static();
24        foreach ($this as $organisation) {
25            $organisation = clone $organisation;
26            if ($useraccount->hasRights(['department'])) {
27                $organisation->departments = $organisation->getDepartmentList();
28            } else {
29                $organisation->departments = $organisation->getDepartmentList()->withAccess($useraccount);
30            }
31            if ($organisation->hasAccess($useraccount)) {
32                $list[] = $organisation;
33            }
34        }
35        return $list;
36    }
37
38    #[\Override]
39    public function sortByName()
40    {
41        parent::sortByName();
42        foreach ($this as $organisation) {
43            if ($organisation->departments instanceof DepartmentList) {
44                $organisation->departments->sortByName();
45            }
46        }
47        return $this;
48    }
49
50    public function withMatchingDepartments(DepartmentList $departmentList)
51    {
52        $list = new static();
53        foreach ($this as $organisation) {
54            $entity = clone $organisation;
55            $entity->departments = new DepartmentList();
56            $departmentMatchList = $organisation->getDepartmentList();
57            foreach ($departmentList as $department) {
58                if ($departmentMatchList->hasEntity($department->id)) {
59                    $entity->departments->addEntity($department);
60                }
61            }
62            if ($entity->departments->count()) {
63                $list->addEntity($entity);
64            }
65        }
66        return $list;
67    }
68}