Lines 97.79% 177 / 181
Methods 97.56% 40 / 41
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 addRequiredJoins 100.00% 1 / 1 100.00% 1 / 1 1
 getLockWorkstationId 100.00% 2 / 2 100.00% 1 / 1 1
 getEntityMapping 100.00% 8 / 8 100.00% 1 / 1 1
 addJoin 100.00% 6 / 6 100.00% 1 / 1 3
 addJoinScope 100.00% 8 / 8 100.00% 1 / 1 1
 addJoinUseraccount 100.00% 8 / 8 100.00% 1 / 1 1
 addConditionLoginName 100.00% 2 / 2 100.00% 1 / 1 1
 addConditionWorkstationName 100.00% 2 / 2 100.00% 1 / 1 1
 addConditionWorkstationIsNotCounter 100.00% 2 / 2 100.00% 1 / 1 1
 addConditionWorkstationId 100.00% 2 / 2 100.00% 1 / 1 1
 addConditionScopeId 100.00% 2 / 2 100.00% 1 / 1 1
 addConditionTime 100.00% 2 / 2 100.00% 1 / 1 1
 addConditionDepartmentId 100.00% 8 / 8 100.00% 1 / 1 1
 reverseEntityMapping 100.00% 13 / 13 100.00% 1 / 1 6
 [BO\Zmsbackend\Query\Base] __construct 88.23% 30 / 34 0.00% 0 / 1 10.16
 [BO\Zmsbackend\Query\Base] __toString n/a 0 / 0 n/a 0 / 0 4
 [BO\Zmsbackend\Query\Base] getName 100.00% 1 / 1 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] addSelect 100.00% 4 / 4 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] setDistinctSelect 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] setResolveLevel 100.00% 3 / 3 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] getResolveLevel 100.00% 3 / 3 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] getAlias 100.00% 5 / 5 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] getTablename 100.00% 3 / 3 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] addTable 100.00% 4 / 4 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] addTableAlias 100.00% 4 / 4 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] addResolvedReferences 100.00% 11 / 11 100.00% 1 / 1 3
 [BO\Zmsbackend\Query\Base] setWithEntities 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] leftJoin 100.00% 5 / 5 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] innerJoin 100.00% 5 / 5 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] getSql 100.00% 2 / 2 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] getParameters 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] getReferenceMapping 100.00% 2 / 2 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] expression 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] addEntityMapping 100.00% 3 / 3 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] getPrefixed 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] getPrefixedList 100.00% 4 / 4 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] addReferenceMapping 100.00% 3 / 3 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] addLimit 100.00% 4 / 4 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] addValues 100.00% 2 / 2 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] postProcess 100.00% 1 / 1 100.00% 1 / 1 1
 [BO\Zmsbackend\Query\Base] postProcessJoins 100.00% 4 / 4 100.00% 1 / 1 2
 [BO\Zmsbackend\Query\Base] shouldLoadEntity 100.00% 3 / 3 100.00% 1 / 1 2
