Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.57% covered (warning)
86.57%
58 / 67
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkstationProcessFinished
86.57% covered (warning)
86.57%
58 / 67
25.00% covered (danger)
25.00%
1 / 4
18.79
0.00% covered (danger)
0.00%
0 / 1
 readResponse
89.13% covered (warning)
89.13%
41 / 46
0.00% covered (danger)
0.00%
0 / 1
11.16
 readRequeststatistic
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
 getFinishedResponse
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
 testProcess
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Mellon\Validator;
11use BO\Slim\Render;
12use Psr\Http\Message\RequestInterface;
13use BO\Zmsadmin\Helper\ProcessFinishedHelper;
14use BO\Zmsentities\Exception\WorkstationMissingAssignedProcess;
15use BO\Zmsentities\Process;
16use BO\Zmsentities\Collection\RequestList;
17use BO\Zmsentities\Requeststatistic;
18use BO\Zmsentities\Workstation;
19
20class WorkstationProcessFinished extends BaseController
21{
22    /**
23     * @SuppressWarnings(Param)
24     * @return \Psr\Http\Message\ResponseInterface
25     */
26    #[\Override]
27    public function readResponse(
28        RequestInterface $request,
29        \Psr\Http\Message\ResponseInterface $response,
30        array $args
31    ): \Psr\Http\Message\ResponseInterface {
32        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
33        if (!$workstation instanceof Workstation) {
34            throw new WorkstationMissingAssignedProcess();
35        }
36        $this->testProcess($workstation);
37        $input = $request->getParsedBody();
38        $validator = $request->getAttribute('validator');
39        $nextProcessId = $validator->getParameter('nextprocess')->isNumber()->getValue();
40        if (!$nextProcessId && is_array($input) && isset($input['nextprocess'])) {
41            $nextProcessId = Validator::value($input['nextprocess'])->isNumber()->getValue();
42        }
43        if ($nextProcessId && ! \App::$allowClusterWideCall) {
44            $nextProcess = \App::$http->readGetResult('/process/' . $nextProcessId . '/')->getEntity();
45            $workstation->validateProcessScopeAccess($workstation->getScopeList(), $nextProcess);
46        }
47        $statisticEnabled = $workstation->getScope()->getPreference('queue', 'statisticsEnabled');
48
49        if (! $statisticEnabled) {
50            $workstation->getProcess()['status'] = 'finished';
51            return $this->getFinishedResponse($workstation, null, $nextProcessId);
52        }
53
54        $scopeId = $workstation->getProcess()->getCurrentScope()['id']
55            ?? $workstation->getScope()['id'];
56
57        $requestStatistic = $this->readRequeststatistic((int) $scopeId);
58        $scopeRequestList = $requestStatistic->getScopeRequests();
59        $additionalDepartmentRequestList = $requestStatistic->getAdditionalDepartmentRequests();
60
61        $selectableRequestList = (new RequestList())
62            ->addList($scopeRequestList)
63            ->addList($additionalDepartmentRequestList);
64
65        if (is_array($input) && isset($input['process']) && array_key_exists('id', $input['process'])) {
66            $source = $workstation->getScope()->getSource();
67            $process = new ProcessFinishedHelper(
68                clone $workstation->getProcess(),
69                $input,
70                $selectableRequestList,
71                $source
72            );
73            return $this->getFinishedResponse($workstation, $process, $nextProcessId);
74        }
75
76        return Render::withHtml(
77            $response,
78            'page/workstationProcessFinished.twig',
79            array(
80                'title' => 'Kundendaten',
81                'workstation' => $workstation,
82                'scopeRequestList' => $scopeRequestList->toSortedByGroup(),
83                'additionalDepartmentRequestList' => $additionalDepartmentRequestList->toSortedByGroup(),
84                'menuActive' => 'workstation',
85                'statisticEnabled' => $statisticEnabled,
86                'nextProcessId' => $nextProcessId
87            )
88        );
89    }
90
91    protected function readRequeststatistic(int $scopeId): Requeststatistic
92    {
93        $entity = \App::$http
94            ->readGetResult('/scope/' . $scopeId . '/request/department/')
95            ->getEntity();
96
97        if ($entity instanceof Requeststatistic) {
98            return $entity;
99        }
100
101        throw new \RuntimeException(
102            'Invalid API response for /scope/' . $scopeId . '/request/department/'
103        );
104    }
105
106    protected function getFinishedResponse(
107        Workstation $workstation,
108        ?Process $process = null,
109        ?int $nextProcessId = null
110    ): \BO\Slim\Response {
111        $process ??= clone $workstation->getProcess();
112        $process['status'] = ('pending' != $process['status']) ? 'finished' : $process['status'];
113        \App::$http->readPostResult('/process/status/finished/', new Process($process))->getEntity();
114        $redirectParams = [];
115        if ($nextProcessId) {
116            $redirectParams['calledprocess'] = $nextProcessId;
117        }
118        return Render::redirect(
119            $workstation->getVariantName(),
120            array(),
121            $redirectParams
122        );
123    }
124
125
126    /**
127     * @return void
128     */
129    protected function testProcess(Workstation $workstation)
130    {
131        if (! $workstation->getProcess()->hasId()) {
132            throw new WorkstationMissingAssignedProcess();
133        }
134    }
135}