Lines 93.35% 281 / 301
Methods 90.90% 30 / 33
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readEntity 100.00% 9 / 9 100.00% 1 / 1 2
 readResolvedReferences 100.00% 18 / 18 100.00% 1 / 1 3
 readLoggedInHashByName 100.00% 3 / 3 100.00% 1 / 1 2
 readLoggedInListByScope 100.00% 15 / 15 100.00% 1 / 1 4
 readLoggedInListByCluster 100.00% 6 / 6 100.00% 1 / 1 3
 readWorkstationByScopeAndName 100.00% 11 / 11 100.00% 1 / 1 2
 readCollectionByDepartmentId 100.00% 12 / 12 100.00% 1 / 1 4
 writeEntityLoginByOidc 100.00% 18 / 18 100.00% 1 / 1 2
 writeEntityLoginByName 100.00% 23 / 23 100.00% 1 / 1 3
 writeEntityLogoutByName 100.00% 4 / 4 100.00% 1 / 1 2
 writeAssignedProcess 96.00% 24 / 25 0.00% 0 / 1 4
 writeRemovedProcess 100.00% 19 / 19 100.00% 1 / 1 3
 updateEntity 100.00% 7 / 7 100.00% 1 / 1 2
 updateEntityAuthkey 0.00% 0 / 14 0.00% 0 / 1 6
 [BO\Zmsbackend\Base] __construct 100.00% 2 / 2 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] init 100.00% 2 / 2 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] getWriter 100.00% 4 / 4 100.00% 1 / 1 2
 [BO\Zmsbackend\Base] getReader 100.00% 5 / 5 100.00% 1 / 1 3
 [BO\Zmsbackend\Base] fetchPreparedStatement 100.00% 9 / 9 100.00% 1 / 1 3
 [BO\Zmsbackend\Base] startExecute 100.00% 5 / 5 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] pdoExceptionHandler 100.00% 13 / 13 100.00% 1 / 1 4
 [BO\Zmsbackend\Base] fetchStatement 100.00% 3 / 3 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] fetchOne 100.00% 7 / 7 100.00% 1 / 1 2
 [BO\Zmsbackend\Base] fetchRow 100.00% 7 / 7 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] fetchValue 100.00% 7 / 7 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] fetchAll 100.00% 7 / 7 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] fetchHandle 100.00% 5 / 5 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] fetchList 100.00% 8 / 8 100.00% 1 / 1 2
 [BO\Zmsbackend\Base] writeItem 100.00% 7 / 7 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] deleteItem 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] perform 100.00% 7 / 7 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] fetchAffected 100.00% 8 / 8 100.00% 1 / 1 1
 [BO\Zmsbackend\Base] hashStringValue 50.00% 5 / 10 0.00% 0 / 1 8.12
