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