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 PRIMARY = 'id';
10
11    public static $schema = "department.json";
12
13    public function getDefaults()
14    {
15        return [
16            'id' => 0,
17            'scopes' => new Collection\ScopeList(),
18            'clusters' => new Collection\ClusterList(),
19            'links' => new Collection\LinkList(),
20            'dayoff' => new Collection\DayoffList(),
21            'name' => '',
22        ];
23    }
24
25    public function hasMail()
26    {
27        return ($this->toProperty()->email->isAvailable() && $this->toProperty()->email->get());
28    }
29
30    public function getContactPerson()
31    {
32        return $this->toProperty()->contact->name->get();
33    }
34
35    public function getContact()
36    {
37        return new Contact($this->toProperty()->contact->get());
38    }
39
40    public function getDayoffList()
41    {
42        if (!$this->dayoff instanceof Collection\DayoffList) {
43            $this->dayoff = new Collection\DayoffList((array)$this->dayoff);
44            foreach ($this->dayoff as $key => $dayOff) {
45                if (!$dayOff instanceof Dayoff) {
46                    $this->dayoff[$key] = new Dayoff($dayOff);
47                }
48            }
49        }
50        return $this->dayoff;
51    }
52
53    public function getClusterByScopeId($scopeId)
54    {
55        $selectedCluster = false;
56        if (isset($this->clusters)) {
57            foreach ($this->clusters as $cluster) {
58                $cluster = new Cluster($cluster);
59                foreach ($cluster['scopes'] as $clusterScope) {
60                    if ($scopeId == $clusterScope['id']) {
61                        $selectedCluster = $cluster->getId();
62                        break;
63                    }
64                }
65            }
66        }
67        return $selectedCluster;
68    }
69
70    /**
71     * Remove duplicate scopes from clusters
72     * Move scopes to clusters to keep the same resolveReference Level
73     */
74    public function withOutClusterDuplicates()
75    {
76        $department = clone $this;
77        if ($this->offsetExists('scopes') && $this->scopes) {
78            $scopeList = clone $this->scopes;
79            $department->scopes = new Collection\ScopeList();
80            $removeScopeList = new Collection\ScopeList();
81            if ($department->toProperty()->clusters->get()) {
82                foreach ($department->clusters as $cluster) {
83                    $cluster = new Cluster($cluster);
84                    foreach ($cluster['scopes'] as $key => $clusterScope) {
85                        $scope = $scopeList->getEntity($clusterScope['id']);
86                        if ($scope) {
87                            $scope = new Scope($scope);
88                            $cluster['scopes'][$key] = clone $scope;
89                            $removeScopeList[] = $scope;
90                        }
91                    }
92                }
93                foreach ($scopeList as $scope) {
94                    if (! $removeScopeList->hasEntity($scope['id'])) {
95                        $scope = new Scope($scope);
96                        $department->scopes->addEntity($scope);
97                    }
98                }
99            }
100        }
101        return $department;
102    }
103
104    public function withCompleteScopeList()
105    {
106        $department = clone $this;
107        $department->scopes = $this->getScopeList()->withUniqueScopes();
108        return $department;
109    }
110
111    public function getScopeList()
112    {
113        $scopeList = new Collection\ScopeList();
114        if ($this->toProperty()->clusters->isAvailable()) {
115            foreach ($this->clusters as $cluster) {
116                if (Property::__keyExists('scopes', $cluster)) {
117                    foreach ($cluster['scopes'] as $clusterScope) {
118                        $scope = new Scope($clusterScope);
119                        $scopeList->addEntity($scope);
120                    }
121                }
122            }
123        }
124        if ($this->toProperty()->scopes->isAvailable()) {
125            foreach ($this->scopes as $scope) {
126                if (! $scopeList->hasEntity($scope['id'])) {
127                    $scope = new Scope($scope);
128                    $scopeList->addEntity($scope);
129                }
130            }
131        }
132        return $scopeList;
133    }
134
135    public function hasAccess(Useraccount $useraccount)
136    {
137        return $useraccount->hasRights(['superuser']) || $useraccount->hasDepartment($this->id);
138    }
139
140    /**
141     * Reduce data of dereferenced entities to a required minimum
142     *
143     */
144    public function withLessData()
145    {
146        $entity = clone $this;
147        if (isset($entity['preferences'])) {
148            unset($entity['preferences']);
149            $entity['preferences'] = [];
150        }
151        if (isset($entity['email'])) {
152            unset($entity['email']);
153        }
154        if (isset($entity['scopes'])) {
155            unset($entity['scopes']);
156        }
157        if (isset($entity['clusters'])) {
158            unset($entity['clusters']);
159        }
160        if (isset($entity['links'])) {
161            unset($entity['links']);
162        }
163        if (isset($entity['dayoff'])) {
164            unset($entity['dayoff']);
165        }
166        if (isset($entity['contact'])) {
167            unset($entity['contact']);
168        }
169        return $entity;
170    }
171}