Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.51% covered (warning)
82.51%
151 / 183
78.57% covered (warning)
78.57%
11 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
Workstation
82.51% covered (warning)
82.51%
151 / 183
78.57% covered (warning)
78.57%
11 / 14
45.72
0.00% covered (danger)
0.00%
0 / 1
 readEntity
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 readResolvedReferences
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
 readLoggedInHashByName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 readLoggedInListByScope
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 readLoggedInListByCluster
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 readWorkstationByScopeAndName
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 readCollectionByDepartmentId
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 writeEntityLoginByOidc
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 writeEntityLoginByName
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
3
 writeEntityLogoutByName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 writeAssignedProcess
96.00% covered (success)
96.00%
24 / 25
0.00% covered (danger)
0.00%
0 / 1
4
 writeRemovedProcess
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
3
 updateEntity
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 updateEntityAuthkey
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsentities\Workstation as Entity;
6use BO\Zmsentities\Useraccount as UseraccountEntity;
7use BO\Zmsentities\Scope as ScopeEntity;
8use BO\Zmsentities\Process as ProcessEntity;
9use BO\Zmsentities\Collection\WorkstationList as Collection;
10
11/**
12 * @SuppressWarnings(Coupling)
13 * @SuppressWarnings(Public)
14 *
15 */
16class Workstation extends Base
17{
18    public function readEntity($loginName, $resolveReferences = 0)
19    {
20        $query = new Query\Workstation(Query\Base::SELECT);
21        $query
22            ->addEntityMapping()
23            ->addConditionLoginName($loginName)
24            ->addResolvedReferences($resolveReferences);
25        $workstation = $this->fetchOne($query, new Entity());
26        if (! $workstation->hasId()) {
27            return null;
28        }
29        return $this->readResolvedReferences($workstation, $resolveReferences);
30    }
31
32    #[\Override]
33    public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences)
34    {
35        if (0 < $resolveReferences) {
36            $entity->useraccount = (new Useraccount())
37                ->readResolvedReferences(
38                    new UseraccountEntity($entity->useraccount),
39                    $resolveReferences - 1
40                );
41            if ($entity->scope['id']) {
42                $entity->scope = (new Scope())->readResolvedReferences(
43                    new ScopeEntity($entity->scope),
44                    $resolveReferences - 1
45                );
46                $entity->scope->cluster = (new Cluster())->readByScopeId($entity->scope->id);
47                $department = (new Department())->readByScopeId($entity->scope['id']);
48                $entity->linkList = (new Link())->readByDepartmentId($department->getId());
49            }
50            $entity->process = (new Process())->readByWorkstation($entity, $resolveReferences - 1);
51            $config = (new Config())->readEntity();
52            $entity->support = $config->support;
53        }
54        return $entity;
55    }
56
57    public function readLoggedInHashByName($loginName)
58    {
59        $query = Query\Workstation::QUERY_LOGGEDIN_CHECK;
60        $loggedInWorkstation = $this->getReader()->fetchOne($query, ['loginName' => $loginName]);
61        return ($loggedInWorkstation['hash']) ? $loggedInWorkstation['hash'] : null;
62    }
63
64    public function readLoggedInListByScope($scopeId, \DateTimeInterface $dateTime, $resolveReferences = 0)
65    {
66        $workstationList = new \BO\Zmsentities\Collection\WorkstationList();
67        $query = new Query\Workstation(Query\Base::SELECT);
68        $query
69            ->addEntityMapping()
70            ->addConditionScopeId($scopeId)
71            ->addConditionWorkstationIsNotCounter()
72            ->addConditionTime($dateTime)
73            ->addResolvedReferences($resolveReferences);
74        $result = $this->fetchList($query, new Entity());
75        if ($result) {
76            foreach ($result as $entity) {
77                if ($entity->hasId()) {
78                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
79                    $workstationList->addEntity($entity);
80                }
81            }
82        }
83        return $workstationList;
84    }
85
86    public function readLoggedInListByCluster($clusterId, \DateTimeInterface $dateTime, $resolveReferences = 0)
87    {
88        $workstationList = new \BO\Zmsentities\Collection\WorkstationList();
89        $cluster = (new Cluster())->readEntity($clusterId, $resolveReferences);
90        if ($cluster->toProperty()->scopes->get()) {
91            foreach ($cluster->scopes as $scope) {
92                $workstationList->addList($this->readLoggedInListByScope($scope['id'], $dateTime, $resolveReferences));
93            }
94        }
95        return $workstationList;
96    }
97
98    public function readWorkstationByScopeAndName($scopeId, $workstationName, $resolveReferences = 0)
99    {
100        $query = new Query\Workstation(Query\Base::SELECT);
101        $query
102            ->addEntityMapping()
103            ->addConditionScopeId($scopeId)
104            ->addConditionWorkstationName($workstationName)
105            ->addResolvedReferences($resolveReferences);
106        $workstation = $this->fetchOne($query, new Entity());
107        if (! $workstation->hasId()) {
108            return null;
109        }
110        $workstation = $this->readResolvedReferences($workstation, $resolveReferences);
111        return $workstation;
112    }
113
114    public function readCollectionByDepartmentId($departmentId, $resolveReferences = 0)
115    {
116        $collection = new Collection();
117        $query = new Query\Workstation(Query\Base::SELECT);
118        $query->addResolvedReferences($resolveReferences)
119            ->addConditionDepartmentId($departmentId)
120            ->addEntityMapping();
121        $result = $this->fetchList($query, new Entity());
122        if (count($result)) {
123            foreach ($result as $entity) {
124                if ($entity instanceof Entity) {
125                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
126                    $collection->addEntity($entity);
127                }
128            }
129        }
130        return $collection;
131    }
132
133    public function writeEntityLoginByOidc($loginName, $authKey, \DateTimeInterface $dateTime, \DateTimeInterface $sessionExpiry, $resolveReferences = 0)
134    {
135        $workstation = new Entity();
136        $query = Query\Workstation::QUERY_LOGIN_OIDC;
137        $result = $this->perform(
138            $query,
139            array(
140                $authKey,
141                $sessionExpiry->format('Y-m-d H:i:s'),
142                $dateTime->format('Y-m-d'),
143                $loginName
144            )
145        );
146        if ($result) {
147            $workstation = $this->readEntity($loginName, $resolveReferences);
148            $workstation->authkey = $authKey;
149            $query = Query\Workstation::QUERY_PROCESS_RESET;
150            $this->perform($query, [$workstation->id]);
151        }
152        return $workstation;
153    }
154
155    public function writeEntityLoginByName($loginName, $password, \DateTimeInterface $dateTime, \DateTimeInterface $sessionExpiry, $resolveReferences = 0)
156    {
157        $useraccount = new Useraccount();
158        $workstation = new Entity();
159        if ($useraccount->readIsUserExisting($loginName, $password)) {
160            $query = Query\Workstation::QUERY_LOGIN;
161            $authKey = (new \BO\Zmsentities\Workstation())->getAuthKey();
162            $result = $this->perform(
163                $query,
164                array(
165                    $authKey,
166                    $sessionExpiry->format('Y-m-d H:i:s'),
167                    $dateTime->format('Y-m-d'),
168                    $dateTime->format('Y-m-d H:i:s'),
169                    $loginName,
170                    $password
171                )
172            );
173            if ($result) {
174                $workstation = $this->readEntity($loginName, $resolveReferences);
175                $workstation->authkey = $authKey;
176                $query = Query\Workstation::QUERY_PROCESS_RESET;
177                $this->perform($query, [$workstation->id]);
178            }
179        } else {
180            throw new Exception\Useraccount\InvalidCredentials();
181        }
182        return $workstation;
183    }
184
185    public function writeEntityLogoutByName($loginName, $resolveReferences = 0)
186    {
187        $query = Query\Workstation::QUERY_LOGOUT;
188        $result = $this->perform($query, [$loginName]);
189        $workstation = $this->readEntity($loginName, $resolveReferences);
190        return ($result) ? $workstation : null;
191    }
192
193    /**
194     *
195     * assign a process to workstation
196     *
197     * @param \BO\Zmsentities\Workstation $workstation
198     * @param \BO\Zmsentities\Process $process
199     *
200     * @return ProcessEntity
201     */
202    public function writeAssignedProcess(
203        \BO\Zmsentities\Workstation $workstation,
204        \BO\Zmsentities\Process $process,
205        \DateTimeInterface $dateTime
206    ) {
207        $processEntity = $process;
208        $processDb = new Process();
209
210        $assignedWorkstationId = $processDb->readAssignedWorkstationIdForUpdate((int) $process->getId());
211
212        if (
213            $assignedWorkstationId !== null
214            && $assignedWorkstationId !== 0
215            && $assignedWorkstationId !== (int) $workstation->getId()
216        ) {
217            throw new \DomainException('PROCESS_ALREADY_ASSIGNED');
218        }
219        $process = (new Process())->updateEntity(
220            $process,
221            $dateTime,
222            0,
223            null,
224            $workstation->getUseraccount()
225        );
226        $query = new Query\Process(Query\Base::UPDATE);
227        $query->addConditionProcessId($process->id);
228        $query->addValues(['NutzerID' => $workstation->id]);
229        $this->writeItem($query);
230        $checksum = sha1($process->id . '-' . $workstation->getUseraccount()->id);
231        Log::writeProcessLog(
232            "UPDATE (Workstation::writeAssignedProcess) $checksum ",
233            Log::ACTION_CALLED,
234            $processEntity
235        );
236
237        return $process;
238    }
239
240    /**
241     *
242     * remove a process from workstation
243     *
244     * @param \BO\Zmsentities\Workstation $workstation
245     *
246     * @return bool
247     */
248    public function writeRemovedProcess(\BO\Zmsentities\Workstation $workstation)
249    {
250        $process = new \BO\Zmsentities\Process($workstation->process);
251        $query = new Query\Process(Query\Base::UPDATE);
252        $query->addConditionProcessId($process->id);
253        $query->addValues(
254            [
255                'aufrufzeit' => 0,
256                'NutzerID' => 0,
257                'AnzahlAufrufe' => $process->queue['callCount'],
258                'nicht_erschienen' => ('missed' == $process->status) ? 1 : 0,
259                'parked' => ('parked' == $process->status) ? 1 : 0
260            ]
261        );
262        Log::writeProcessLog(
263            "UPDATE (Workstation::writeRemovedProcess)",
264            Log::ACTION_REMOVED,
265            $process,
266            $workstation->getUseraccount()
267        );
268        return $this->writeItem($query);
269    }
270
271
272    /**
273     * update a workstation
274     *
275     * @param int|string $useraccountId
276     *
277     * @return Entity
278     */
279    public function updateEntity(\BO\Zmsentities\Workstation $entity, $resolveReferences = 0)
280    {
281        $query = new Query\Workstation(Query\Base::UPDATE);
282        $query->addConditionWorkstationId($entity->getId());
283        $values = $query->reverseEntityMapping($entity);
284        $query->addValues($values);
285
286        if ($this->perform($query->getLockWorkstationId(), ['workstationId' => $entity->getId()])) {
287            $this->writeItem($query);
288        }
289        return $this->readEntity($entity->useraccount['id'], $resolveReferences);
290    }
291
292    /**
293     * update a workstations authkey - is needed for openid login
294     *
295     * @param int|string $useraccountId
296     *
297     * @return Entity
298     */
299    public function updateEntityAuthkey($loginName, $password, $authKey, \DateTimeInterface $sessionExpiry, $resolveReferences)
300    {
301        $query = Query\Workstation::QUERY_UPDATE_AUTHKEY;
302        $result = $this->perform(
303            $query,
304            array(
305                $authKey,
306                $sessionExpiry->format('Y-m-d H:i:s'),
307                $loginName,
308                $password
309            )
310        );
311        if ($result) {
312            $workstation = $this->readEntity($loginName, $resolveReferences);
313            $workstation->authkey = $authKey;
314        }
315        return $workstation;
316    }
317}