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