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