Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.55% covered (warning)
84.55%
93 / 110
76.00% covered (warning)
76.00%
19 / 25
CRAP
0.00% covered (danger)
0.00%
0 / 1
Workstation
84.55% covered (warning)
84.55%
93 / 110
76.00% covered (warning)
76.00%
19 / 25
86.57
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getQueuePreference
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 getUseraccount
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getDepartmentById
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDepartmentList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 testDepartmentList
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getProviderOfGivenScope
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUseraccountRights
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getUseraccountPermissions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 hasSuperUseraccount
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 hasAuditAccount
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 getAuthKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasAuthKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getVariantName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getScope
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getScopeList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getScopeListFromAssignedDepartments
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 validateProcessScopeAccess
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
5.00
 setValidatedName
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 setValidatedHint
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 setValidatedScope
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 setValidatedAppointmentsOnly
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isClusterEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 hasAccessToUseraccount
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Zmsentities\Helper\Property;
6
7/**
8 * @SuppressWarnings(Complexity)
9 *
10 */
11class Workstation extends Schema\Entity
12{
13    public const PRIMARY = 'id';
14
15    public static $schema = "workstation.json";
16
17    #[\Override]
18    public function getDefaults()
19    {
20        return [
21            'id' => 0,
22            'useraccount' => new Useraccount(),
23            'process' => new Process(),
24            'name' => '',
25            'scope' => new Scope()
26        ];
27    }
28
29    public function getQueuePreference($key, $isBoolean = false)
30    {
31        $result = null;
32        if (isset($this['queue']) && Property::__keyExists($key, $this['queue'])) {
33            if ($isBoolean) {
34                $result = ($this['queue'][$key]) ? 1 : 0;
35            } else {
36                $result = $this['queue'][$key];
37            }
38        }
39        return $result;
40    }
41
42    public function getUseraccount()
43    {
44        if (!$this->useraccount instanceof Useraccount) {
45            $this->useraccount = new Useraccount($this->useraccount);
46        }
47        return $this->useraccount;
48    }
49
50    public function getDepartmentById($departmentId)
51    {
52        return $this->getUseraccount()->getDepartmentById($departmentId);
53    }
54
55    public function getDepartmentList()
56    {
57        $departmentList = new Collection\DepartmentList();
58        foreach ($this->getUseraccount()->departments as $department) {
59            $departmentList->addEntity(new Department($department));
60        }
61        return $departmentList;
62    }
63
64    public function testDepartmentList()
65    {
66        if (0 == $this->getDepartmentList()->count()) {
67            throw new Exception\WorkstationMissingAssignedDepartments();
68        }
69    }
70
71    public function getProviderOfGivenScope()
72    {
73        return $this->toProperty()->scope->provider->id->get();
74    }
75
76    public function getUseraccountRights()
77    {
78        $rights = null;
79        if (Property::__keyExists('rights', $this->useraccount)) {
80            $rights = $this->useraccount['rights'];
81        }
82        return $rights;
83    }
84
85    public function getUseraccountPermissions()
86    {
87        $rights = null;
88        if (Property::__keyExists('permissions', $this->useraccount)) {
89            $rights = $this->useraccount['permissions'];
90        }
91        return $rights;
92    }
93
94    public function hasSuperUseraccount()
95    {
96        $isSuperuser = false;
97        $userRights = $this->getUseraccountRights();
98        if (isset($userRights['superuser']) && $userRights['superuser']) {
99            $isSuperuser = true;
100        }
101        return $isSuperuser;
102    }
103
104    public function hasAuditAccount()
105    {
106        $userPermissions = $this->getUseraccountPermissions();
107        if (isset($userPermissions['logs']) && $userPermissions['logs']) {
108            return true;
109        }
110
111        $userRights = $this->getUseraccountRights();
112        if (isset($userRights['audit']) && $userRights['audit']) {
113            return true;
114        }
115
116        return false;
117    }
118
119    public function getAuthKey()
120    {
121        return bin2hex(openssl_random_pseudo_bytes(16));
122    }
123
124    public function hasAuthKey()
125    {
126        return (isset($this->authkey)) ? true : false;
127    }
128
129    public function getVariantName()
130    {
131        return (! trim($this->name)) ? 'counter' : 'workstation';
132    }
133
134    public function getName()
135    {
136        return ($this->name) ? $this->name : "Tresen";
137    }
138
139    public function getScope()
140    {
141        if (!$this->offsetExists('scope')) {
142            $this->scope = new Scope();
143        } elseif (!$this->scope instanceof Scope) {
144            $this->scope = new Scope($this->scope);
145        }
146        return $this->scope;
147    }
148
149    public function getScopeList($cluster = null)
150    {
151        $scopeList = new Collection\ScopeList();
152        $scopeList->addEntity(new Scope($this->getScope()));
153        if ($cluster && 1 == $this->queue['clusterEnabled']) {
154            $scopeList = new Collection\ScopeList();
155            $scopeList->addList($cluster->scopes);
156        }
157        return $scopeList;
158    }
159
160    public function getScopeListFromAssignedDepartments()
161    {
162        $scopeList = new Collection\ScopeList();
163        foreach ($this->getDepartmentList() as $department) {
164            $scopeList->addList($department->getScopeList());
165        }
166        foreach ($this->getScopeList() as $scope) {
167            if (! $scopeList->hasEntity($scope->id) && $scope instanceof Scope) {
168                $scopeList->addEntity($scope);
169            }
170        }
171        return $scopeList;
172    }
173
174    public function validateProcessScopeAccess($scopeList, $process = null)
175    {
176        if (null === $process) {
177            $process = $this->process;
178        }
179        if (! $scopeList->hasEntity($process->getScopeId())) {
180            $exception = new Exception\WorkstationProcessMatchScopeFailed();
181            $scopeContactName = null;
182            $currentScope = $process->getCurrentScope();
183            if ($currentScope && isset($currentScope['contact']['name'])) {
184                $scopeContactName = $currentScope['contact']['name'];
185            }
186
187            $exception->data = [
188                'id' => $process->getId(),
189                'scope' => [
190                    'id' => $process->getScopeId(),
191                    'contact' => [
192                        'name' => $scopeContactName
193                    ]
194                ]
195            ];
196            throw $exception;
197        }
198    }
199
200    public function setValidatedName(array $formData)
201    {
202        if (isset($formData['workstation']) && trim($formData['workstation']->getValue())) {
203            $this->name = $formData['workstation']->getValue();
204        } elseif (isset($formData['workstation']) && ! trim($formData['workstation']->getValue())) {
205            $this->name = '';
206        }
207        return $this;
208    }
209
210    public function setValidatedHint(array $formData)
211    {
212        if (isset($formData['hint']) && $formData['hint']->getValue()) {
213            $this->hint = $formData['hint']->getValue();
214        } elseif (isset($formData['hint']) && ! $formData['hint']->getValue()) {
215            $this->hint = '';
216        }
217        return $this;
218    }
219
220    public function setValidatedScope(array $formData)
221    {
222        if (isset($formData['scope']) && 'cluster' === $formData['scope']->getValue()) {
223            $this->queue['clusterEnabled'] = 1;
224        } elseif (isset($formData['scope'])) {
225            $this->queue['clusterEnabled'] = 0;
226            $this->scope = new Scope([
227                'id' => $formData['scope']->getValue(),
228            ]);
229        }
230        return $this;
231    }
232
233    public function setValidatedAppointmentsOnly(array $formData)
234    {
235        $this->queue['appointmentsOnly'] = (isset($formData['appointmentsOnly'])) ?
236            $formData['appointmentsOnly']->getValue() :
237            0;
238        return $this;
239    }
240
241    public function isClusterEnabled()
242    {
243        return $this->queue['clusterEnabled'] ? true : false;
244    }
245
246    public function hasAccessToUseraccount($useraccount)
247    {
248        $departmentList = $this->getDepartmentList();
249        $accessedList = $departmentList->withAccess($useraccount);
250        return ($accessedList->count()) ? true : false;
251    }
252}