16class Workstation extends \BO\Zmsbackend\Base
17{
18    public function readEntity($loginName, int $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, int $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, int $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, int $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, int $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, int $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, int $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, int $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, int $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 \BO\Zmsentities\Workstation $entity
280     * @param int $resolveReferences
281     *
282     * @return Entity
283     */
284    public function updateEntity(\BO\Zmsentities\Workstation $entity, int $resolveReferences = 0)
285    {
286        $query = new \BO\Zmsbackend\Workstation\Repository\Workstation(\BO\Zmsbackend\Query\Base::UPDATE);
287        $query->addConditionWorkstationId($entity->getId());
288        $values = $query->reverseEntityMapping($entity);
289        $query->addValues($values);
290
291        if ($this->perform($query->getLockWorkstationId(), ['workstationId' => $entity->getId()])) {
292            $this->writeItem($query);
293        }
294        return $this->readEntity($entity->useraccount['id'], $resolveReferences);
295    }
296
297    /**
298     * update a workstations authkey - is needed for openid login
299     *
300     * @param string $loginName
301     * @param string $password
302     * @param string $authKey
303     * @param \DateTimeInterface $sessionExpiry
304     * @param int $resolveReferences
305     *
306     * @return Entity
307     * @psalm-api
308     */
309    public function updateEntityAuthkey($loginName, $password, $authKey, \DateTimeInterface $sessionExpiry, int $resolveReferences)
310    {
311        $query = \BO\Zmsbackend\Workstation\Repository\Workstation::QUERY_UPDATE_AUTHKEY;
312        $result = $this->perform(
313            $query,
314            array(
315                $authKey,
316                $sessionExpiry->format('Y-m-d H:i:s'),
317                $loginName,
318                $password
319            )
320        );
321        if ($result) {
322            $workstation = $this->readEntity($loginName, $resolveReferences);
323            $workstation->authkey = $authKey;
324        }
325        return $workstation;
326    }
327}

Inherited from BO\Zmsbackend\Base

38    public function __construct(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null)
39    {
40        $this->writeDb = $writeConnection;
41        $this->readDb = $readConnection;
42    }
44    public static function init(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null): static
45    {
46        $instance = new static($writeConnection, $readConnection);
47        return $instance;
48    }
50    public function getWriter(): Connection\Pdo
51    {
52        if (null === $this->writeDb) {
53            $this->writeDb = Connection\Select::getWriteConnection();
54            $this->readDb = $this->writeDb;
55        }
56        return $this->writeDb;
57    }
59    public function getReader(): Connection\Pdo
60    {
61        if (null !== $this->writeDb) {
62            // if readDB gets a reset, still use writeDB
63            return $this->writeDb;
64        }
65        if (null === $this->readDb) {
66            $this->readDb = Connection\Select::getReadConnection();
67        }
68        return $this->readDb;
69    }
71    public function fetchPreparedStatement(Query\Base|string $query)
72    {
73        $sql = "$query";
74        $reader = $this->getReader();
75        if (spl_object_hash($reader) != static::$preparedConnectionId) {
76            // do not use prepared statements on a different connection
77            static::$preparedCache = [];
78            static::$preparedConnectionId = spl_object_hash($reader);
79        }
80        if (!isset(static::$preparedCache[$sql])) {
81            $prepared = $this->getReader()->prepare($sql);
82            static::$preparedCache[$sql] = $prepared;
83        }
84        return static::$preparedCache[$sql];
85    }
87    public function startExecute($statement, array $parameters)
88    {
89        $statement = static::pdoExceptionHandler(function () use ($statement, $parameters) {
90            $statement->execute($parameters);
91            return $statement;
92        });
93        return $statement;
94    }
96    protected static function pdoExceptionHandler(\Closure $pdoFunction, $parameters = [])
97    {
98        try {
99            $statement = $pdoFunction($parameters);
100        } catch (\PDOException $pdoException) {
101            if (stripos($pdoException->getMessage(), 'Lock wait timeout') !== false) {
102                throw new \BO\Zmsbackend\Exception\Pdo\LockTimeout();
103            }
104            //@codeCoverageIgnoreStart
105            if (stripos($pdoException->getMessage(), 'Deadlock found') !== false) {
106                throw new \BO\Zmsbackend\Exception\Pdo\DeadLockFound();
107            }
108            //@codeCoverageIgnoreEnd
109            $message = "SQL: "
110                . " Err: "
111                . $pdoException->getMessage()
112                //. " || Statement: "
113                //.$statement->queryString
114                //." || Parameters=". var_export($parameters, true)
115                ;
116            throw new \BO\Zmsbackend\Exception\Pdo\PDOFailed($message, 0, $pdoException);
117        }
118        return $statement;
119    }
121    public function fetchStatement(Query\Base $query)
122    {
123        $parameters = $query->getParameters();
124        $statement = $this->startExecute($this->fetchPreparedStatement($query), $parameters);
125        return $statement;
126    }
133    public function fetchOne(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity)
134    {
135        $statement = $this->fetchStatement($query);
136        $data = $statement->fetch(\PDO::FETCH_ASSOC);
137        if ($data) {
138            $entity->exchangeArray($query->postProcessJoins($data));
139            $entity->setResolveLevel($query->getResolveLevel());
140        }
141        $statement->closeCursor();
142        return $entity;
143    }
145    public function fetchRow(Query\Base|string $query, array|null $parameters = null)
146    {
147        return static::pdoExceptionHandler(function () use ($query, $parameters) {
148            $prepared = $this->fetchPreparedStatement($query);
149            $prepared->execute($parameters);
150            $row = $prepared->fetch(\PDO::FETCH_ASSOC);
151            $prepared->closeCursor();
152            return $row;
153        });
154    }
156    public function fetchValue(Query\Base|string $query, array|null $parameters = null)
157    {
158        return static::pdoExceptionHandler(function () use ($query, $parameters) {
159            $prepared = $this->fetchPreparedStatement($query);
160            $prepared->execute($parameters);
161            $value = $prepared->fetchColumn();
162            $prepared->closeCursor();
163            return $value;
164        });
165    }
167    public function fetchAll(Query\Base|string $query, array|null $parameters = null)
168    {
169        return static::pdoExceptionHandler(function () use ($query, $parameters) {
170            $prepared = $this->fetchPreparedStatement($query);
171            $prepared->execute($parameters);
172            $list = $prepared->fetchAll(\PDO::FETCH_ASSOC);
173            $prepared->closeCursor();
174            return $list;
175        });
176    }
178    public function fetchHandle(Query\Base|string $query, array|null $parameters = null)
179    {
180        return static::pdoExceptionHandler(function () use ($query, $parameters) {
181            $prepared = $this->fetchPreparedStatement($query);
182            $prepared->execute($parameters);
183            return $prepared;
184        });
185    }
187    public function fetchList(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity, array|\ArrayAccess $resultList = [])
188    {
189        $statement = $this->fetchStatement($query);
190        while ($data = $statement->fetch(\PDO::FETCH_ASSOC)) {
191            $dataEntity = clone $entity;
192            $dataEntity->exchangeArray($query->postProcessJoins($data));
193            $dataEntity->setResolveLevel($query->getResolveLevel());
194            $resultList[] = $dataEntity;
195        }
196        $statement->closeCursor();
197        return $resultList;
198    }
206    public function writeItem(Query\Base $query)
207    {
208        return static::pdoExceptionHandler(function () use ($query) {
209            $this->getWriter(); //Switch to writer for write/delete
210            $statement = $this->fetchPreparedStatement($query);
211            $status = $statement->execute($query->getParameters());
212            $statement->closeCursor();
213            return $status;
214        });
215    }
221    public function deleteItem(Query\Base $query)
222    {
223        return $this->writeItem($query);
224    }
226    public function perform(string $sql, array|null $parameters = null)
227    {
228        return static::pdoExceptionHandler(function () use ($sql, $parameters) {
229            $this->getWriter(); //Switch to writer for perform
230            $prepared = $this->fetchPreparedStatement($sql);
231            $status = $prepared->execute($parameters);
232            $prepared->closeCursor();
233            return $status;
234        });
235    }
237    public function fetchAffected(string $sql, array|null $parameters = null)
238    {
239        return static::pdoExceptionHandler(function () use ($sql, $parameters) {
240            $this->getWriter(); //Switch to writer for perform
241            $prepared = $this->fetchPreparedStatement($sql);
242            $prepared->execute($parameters);
243            $count = $prepared->rowCount();
244            $prepared->closeCursor();
245            return $count;
246        });
247    }
268    public function hashStringValue(string $value, ?callable $callable = null): string
269    {
270        if ($callable === null) {
271            $callable = 'sha1';
272        }
273
274        if (is_callable($callable)) {
275            $hash = $callable($value);
276
277            if (is_string($callable)) {
278                return $callable . ':' . $hash;
279            }
280            if ($callable instanceof \Closure) {
281                return 'closure:' . $hash;
282            }
283            // else
284            return 'custom:' . $hash;
285        }
286
287        throw new \InvalidArgumentException('hashStringValue() should be called with a callable as second parameter.');
288    }