Lines
91.84%
169 / 184
Functions and Methods
85.71%
12 / 14
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Workstation | 91.84% | 169 / 184 | 85.71% | 12 / 14 | 38.78 | 0.00% | 0 / 1 | |||
| 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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Workstation\Service; | |
| 4 | ||
| 5 | use BO\Zmsentities\Workstation as Entity; | |
| 6 | use BO\Zmsentities\Useraccount as UseraccountEntity; | |
| 7 | use BO\Zmsentities\Scope as ScopeEntity; | |
| 8 | use BO\Zmsentities\Process as ProcessEntity; | |
| 9 | use BO\Zmsentities\Collection\WorkstationList as Collection; | |
| 10 | ||
| 11 | /** | |
| 12 | * @SuppressWarnings(Coupling) | |
| 13 | * @SuppressWarnings(Public) | |
| 14 | * | |
| 15 | */ | |
| 16 | class 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 | } |