9class Workstation extends \BO\Zmsbackend\Query\Base implements \BO\Zmsbackend\Query\MappingInterface
10{
11    /**
12     * @var String TABLE mysql table reference
13     */
14    const string TABLE = 'nutzer';
15
16    const QUERY_LOGIN = '
17        UPDATE
18            ' . self::TABLE . '
19        SET
20            `SessionID`=SHA2(?, 256),
21            `sessionExpiry`=?,
22            `Datum`=?,
23            `lastUpdate`=?,
24            `Arbeitsplatznr`="",
25            `aufrufzusatz`="",
26            `StandortID`=0
27        WHERE
28            `Name`=? AND
29            `Passworthash` = ?
30    ';
31
32    const QUERY_LOGIN_OIDC = '
33        UPDATE
34            ' . self::TABLE . '
35        SET
36            `SessionID`=SHA2(?, 256),
37            `sessionExpiry`=?,
38            `Datum`=?,
39            `lastUpdate`=?,
40            `Arbeitsplatznr`="",
41            `aufrufzusatz`="",
42            `StandortID`=0
43        WHERE
44            `Name`=?
45    ';
46
47    const QUERY_PROCESS_RESET = '
48        UPDATE
49            ' . \BO\Zmsbackend\Process\Repository\Process::TABLE . '
50        SET
51            `NutzerID` = 0,
52            `aufrufzeit` = "00:00:00"
53        WHERE
54            `NutzerID` = ?
55    ';
56
57    const QUERY_LOGOUT = '
58        UPDATE
59            ' . self::TABLE . '
60        SET
61            `SessionID`="",
62            `sessionExpiry`=NULL,
63            `StandortID`=0,
64            `Datum`="0000-00-00",
65            `Arbeitsplatznr`="",
66            `aufrufzusatz`=""
67        WHERE
68            `Name`= ?
69    ';
70
71    const QUERY_LOGGEDIN_CHECK = '
72        SELECT
73            SessionID as hash
74        FROM
75            ' . self::TABLE . '
76        WHERE
77            `Name` = :loginName
78        LIMIT 1
79    ';
80
81    const QUERY_UPDATE_AUTHKEY = '
82        UPDATE
83            ' . self::TABLE . '
84        SET
85            `SessionID`=SHA2(?, 256),
86            `sessionExpiry`=?
87        WHERE
88            `Name`= ?  AND
89            `Passworthash` = ?
90    ';
91
92    /**
93     * @return void
94     */
95    #[\Override]
96    protected function addRequiredJoins()
97    {
98    }
99
100    public function getLockWorkstationId(): string
101    {
102        return 'SELECT * FROM `' . self::getTablename() . '` A
103            WHERE A.`NutzerID` = :workstationId FOR UPDATE';
104    }
105
106    /**
107     * @return string[]
108     *
109     */
110    #[\Override]
111    public function getEntityMapping()
112    {
113        return [
114            'id' => 'workstation.NutzerID',
115            'hint' => 'workstation.aufrufzusatz',
116            'name' => 'workstation.Arbeitsplatznr',
117            'queue__appointmentsOnly' => 'workstation.Kalenderansicht',
118            'queue__clusterEnabled' => 'workstation.clusteransicht',
119            'scope__id' => 'workstation.StandortID'
120        ];
121    }
122
123    #[\Override]
124    public function addJoin()
125    {
126        $joins = [];
127
128        if ($this->shouldLoadEntity('useraccount')) {
129            $joins[] = $this->addJoinUseraccount();
130        }
131
132        if ($this->shouldLoadEntity('scope')) {
133            $joins[] = $this->addJoinScope();
134        }
135
136        return $joins;
137    }
138
139    public function addJoinScope(): \BO\Zmsbackend\Query\Scope
140    {
141        $this->leftJoin(
142            new \BO\Zmsbackend\Query\Alias(\BO\Zmsbackend\Query\Scope::TABLE, 'scope'),
143            'workstation.StandortID',
144            '=',
145            'scope.StandortID'
146        );
147        $joinQuery = new \BO\Zmsbackend\Query\Scope($this, $this->getPrefixed('scope__'));
148        return $joinQuery;
149    }
150
151
152    public function addJoinUseraccount(): \BO\Zmsbackend\Useraccount\Repository\Useraccount
153    {
154        $this->leftJoin(
155            new \BO\Zmsbackend\Query\Alias(\BO\Zmsbackend\Useraccount\Repository\Useraccount::TABLE, 'useraccount'),
156            'workstation.NutzerID',
157            '=',
158            'useraccount.NutzerID'
159        );
160        $joinQuery = new \BO\Zmsbackend\Useraccount\Repository\Useraccount($this, $this->getPrefixed('useraccount__'));
161        return $joinQuery;
162    }
163
164    public function addConditionLoginName($loginName): static
165    {
166        $this->query->where('workstation.Name', '=', $loginName);
167        return $this;
168    }
169
170    /**
171     * @psalm-api
172     */
173    public function addConditionWorkstationName($workstationName): static
174    {
175        $this->query->where('workstation.Arbeitsplatznr', '=', $workstationName);
176        return $this;
177    }
178
179    /**
180     * @psalm-api
181     */
182    public function addConditionWorkstationIsNotCounter(): static
183    {
184        $this->query->where('workstation.Arbeitsplatznr', '>', 0);
185        return $this;
186    }
187
188    public function addConditionWorkstationId($workstationId): static
189    {
190        $this->query->where('workstation.NutzerID', '=', $workstationId);
191        return $this;
192    }
193
194    public function addConditionScopeId($scopeId): static
195    {
196        $this->query->where('workstation.StandortID', '=', $scopeId);
197        return $this;
198    }
199
200    /**
201     * @psalm-api
202     */
203    public function addConditionTime($now): static
204    {
205        $this->query->where('workstation.Datum', '=', $now->format('Y-m-d'));
206        return $this;
207    }
208
209    public function addConditionDepartmentId($departmentId): static
210    {
211        $this->leftJoin(
212            new \BO\Zmsbackend\Query\Alias(\BO\Zmsbackend\Useraccount\Repository\Useraccount::TABLE_ASSIGNMENT, 'workstation_department'),
213            'workstation.NutzerID',
214            '=',
215            'workstation_department.nutzerid'
216        );
217        $this->query->where('workstation_department.behoerdenid', '=', $departmentId);
218        return $this;
219    }
220
221    public function reverseEntityMapping(\BO\Zmsentities\Workstation $entity): array
222    {
223        $data = array();
224        if ((isset($entity['hint']) && '' == $entity['hint']) || ! isset($entity['hint'])) {
225            $data['aufrufzusatz'] = $entity->name;
226        } else {
227            $data['aufrufzusatz'] = $entity['hint'];
228        }
229
230        $data['Kalenderansicht'] = $entity->getQueuePreference('appointmentsOnly', true);
231        $data['clusteransicht'] = $entity->getQueuePreference('clusterEnabled', true);
232        if (isset($entity->scope['id'])) {
233            $data['StandortID'] = $entity->scope['id'];
234        }
235        $data['Arbeitsplatznr'] = $entity->name;
236
237        $data = array_filter($data, function ($value) {
238            return ($value !== null && $value !== false);
239        });
240        return $data;
241    }
242}

