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