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