Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.47% covered (success)
97.47%
77 / 79
90.91% covered (success)
90.91%
10 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Department
97.47% covered (success)
97.47%
77 / 79
90.91% covered (success)
90.91%
10 / 11
42
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
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
9
 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()
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($scopeId)
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            $scopeList = clone $this->scopes;
84            $department->scopes = new Collection\ScopeList();
85            $removeScopeList = new Collection\ScopeList();
86            if ($department->toProperty()->clusters->get()) {
87                foreach ($department->clusters as $cluster) {
88                    $cluster = new Cluster($cluster);
89                    foreach ($cluster['scopes'] as $key => $clusterScope) {
90                        $scope = $scopeList->getEntity($clusterScope['id']);
91                        if ($scope) {
92                            $scope = new Scope($scope);
93                            $cluster['scopes'][$key] = clone $scope;
94                            $removeScopeList[] = $scope;
95                        }
96                    }
97                }
98                foreach ($scopeList as $scope) {
99                    if (! $removeScopeList->hasEntity($scope['id'])) {
100                        $scope = new Scope($scope);
101                        $department->scopes->addEntity($scope);
102                    }
103                }
104            }
105        }
106        return $department;
107    }
108
109    public function withCompleteScopeList(): static
110    {
111        $department = clone $this;
112        $department->scopes = $this->getScopeList()->withUniqueScopes();
113        return $department;
114    }
115
116    public function getScopeList(): Collection\ScopeList
117    {
118        $scopeList = new Collection\ScopeList();
119        if ($this->toProperty()->clusters->isAvailable()) {
120            foreach ($this->clusters as $cluster) {
121                if (Property::__keyExists('scopes', $cluster)) {
122                    foreach ($cluster['scopes'] as $clusterScope) {
123                        $scope = new Scope($clusterScope);
124                        $scopeList->addEntity($scope);
125                    }
126                }
127            }
128        }
129        if ($this->toProperty()->scopes->isAvailable()) {
130            foreach ($this->scopes as $scope) {
131                if (! $scopeList->hasEntity($scope['id'])) {
132                    $scope = new Scope($scope);
133                    $scopeList->addEntity($scope);
134                }
135            }
136        }
137        return $scopeList;
138    }
139
140    /**
141     * @return bool
142     */
143    #[\Override]
144    public function hasAccess(Useraccount $useraccount)
145    {
146        return $useraccount->isSuperUser() || $useraccount->hasDepartment($this->id);
147    }
148
149    /**
150     * Reduce data of dereferenced entities to a required minimum
151     *
152     * @return static
153     */
154    #[\Override]
155    public function withLessData()
156    {
157        $entity = clone $this;
158        if (isset($entity['preferences'])) {
159            unset($entity['preferences']);
160            $entity['preferences'] = [];
161        }
162        if (isset($entity['email'])) {
163            unset($entity['email']);
164        }
165        if (isset($entity['scopes'])) {
166            unset($entity['scopes']);
167        }
168        if (isset($entity['clusters'])) {
169            unset($entity['clusters']);
170        }
171        if (isset($entity['links'])) {
172            unset($entity['links']);
173        }
174        if (isset($entity['dayoff'])) {
175            unset($entity['dayoff']);
176        }
177        if (isset($entity['contact'])) {
178            unset($entity['contact']);
179        }
180        return $entity;
181    }
182}