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