Lines 5.17% 3 / 58
Methods 20.00% 1 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readResponse 0.00% 0 / 27 0.00% 0 / 1 6
 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
 [BO\Zmsbackend\Api\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
22class 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        if (!is_array($input)) {
36            $entity->testValid();
37            throw new \BO\Zmsbackend\Process\Exception\ProcessInvalid();
38        }
39        $process = $this->readValidProcess($workstation, $entity, $input, $workstation);
40        $newProcess->requests = new RequestList();
41        $this->testProcessAccess($workstation, $process);
42        \BO\Zmsbackend\Connection\Select::getWriteConnection();
43        $processStatusArchived = new \BO\Zmsbackend\Process\Service\ProcessStatusArchived();
44        $process->status = 'finished';
45        $process = (new Query())->updateEntity($process, \App::$now, 0, 'processing', $workstation->getUseraccount());
46        (new \BO\Zmsbackend\Workstation\Service\Workstation())->writeRemovedProcess($workstation);
47        $processStatusArchived->writeEntityFinished($process, \App::$now, false, $workstation->getUseraccount(), HistoryService::STATUS_COMPLETED);
48        $newProcess->displayNumber = $process->displayNumber;
49        $newProcess = (new \BO\Zmsbackend\Process\Service\Process())->redirectToScope($newProcess, $process->scope, $process->queue['number'] ?? $process->id, $workstation->getUseraccount());
50        \App::$log->info('Process redirected', [
51            'process_redirected' => true,
52            'process_id' => $newProcess->id,
53            'scope_id' => $newProcess->scope['id']
54        ]);
55        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
56        $message->data = $newProcess;
57        $response = Render::withLastModified($response, time(), '0');
58        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
59    }
60
61    /**
62     * @return void
63     */
64    protected function testProcessData(\BO\Zmsentities\Process $entity)
65    {
66        $entity->testValid();
67        $authCheck = (new Query())->readAuthKeyByProcessId($entity->id);
68        if (! $authCheck) {
69            throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound();
70        } elseif ($authCheck['authKey'] !== $entity->authKey) {
71            throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed();
72        }
73    }
74
75    /**
76     * @return void
77     */
78    protected function testProcessAccess(\BO\Zmsentities\Workstation $workstation, \BO\Zmsentities\Process $process)
79    {
80        $cluster = (new \BO\Zmsbackend\Cluster\Service\Cluster())->readByScopeId($workstation->scope['id'], 1);
81        $workstation->validateProcessScopeAccess($workstation->getScopeList($cluster), $process);
82        if ($workstation->process && $workstation->process->hasId() && $workstation->process->id != $process->id) {
83            $exception = new \BO\Zmsbackend\Workstation\Exception\WorkstationHasAssignedProcess();
84            $exception->data = [
85                'process' => $workstation->process
86            ];
87            throw $exception;
88        }
89    }
90
91    protected function readValidProcess(\BO\Zmsentities\Workstation $workstation, \BO\Zmsentities\Process $entity, array $input)
92    {
93        if ($entity->hasProcessCredentials()) {
94            $this->testProcessData($entity);
95            $entity->addData($input);
96            $process = (new Query())->updateEntity($entity, \App::$now, 0, null, $workstation->getUseraccount());
97        } elseif ($entity->hasQueueNumber()) {
98            // Allow waitingnumbers over 1000 with the fourth parameter
99            $process = \BO\Zmsbackend\Process\Service\ProcessStatusQueued::init()
100                ->readByQueueNumberAndScope($entity['queue']['number'], $workstation->scope['id'], 0, 100000000);
101            if (! $process->id) {
102                $entity->testValid();
103                throw new \BO\Zmsbackend\Process\Exception\ProcessInvalid();
104            }
105
106            $process->testValid();
107        } else {
108            $entity->testValid();
109            throw new \BO\Zmsbackend\Process\Exception\ProcessInvalid();
110        }
111        return $process;
112    }
113}

Inherited from BO\Zmsbackend\Api\BaseController

20    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
21    {
22        $request = $this->initRequest($request);
23        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
24        return $this->readResponse($request, $noCacheResponse, $args);
25    }