Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.59% covered (success)
92.59%
50 / 54
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
DepartmentList
92.59% covered (success)
92.59%
50 / 54
87.50% covered (warning)
87.50%
7 / 8
29.34
0.00% covered (danger)
0.00%
0 / 1
 getArrayCopy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withOutClusterDuplicates
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getUniqueScopeList
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 getUniqueClusterList
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 withAccess
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 withMatchingScopes
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 sortByName
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
 getIds
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BO\Zmsentities\Collection;
4
5use BO\Zmsentities\Helper\Property;
6
7/**
8 * @extends Base<\BO\Zmsentities\Department>
9 */
10class DepartmentList extends Base implements JsonUnindexed
11{
12    public const string ENTITY_CLASS = '\BO\Zmsentities\Department';
13
14    #[\Override]
15    public function getArrayCopy(): array
16    {
17        return parent::getArrayCopy();
18    }
19
20    public function withOutClusterDuplicates(): self
21    {
22        $departmentList = new self();
23        foreach ($this as $department) {
24            $entity = new \BO\Zmsentities\Department($department);
25            $departmentList->addEntity($entity->withOutClusterDuplicates());
26        }
27        return $departmentList;
28    }
29
30    public function getUniqueScopeList(): \BO\Zmsentities\Collection\ScopeList
31    {
32        $scopeList = new ScopeList();
33        $clusterList = $this->getUniqueClusterList();
34        foreach ($this as $department) {
35            $entity = new \BO\Zmsentities\Department($department);
36            foreach ($entity->scopes as $scope) {
37                $scope = new \BO\Zmsentities\Scope($scope);
38                $scopeList->addEntity($scope);
39            }
40        }
41        foreach ($clusterList as $cluster) {
42            foreach ($cluster->scopes as $scope) {
43                $scope = new \BO\Zmsentities\Scope($scope);
44                $scopeList->addEntity($scope);
45            }
46        }
47        return $scopeList->withUniqueScopes();
48    }
49
50    public function getUniqueClusterList(): \BO\Zmsentities\Collection\ClusterList
51    {
52        $clusterList = new ClusterList();
53        foreach ($this as $department) {
54            if (Property::__keyExists('clusters', $department)) {
55                foreach ($department['clusters'] as $cluster) {
56                    $entity = new \BO\Zmsentities\Cluster($cluster);
57                    $clusterList->addEntity($entity);
58                }
59            }
60        }
61        return $clusterList->withUniqueClusters();
62    }
63
64    public function withAccess(\BO\Zmsentities\Useraccount $useraccount): static
65    {
66        $list = new static();
67        foreach ($this as $department) {
68            if ($department->hasAccess($useraccount)) {
69                if ($useraccount->hasPermissions(['organisation'])) {
70                    return $this;
71                }
72                $list->addEntity($department);
73            }
74        }
75        return $list;
76    }
77
78    public function withMatchingScopes(ScopeList $scopeList): static
79    {
80        $list = new static();
81        foreach ($this as $department) {
82            $entity = clone $department;
83            $entity->scopes = new ScopeList();
84            $departmentScopeList = $department->getScopeList()->withUniqueScopes();
85            foreach ($scopeList as $scope) {
86                if ($departmentScopeList->hasEntity($scope->id)) {
87                    $entity->scopes->addEntity($scope);
88                }
89            }
90            if ($entity->scopes->count()) {
91                $list->addEntity($entity);
92            }
93        }
94        return $list;
95    }
96
97    /**
98     * @return static
99     */
100    #[\Override]
101    public function sortByName()
102    {
103        parent::sortByName();
104        foreach ($this as $department) {
105            if (isset($department->clusters) && $department->clusters instanceof ClusterList) {
106                $department->clusters->sortByName();
107            }
108            if (isset($department->scopes) && $department->scopes instanceof ScopeList) {
109                $department->scopes->sortByName();
110            }
111        }
112        return $this;
113    }
114
115    /**
116     * @return array
117     *
118     */
119    #[\Override]
120    public function getIds()
121    {
122        $ids = [];
123        foreach ($this as $department) {
124            $ids[] = $department->id;
125        }
126
127        return $ids;
128    }
129}