Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.07% covered (success)
91.07%
51 / 56
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkstationProcessFinished
91.07% covered (success)
91.07%
51 / 56
50.00% covered (danger)
50.00%
2 / 4
12.10
0.00% covered (danger)
0.00%
0 / 1
 readResponse
94.74% covered (success)
94.74%
36 / 38
0.00% covered (danger)
0.00%
0 / 1
6.01
 readRequeststatistic
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
 getFinishedResponse
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 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\Slim\Render;
11use Psr\Http\Message\RequestInterface;
12use BO\Zmsadmin\Helper\ProcessFinishedHelper;
13use BO\Zmsentities\Exception\WorkstationMissingAssignedProcess;
14use BO\Zmsentities\Process;
15use BO\Zmsentities\Collection\RequestList;
16use BO\Zmsentities\Requeststatistic;
17use BO\Zmsentities\Workstation;
18
19class WorkstationProcessFinished extends BaseController
20{
21    /**
22     * @SuppressWarnings(Param)
23     * @return \Psr\Http\Message\ResponseInterface
24     */
25    #[\Override]
26    public function readResponse(
27        RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ): \Psr\Http\Message\ResponseInterface {
31        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
32        if (!$workstation instanceof Workstation) {
33            throw new WorkstationMissingAssignedProcess();
34        }
35        $this->testProcess($workstation);
36        $input = $request->getParsedBody();
37        $statisticEnabled = $workstation->getScope()->getPreference('queue', 'statisticsEnabled');
38
39        if (! $statisticEnabled) {
40            $workstation->getProcess()['status'] = 'finished';
41            return $this->getFinishedResponse($workstation);
42        }
43
44        $scopeId = $workstation->getProcess()->getCurrentScope()['id']
45            ?? $workstation->getScope()['id'];
46
47        $requestStatistic = $this->readRequeststatistic((int) $scopeId);
48        $scopeRequestList = $requestStatistic->getScopeRequests();
49        $additionalDepartmentRequestList = $requestStatistic->getAdditionalDepartmentRequests();
50
51        $selectableRequestList = (new RequestList())
52            ->addList($scopeRequestList)
53            ->addList($additionalDepartmentRequestList);
54
55        if (is_array($input) && isset($input['process']) && array_key_exists('id', $input['process'])) {
56            $source = $workstation->getScope()->getSource();
57            $process = new ProcessFinishedHelper(
58                clone $workstation->getProcess(),
59                $input,
60                $selectableRequestList,
61                $source
62            );
63            return $this->getFinishedResponse($workstation, $process);
64        }
65
66        return Render::withHtml(
67            $response,
68            'page/workstationProcessFinished.twig',
69            array(
70                'title' => 'Kundendaten',
71                'workstation' => $workstation,
72                'scopeRequestList' => $scopeRequestList->toSortedByGroup(),
73                'additionalDepartmentRequestList' => $additionalDepartmentRequestList->toSortedByGroup(),
74                'menuActive' => 'workstation',
75                'statisticEnabled' => $statisticEnabled
76            )
77        );
78    }
79
80    protected function readRequeststatistic(int $scopeId): Requeststatistic
81    {
82        $entity = \App::$http
83            ->readGetResult('/scope/' . $scopeId . '/request/department/')
84            ->getEntity();
85
86        if ($entity instanceof Requeststatistic) {
87            return $entity;
88        }
89
90        throw new \RuntimeException(
91            'Invalid API response for /scope/' . $scopeId . '/request/department/'
92        );
93    }
94
95    protected function getFinishedResponse(
96        Workstation $workstation,
97        ?Process $process = null
98    ) {
99        $process ??= clone $workstation->getProcess();
100        $process['status'] = ('pending' != $process['status']) ? 'finished' : $process['status'];
101        \App::$http->readPostResult('/process/status/finished/', new Process($process))->getEntity();
102        return Render::redirect(
103            $workstation->getVariantName(),
104            array(),
105            array()
106        );
107    }
108
109
110    protected function testProcess(Workstation $workstation)
111    {
112        if (! $workstation->getProcess()->hasId()) {
113            throw new WorkstationMissingAssignedProcess();
114        }
115    }
116}