Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.38% covered (warning)
84.38%
54 / 64
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkstationProcessNext
84.38% covered (warning)
84.38%
54 / 64
0.00% covered (danger)
0.00%
0 / 2
20.38
0.00% covered (danger)
0.00%
0 / 1
 timeToUnix
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
9.83
 readResponse
91.23% covered (success)
91.23%
52 / 57
0.00% covered (danger)
0.00%
0 / 1
15.15
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 BO\Zmsentities\Collection\ProcessList;
12
13class WorkstationProcessNext extends BaseController
14{
15    /**
16     * @SuppressWarnings(Param)
17     * @return int|null
18     */
19    public function timeToUnix($timeValue): ?int
20    {
21        if ($timeValue === null) {
22            return null;
23        }
24        $timeString = trim((string) $timeValue);
25        if ($timeString === '') {
26            return null;
27        }
28        $unixTimestamp = strtotime($timeString);
29
30        return $unixTimestamp !== false ? $unixTimestamp : null;
31    }
32
33    #[\Override]
34    public function readResponse(
35        \Psr\Http\Message\RequestInterface $request,
36        \Psr\Http\Message\ResponseInterface $response,
37        array $args
38    ): \Psr\Http\Message\ResponseInterface {
39        $workstation = \App::$http->readGetResult('/workstation/', [
40            'resolveReferences' => 1,
41            'gql' => Helper\GraphDefaults::getWorkstation()
42        ])->getEntity();
43        $validator = $request->getAttribute('validator');
44        $excludedIds = $validator->getParameter('exclude')->isString()->getValue();
45        $excludedIds = ($excludedIds) ? $excludedIds : '';
46
47        $selectedDateTime = \App::$now;
48        $selectedDateTime = ($selectedDateTime < \App::$now) ? \App::$now : $selectedDateTime;
49
50        $workstationRequest = new \BO\Zmsclient\WorkstationRequests(\App::$http, $workstation);
51
52        $processList = $workstationRequest->readProcessListByDate(
53            $selectedDateTime,
54            Helper\GraphDefaults::getProcess()
55        );
56
57        $filteredProcessList = new ProcessList();
58
59        foreach ($processList as $process) {
60            if ($process->status === "queued" || $process->status === "confirmed") {
61                $timeoutTimeUnix = $this->timeToUnix($process->timeoutTime ?? null);
62                $currentTimeUnix = time();
63
64                if (!isset($process->timeoutTime)) {
65                    $filteredProcessList->addEntity(clone $process);
66                } elseif ($timeoutTimeUnix !== null && !($process->queue->callCount > 0 && ($currentTimeUnix - $timeoutTimeUnix) < 300)) {
67                    $filteredProcessList->addEntity(clone $process);
68                } else {
69                    if (!empty($excludedIds)) {
70                        $excludedIds .= ",";
71                    }
72                    $excludedIds .= $process->queue->number;
73                }
74            }
75        }
76
77        $process = (new Helper\ClusterHelper($workstation))->getNextProcess($excludedIds);
78
79        if (!$process || ! $process->hasId() || $process->getFirstAppointment()->date > \App::$now->getTimestamp()) {
80            return Render::withHtml(
81                $response,
82                'block/process/next.twig',
83                array(
84                    'workstation' => $workstation,
85                    'processNotFoundInQueue' => 1,
86                    'exclude' => ''
87                )
88            );
89        }
90        if ($process->toProperty()->amendment->get()) {
91            return Render::redirect(
92                'workstationProcessPreCall',
93                array(
94                    'id' => $process->id,
95                    'authkey' => $process->authKey
96                ),
97                array(
98                    'exclude' => $excludedIds
99                )
100            );
101        }
102        return Render::redirect(
103            'workstationProcessCalled',
104            array(
105                'id' => $process->id
106            ),
107            array(
108                'exclude' => $excludedIds
109            )
110        );
111    }
112}