Inherited from BO\Zmsbackend\Query\Base

96    public function __construct(
97        $queryType,
98        $prefix = '',
99        string|false $name = false,
100        mixed $resolveLevel = null,
101        array $withEntities = []
102    ) {
103        $this->prefix = $prefix;
104        $this->name = $name;
105        $this->withEntities = $withEntities;
106        $this->setResolveLevel($resolveLevel);
107        $dialect = new MySQL();
108        if (self::SELECT === $queryType) {
109            $this->query = new Select($dialect);
110            $this->addSelect();
111        } elseif (self::INSERT === $queryType) {
112            $this->query = new Insert($dialect);
113            $this->addTable();
114        } elseif (self::UPDATE === $queryType) {
115            $this->query = new Update($dialect);
116            $this->addTableAlias();
117        } elseif (self::REPLACE === $queryType) {
118            $this->query = new INSERT($dialect);
119            $this->query->queryBaseStatement('REPLACE INTO');
120            $this->addTable();
121        } elseif (self::DELETE === $queryType) {
122            $this->query = new Delete($dialect);
123            $this->query->queryBaseStatement('DELETE ' . $this::getAlias() . ' FROM');
124            $this->addTableAlias();
125        } elseif ($queryType instanceof self) {
126            $this->query = $queryType->query;
127            // Share join-alias state with the parent query (intentional by-ref).
128            /** @psalm-suppress UnsupportedPropertyReferenceUsage */
129            $this->joinedAliasList =& $queryType->joinedAliasList;
130            $this->resolveLevel = $queryType->resolveLevel - 1;
131        } elseif ($queryType instanceof QueryBuilder) {
132            $this->query = $queryType;
133        } else {
134            throw new \InvalidArgumentException(
135                'Unsupported query type for ' . static::class . ': '
136                . (is_object($queryType) ? $queryType::class : gettype($queryType))
137            );
138        }
139        if ($this->query instanceof Select) {
140            $this->addRequiredJoins();
141        }
142    }
147    public function __toString()
148    {
149        if ($this->name) {
150            $name = $this->name . '_' . $this->prefix . (string) $this->resolveLevel;
151            if (!isset(static::$sqlCache[$name])) {
152                static::$sqlCache[$name] = $this->getSql();
153            }
154            return static::$sqlCache[$name];
155        }
156        if ($this->currentSqlString) {
157            $sql = $this->currentSqlString;
158        } else {
159            $sql = $this->getSql();
160        }
161        return $sql;
162    }
164    public function getName()
165    {
166        return $this->name ? $this->name : get_class($this);
167    }
174    protected function addSelect(): static
175    {
176        $table = $this::getTablename();
177        $alias = $this::getAlias();
178        $this->query->from($table, $alias);
179        return $this;
180    }
182    public function setDistinctSelect(): void
183    {
184        $this->query->queryBaseStatement('SELECT DISTINCT');
185    }
187    public function setResolveLevel(int|null $resolveLevel): static
188    {
189        if ($resolveLevel !== null) {
190            $this->resolveLevel = $resolveLevel;
191        }
192        return $this;
193    }
195    public function getResolveLevel()
196    {
197        if (null === $this->resolveLevel) {
198            throw new \Exception("Required setting for resolveReferenceLevel missing in " . get_class($this));
199        }
200        return $this->resolveLevel;
201    }
210    public static function getAlias()
211    {
212        $class = get_called_class();
213        $alias = constant($class . '::ALIAS');
214        if (null === $alias) {
215            $alias = lcfirst(preg_replace('#^.*\\\#', '', $class));
216        }
217        return $alias;
218    }
225    public static function getTablename()
226    {
227        $class = get_called_class();
228        $table = constant($class . '::TABLE');
229        return $table;
230    }
237    protected function addTable(): static
238    {
239        $table = $this::getTablename();
240        $alias = $this::getAlias();
241        $this->query->table($table, $alias);
242        return $this;
243    }
250    protected function addTableAlias(): static
251    {
252        $table = $this::getTablename();
253        $alias = $this::getAlias();
254        $this->query->table(self::expression($table . ' ' . $alias));
255        return $this;
256    }
273    public function addResolvedReferences($depth): static
274    {
275        $this->setResolveLevel($depth);
276        if ($depth > 0) {
277            $queryList = $this->addJoin();
278            foreach ($queryList as $query) {
279                $query->setResolveLevel($depth);
280                $query->setWithEntities($this->withEntities);
281                $query->addResolvedReferences($depth - 1);
282                $query->addEntityMapping();
283            }
284            $this->joinedQueryList = $queryList;
285        } else {
286            $this->addReferenceMapping();
287        }
288        return $this;
289    }
294    public function setWithEntities($withEntities = []): void
295    {
296        $this->withEntities = $withEntities;
297    }
309    protected function leftJoin(Alias $alias, string|Expression|null $left = null, string|null $operator = null, string|null $right = null): QueryBuilder
310    {
311        $aliasId = $alias->getAliasIdentifier();
312        if (!in_array($aliasId, $this->joinedAliasList)) {
313            $this->joinedAliasList[] = $aliasId;
314            $this->query->leftJoin($alias, $left, $operator, $right);
315        } else {
316            //throw new \Exception("Tried to add Alias ".$aliasId);
317        }
318        return $this->query;
319    }
321    protected function innerJoin(Alias $alias, string|null $left = null, string|null $operator = null, string|null $right = null): QueryBuilder
322    {
323        $aliasId = $alias->getAliasIdentifier();
324        if (!in_array($aliasId, $this->joinedAliasList)) {
325            $this->joinedAliasList[] = $aliasId;
326            $this->query->join($alias, $left, $operator, $right);
327        }
328        return $this->query;
329    }
337    public function getSql()
338    {
339        $this->currentSqlString = (string)$this->query;
340        return $this->currentSqlString;
341    }
348    public function getParameters()
349    {
350        return $this->query->params();
351    }
357    public function getReferenceMapping()
358    {
359        return [
360        ];
361    }
368    protected static function expression(string $string)
369    {
370        return new Expression($string);
371    }
379    public function addEntityMapping(mixed $type = null): static
380    {
381        // Concrete query classes provide getEntityMapping(); not all declare MappingInterface.
382        /** @psalm-suppress UndefinedMethod, TooManyArguments */
383        $entityMapping = $this->getPrefixedList($this->getEntityMapping($type));
384        $this->query->select($entityMapping);
385        return $this;
386    }
388    protected function getPrefixed($prefix): string
389    {
390        return $this->prefix . $prefix;
391    }
393    protected function getPrefixedList(array $unprefixedList): array
394    {
395        $prefixed = [];
396        foreach ($unprefixedList as $key => $value) {
397            $prefixed[$this->getPrefixed($key)] = $value;
398        }
399        return $prefixed;
400    }
405    protected function addReferenceMapping(): static
406    {
407        $referenceMapping = $this->getPrefixedList($this->getReferenceMapping());
408        $this->query->select($referenceMapping);
409        return $this;
410    }
412    public function addLimit(int $count, ?int $offset = null): static
413    {
414        $this->query->limit($count);
415        if ($offset) {
416            $this->query->offset($offset);
417        }
418        return $this;
419    }
424    public function addValues(array $values): static
425    {
426        $this->query->values($values);
427        return $this;
428    }
434    public function postProcess($data)
435    {
436        return $data;
437    }
445    public function postProcessJoins(array $data)
446    {
447        $data = $this->postProcess($data);
448        foreach ($this->joinedQueryList as $query) {
449            $data = $query->postProcess($data);
450        }
451        return $data;
452    }
454    public function shouldLoadEntity(string $name): bool
455    {
456        if (empty($this->withEntities)) {
457            return true;
458        }
459
460        return in_array($name, $this->withEntities);
461    }