Lines 87.50% 91 / 104
Methods 50.00% 3 / 6
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 timeToUnix 28.57% 2 / 7 0.00% 0 / 1 9.83
 readResponse 94.64% 53 / 56 0.00% 0 / 1 13.03
 [BO\Zmsadmin\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
 [BO\Zmsadmin\BaseController] getSchemaConstraintList 100.00% 8 / 8 100.00% 1 / 1 4
 [BO\Zmsadmin\BaseController] transformValidationErrors 61.53% 8 / 13 0.00% 0 / 1 15.69
 [BO\Zmsadmin\BaseController] handleEntityWrite 100.00% 17 / 17 100.00% 1 / 1 5
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}

Inherited from BO\Zmsadmin\BaseController

21    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
22    {
23        $request = $this->initRequest($request);
24        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
25        return $this->readResponse($request, $noCacheResponse, $args);
26    }
41    public function getSchemaConstraintList($schema): array
42    {
43        $list = [];
44        $locale = \App::$language->getLocale();
45        foreach ($schema->properties as $key => $property) {
46            if (isset($property['x-locale'])) {
47                $constraints = $property['x-locale'][$locale];
48                if ($constraints) {
49                    $list[$key]['description'] = $constraints['messages'];
50                }
51            }
52        }
53        return $list;
54    }
65    protected function transformValidationErrors($errorData)
66    {
67        if (!is_array($errorData) && !($errorData instanceof \Traversable)) {
68            return [];
69        }
70        $transformed = [];
71        foreach ($errorData as $pointer => $item) {
72            // Extract field name from JSON pointer (e.g., "/id" -> "id", "/contact/email" -> "contact/email")
73            // If the key doesn't start with "/", it's already a field name, so use it as-is
74            $fieldName = (strpos($pointer, '/') === 0) ? ltrim($pointer, '/') : $pointer;
75            // Handle root level errors
76            if ($fieldName === '' || $fieldName === null) {
77                $fieldName = '_root';
78            }
79            // Ensure the item structure is correct (has 'messages' array)
80            if (is_array($item) && isset($item['messages'])) {
81                $transformed[$fieldName] = $item;
82            } elseif (is_array($item)) {
83                // If item is an array but doesn't have 'messages', wrap it
84                $transformed[$fieldName] = $item;
85            } else {
86                $transformed[$fieldName] = $item;
87            }
88        }
89        return $transformed;
90    }
99    protected function handleEntityWrite(callable $httpCall)
100    {
101        try {
102            return $httpCall();
103        } catch (\BO\Zmsclient\Exception $exception) {
104            if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) {
105                return [
106                    'template' => 'exception/bo/zmsentities/exception/schemavalidation.twig',
107                    'include' => true,
108                    'data' => $this->transformValidationErrors($exception->data)
109                ];
110            }
111
112            $template = TwigExceptionHandler::getExceptionTemplate($exception);
113            if (
114                '' != $exception->template
115                && \App::$slim->getContainer()->get('view')->getLoader()->exists($template)
116            ) {
117                return [
118                    'template' => $template,
119                    'include' => true,
120                    'data' => $this->transformValidationErrors($exception->data)
121                ];
122            }
123
124            throw $exception;
125        }
126    }