Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ProcessRedirect | |
0.00% |
0 / 52 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| readResponse | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
2 | |||
| testProcessData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| testProcessAccess | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| readValidProcess | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package ZMS API |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsbackend\Process\Api; |
| 9 | |
| 10 | use BO\Slim\Render; |
| 11 | use BO\Mellon\Validator; |
| 12 | use BO\Zmsbackend\Process\Service\Process; |
| 13 | use BO\Zmsbackend\Process\Service\Process as Query; |
| 14 | use BO\Zmsbackend\Process\Service\ProcessStatusQueued; |
| 15 | use BO\Zmsbackend\Workstation\Service\Workstation; |
| 16 | use BO\Zmsentities\Collection\RequestList; |
| 17 | use BO\Zmsbackend\ProcessSearchHistory\Service\ProcessSearchHistory as HistoryService; |
| 18 | |
| 19 | /** |
| 20 | * @SuppressWarnings(Coupling) |
| 21 | */ |
| 22 | class ProcessRedirect extends \BO\Zmsbackend\Api\BaseController |
| 23 | { |
| 24 | /** |
| 25 | * @SuppressWarnings(Param) |
| 26 | * @return \Psr\Http\Message\ResponseInterface |
| 27 | */ |
| 28 | #[\Override] |
| 29 | public function readResponse(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) |
| 30 | { |
| 31 | $workstation = (new \BO\Zmsbackend\Helper\User($request))->checkPermissions('appointment'); |
| 32 | $input = Validator::input()->isJson()->assertValid()->getValue(); |
| 33 | $entity = new \BO\Zmsentities\Process($input); |
| 34 | $newProcess = new \BO\Zmsentities\Process($input); |
| 35 | $process = $this->readValidProcess($workstation, $entity, $input, $workstation); |
| 36 | $newProcess->requests = new RequestList(); |
| 37 | $this->testProcessAccess($workstation, $process); |
| 38 | \BO\Zmsbackend\Connection\Select::getWriteConnection(); |
| 39 | $processStatusArchived = new \BO\Zmsbackend\Process\Service\ProcessStatusArchived(); |
| 40 | $process->status = 'finished'; |
| 41 | $process = (new Query())->updateEntity($process, \App::$now, 0, 'processing', $workstation->getUseraccount()); |
| 42 | (new \BO\Zmsbackend\Workstation\Service\Workstation())->writeRemovedProcess($workstation); |
| 43 | $processStatusArchived->writeEntityFinished($process, \App::$now, false, $workstation->getUseraccount(), HistoryService::STATUS_COMPLETED); |
| 44 | $newProcess->displayNumber = $process->displayNumber; |
| 45 | $newProcess = (new \BO\Zmsbackend\Process\Service\Process())->redirectToScope($newProcess, $process->scope, $process->queue['number'] ?? $process->id, $workstation->getUseraccount()); |
| 46 | \App::$log->info('Process redirected', [ |
| 47 | 'process_redirected' => true, |
| 48 | 'process_id' => $newProcess->id, |
| 49 | 'scope_id' => $newProcess->scope['id'] |
| 50 | ]); |
| 51 | $message = \BO\Zmsbackend\Api\Response\Message::create($request); |
| 52 | $message->data = $newProcess; |
| 53 | $response = Render::withLastModified($response, time(), '0'); |
| 54 | return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return void |
| 59 | */ |
| 60 | protected function testProcessData($entity) |
| 61 | { |
| 62 | $entity->testValid(); |
| 63 | $authCheck = (new Query())->readAuthKeyByProcessId($entity->id); |
| 64 | if (! $authCheck) { |
| 65 | throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound(); |
| 66 | } elseif ($authCheck['authKey'] !== $entity->authKey) { |
| 67 | throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return void |
| 73 | */ |
| 74 | protected function testProcessAccess($workstation, $process) |
| 75 | { |
| 76 | $cluster = (new \BO\Zmsbackend\Cluster\Service\Cluster())->readByScopeId($workstation->scope['id'], 1); |
| 77 | $workstation->validateProcessScopeAccess($workstation->getScopeList($cluster), $process); |
| 78 | if ($workstation->process && $workstation->process->hasId() && $workstation->process->id != $process->id) { |
| 79 | $exception = new \BO\Zmsbackend\Workstation\Exception\WorkstationHasAssignedProcess(); |
| 80 | $exception->data = [ |
| 81 | 'process' => $workstation->process |
| 82 | ]; |
| 83 | throw $exception; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | protected function readValidProcess($workstation, \BO\Zmsentities\Process $entity, $input) |
| 88 | { |
| 89 | if ($entity->hasProcessCredentials()) { |
| 90 | $this->testProcessData($entity); |
| 91 | $entity->addData($input); |
| 92 | $process = (new Query())->updateEntity($entity, \App::$now, 0, null, $workstation->getUseraccount()); |
| 93 | } elseif ($entity->hasQueueNumber()) { |
| 94 | // Allow waitingnumbers over 1000 with the fourth parameter |
| 95 | $process = \BO\Zmsbackend\Process\Service\ProcessStatusQueued::init() |
| 96 | ->readByQueueNumberAndScope($entity['queue']['number'], $workstation->scope['id'], 0, 100000000); |
| 97 | if (! $process->id) { |
| 98 | $entity->testValid(); |
| 99 | throw new \BO\Zmsbackend\Process\Exception\ProcessInvalid(); |
| 100 | } |
| 101 | |
| 102 | $process->testValid(); |
| 103 | } else { |
| 104 | $entity->testValid(); |
| 105 | throw new \BO\Zmsbackend\Process\Exception\ProcessInvalid(); |
| 106 | } |
| 107 | return $process; |
| 108 | } |
| 109 | } |