Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.34% covered (success)
96.34%
79 / 82
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Department
96.34% covered (success)
96.34%
79 / 82
81.82% covered (warning)
81.82%
9 / 11
43
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 hasMail
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getContactPerson
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContact
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDayoffList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 getClusterByScopeId
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 withOutClusterDuplicates
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
10
 withCompleteScopeList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getScopeList
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
8
 hasAccess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 withLessData
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
8.10
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Zmsentities\Helper\Property;
6
7class Department extends Schema\Entity implements Useraccount\AccessInterface
8{
9    public const string PRIMARY = 'id';
10
11    public static $schema = "department.json";
12
13    /**
14     * @return (Collection\ClusterList|Collection\DayoffList|Collection\LinkList|Collection\ScopeList|int|string)[]
15     *
16     */
17    #[\Override]
18    public function getDefaults()
19    {
20        return [
21            'id' => 0,
22            'scopes' => new Collection\ScopeList(),
23            'clusters' => new Collection\ClusterList(),
24            'links' => new Collection\LinkList(),
25            'dayoff' => new Collection\DayoffList(),
26            'name' => '',
27        ];
28    }
29
30    public function hasMail(): bool
31    {
32        return ($this->toProperty()->email->isAvailable() && $this->toProperty()->email->get());
33    }
34
35    public function getContactPerson(): mixed
36    {
37        return $this->toProperty()->contact->name->get();
38    }
39
40    public function getContact(): Contact
41    {
42        return new Contact($this->toProperty()->contact->get());
43    }
44
45    public function getDayoffList(): Collection\DayoffList
46    {
47        if (!$this->dayoff instanceof Collection\DayoffList) {
48            $this->dayoff = new Collection\DayoffList((array)$this->dayoff);
49            foreach ($this->dayoff as $key => $dayOff) {
50                if (!$dayOff instanceof Dayoff) {
51                    $this->dayoff[$key] = new Dayoff($dayOff);
52                }
53            }
54        }
55        return $this->dayoff;
56    }
57
58    public function getClusterByScopeId(mixed $scopeId): mixed
59    {
60        $selectedCluster = false;
61        if (isset($this->clusters)) {
62            foreach ($this->clusters as $cluster) {
63                $cluster = new Cluster($cluster);
64                foreach ($cluster['scopes'] as $clusterScope) {
65                    if ($scopeId == $clusterScope['id']) {
66                        $selectedCluster = $cluster->getId();
67                        break;
68                    }
69                }
70            }
71        }
72        return $selectedCluster;
73    }
74
75    /**
76     * Remove duplicate scopes from clusters
77     * Move scopes to clusters to keep the same resolveReference Level
78     */
79    public function withOutClusterDuplicates(): static
80    {
81        $department = clone $this;
82        if ($this->offsetExists('scopes') && $this->scopes) {
83            $scopes = $this->scopes;
84            $scopeList = $scopes instanceof Collection\ScopeList
85                ? clone $scopes
86                : new Collection\ScopeList($scopes);
87            $department->scopes = new Collection\ScopeList();
88            $removeScopeList = new Collection\ScopeList();
89            if ($department->toProperty()->clusters->get()) {
90                foreach ($department->clusters as $cluster) {
91                    $cluster = new Cluster($cluster);
92                    foreach ($cluster['scopes'] as $key => $clusterScope) {
93                        $scope = $scopeList->getEntity($clusterScope['id']);
94                        if ($scope) {
95                            $scope = new Scope($scope);
96                            $cluster['scopes'][$key] = clone $scope;
97                            $removeScopeList[] = $scope;
98                        }
99                    }
100                }
101                foreach ($scopeList as $scope) {
102                    if (! $removeScopeList->hasEntity($scope['id'])) {
103                        $scope = new Scope($scope);
104                        $department->scopes->addEntity($scope);
105                    }
106                }
107            }
108        }
109        return $department;
110    }
111
112    public function withCompleteScopeList(): static
113    {
114        $department = clone $this;
115        $department->scopes = $this->getScopeList()->withUniqueScopes();
116        return $department;
117    }
118
119    public function getScopeList(): Collection\ScopeList
120    {
121        $scopeList = new Collection\ScopeList();
122        if ($this->toProperty()->clusters->isAvailable()) {
123            foreach ($this->clusters as $cluster) {
124                if (Property::__keyExists('scopes', $cluster)) {
125                    foreach ($cluster['scopes'] as $clusterScope) {
126                        $scope = new Scope($clusterScope);
127                        $scopeList->addEntity($scope);
128                    }
129                }
130            }
131        }
132        if ($this->toProperty()->scopes->isAvailable()) {
133            foreach ($this->scopes as $scope) {
134                if (! $scopeList->hasEntity($scope['id'])) {
135                    $scope = new Scope($scope);
136                    $scopeList->addEntity($scope);
137                }
138            }
139        }
140        return $scopeList;
141    }
142
143    /**
144     * @return bool
145     */
146    #[\Override]
147    public function hasAccess(Useraccount $useraccount): bool
148    {
149        return $useraccount->isSuperUser() || $useraccount->hasDepartment($this->id);
150    }
151
152    /**
153     * Reduce data of dereferenced entities to a required minimum
154     *
155     * @return static
156     */
157    #[\Override]
158    public function withLessData()
159    {
160        $entity = clone $this;
161        if (isset($entity['preferences'])) {
162            unset($entity['preferences']);
163            $entity['preferences'] = [];
164        }
165        if (isset($entity['email'])) {
166            unset($entity['email']);
167        }
168        if (isset($entity['scopes'])) {
169            unset($entity['scopes']);
170        }
171        if (isset($entity['clusters'])) {
172            unset($entity['clusters']);
173        }
174        if (isset($entity['links'])) {
175            unset($entity['links']);
176        }
177        if (isset($entity['dayoff'])) {
178            unset($entity['dayoff']);
179        }
180        if (isset($entity['contact'])) {
181            unset($entity['contact']);
182        }
183        return $entity;
184    }
185}