Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
88 / 88
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
Department
100.00% covered (success)
100.00%
88 / 88
100.00% covered (success)
100.00%
15 / 15
49
100.00% covered (success)
100.00%
1 / 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
 hasNotificationEnabled
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 hasNotificationReminderEnabled
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 hasNotificationConfirmationEnabled
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getNotificationPreferences
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
8
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 hasNotificationEnabled()
31    {
32        $prefs = $this->getNotificationPreferences();
33        return ($prefs['identification'] && $prefs['enabled']);
34    }
35
36    public function hasNotificationReminderEnabled()
37    {
38        $prefs = $this->getNotificationPreferences();
39        return ($this->hasNotificationEnabled() && $prefs['sendReminderEnabled']);
40    }
41
42    public function hasNotificationConfirmationEnabled()
43    {
44        $prefs = $this->getNotificationPreferences();
45        return ($this->hasNotificationEnabled() && $prefs['sendConfirmationEnabled']);
46    }
47
48    public function getNotificationPreferences()
49    {
50        return $this->toProperty()->preferences->notifications->get();
51    }
52
53    public function getContactPerson()
54    {
55        return $this->toProperty()->contact->name->get();
56    }
57
58    public function getContact()
59    {
60        return new Contact($this->toProperty()->contact->get());
61    }
62
63    public function getDayoffList()
64    {
65        if (!$this->dayoff instanceof Collection\DayoffList) {
66            $this->dayoff = new Collection\DayoffList((array)$this->dayoff);
67            foreach ($this->dayoff as $key => $dayOff) {
68                if (!$dayOff instanceof Dayoff) {
69                    $this->dayoff[$key] = new Dayoff($dayOff);
70                }
71            }
72        }
73        return $this->dayoff;
74    }
75
76    public function getClusterByScopeId($scopeId)
77    {
78        $selectedCluster = false;
79        if (isset($this->clusters)) {
80            foreach ($this->clusters as $cluster) {
81                $cluster = new Cluster($cluster);
82                foreach ($cluster['scopes'] as $clusterScope) {
83                    if ($scopeId == $clusterScope['id']) {
84                        $selectedCluster = $cluster->getId();
85                        break;
86                    }
87                }
88            }
89        }
90        return $selectedCluster;
91    }
92
93    /**
94     * Remove duplicate scopes from clusters
95     * Move scopes to clusters to keep the same resolveReference Level
96     */
97    public function withOutClusterDuplicates()
98    {
99        $department = clone $this;
100        if ($this->offsetExists('scopes') && $this->scopes) {
101            $scopeList = clone $this->scopes;
102            $department->scopes = new Collection\ScopeList();
103            $removeScopeList = new Collection\ScopeList();
104            if ($department->toProperty()->clusters->get()) {
105                foreach ($department->clusters as $cluster) {
106                    $cluster = new Cluster($cluster);
107                    foreach ($cluster['scopes'] as $key => $clusterScope) {
108                        $scope = $scopeList->getEntity($clusterScope['id']);
109                        if ($scope) {
110                            $scope = new Scope($scope);
111                            $cluster['scopes'][$key] = clone $scope;
112                            $removeScopeList[] = $scope;
113                        }
114                    }
115                }
116                foreach ($scopeList as $scope) {
117                    if (! $removeScopeList->hasEntity($scope['id'])) {
118                        $scope = new Scope($scope);
119                        $department->scopes->addEntity($scope);
120                    }
121                }
122            }
123        }
124        return $department;
125    }
126
127    public function withCompleteScopeList()
128    {
129        $department = clone $this;
130        $department->scopes = $this->getScopeList()->withUniqueScopes();
131        return $department;
132    }
133
134    public function getScopeList()
135    {
136        $scopeList = new Collection\ScopeList();
137        if ($this->toProperty()->clusters->isAvailable()) {
138            foreach ($this->clusters as $cluster) {
139                if (Property::__keyExists('scopes', $cluster)) {
140                    foreach ($cluster['scopes'] as $clusterScope) {
141                        $scope = new Scope($clusterScope);
142                        $scopeList->addEntity($scope);
143                    }
144                }
145            }
146        }
147        if ($this->toProperty()->scopes->isAvailable()) {
148            foreach ($this->scopes as $scope) {
149                if (! $scopeList->hasEntity($scope['id'])) {
150                    $scope = new Scope($scope);
151                    $scopeList->addEntity($scope);
152                }
153            }
154        }
155        return $scopeList;
156    }
157
158    public function hasAccess(Useraccount $useraccount)
159    {
160        return $useraccount->hasRights(['superuser']) || $useraccount->hasDepartment($this->id);
161    }
162
163    /**
164     * Reduce data of dereferenced entities to a required minimum
165     *
166     */
167    public function withLessData()
168    {
169        $entity = clone $this;
170        if (isset($entity['preferences'])) {
171            unset($entity['preferences']);
172            $entity['preferences'] = [];
173            $entity['preferences']['notifications'] = [];
174            $entity['preferences']['notifications']['enabled'] = $this['preferences']['notifications']['enabled'];
175        }
176        if (isset($entity['email'])) {
177            unset($entity['email']);
178        }
179        if (isset($entity['scopes'])) {
180            unset($entity['scopes']);
181        }
182        if (isset($entity['clusters'])) {
183            unset($entity['clusters']);
184        }
185        if (isset($entity['links'])) {
186            unset($entity['links']);
187        }
188        if (isset($entity['dayoff'])) {
189            unset($entity['dayoff']);
190        }
191        if (isset($entity['contact'])) {
192            unset($entity['contact']);
193        }
194        return $entity;
195    }
196}