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