Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.43% covered (warning)
78.43%
40 / 51
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessListByClusterAndDate
78.43% covered (warning)
78.43%
40 / 51
0.00% covered (danger)
0.00%
0 / 1
11.00
0.00% covered (danger)
0.00%
0 / 1
 readResponse
78.43% covered (warning)
78.43%
40 / 51
0.00% covered (danger)
0.00%
0 / 1
11.00
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsapi;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsdb\Cluster as Query;
13use BO\Zmsdb\ProcessStatusArchived;
14use BO\Zmsentities\Collection\ProcessList as ProcessListCollection;
15use BO\Zmsentities\Collection\QueueList;
16
17class ProcessListByClusterAndDate extends BaseController
18{
19    /**
20     * @SuppressWarnings(Param)
21     * @return String
22     */
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ) {
28        (new Helper\User($request))->checkRights('basic');
29        $showWeek = Validator::param('showWeek')->isNumber()->setDefault(0)->getValue();
30        $dateTime = new \BO\Zmsentities\Helper\DateTime($args['date']);
31        $dateTime = $dateTime->modify(\App::$now->format('H:i'));
32        $dates = [$dateTime];
33
34        if ($showWeek) {
35            $dates = [];
36            $startDate = clone $dateTime->modify('Monday this week');
37            $endDate = clone $dateTime->modify('Sunday this week');
38
39            while ($startDate <= $endDate) {
40                $dates[] = $startDate;
41                $startDate = $startDate->modify('+1 day');
42            }
43        }
44
45        $query = new Query();
46        $cluster = $query->readEntity($args['id'], 1);
47        if (! $cluster) {
48            throw new Exception\Cluster\ClusterNotFound();
49        }
50
51        $shortNames = [];
52        foreach ($cluster->scopes as $scope) {
53            $shortNames[$scope->id] = $scope->shortName;
54        }
55
56        $queueList = new QueueList();
57        foreach ($dates as $date) {
58            $dateQueueList = $query->readQueueList(
59                $cluster->id,
60                $date,
61                2,
62                ['availability']
63            );
64
65            if (! $dateQueueList) {
66                continue;
67            }
68
69            /** @var QueueList $dateQueueList */
70            $queueList->addList($dateQueueList);
71        }
72
73        $allArchivedProcesses = new ProcessListCollection();
74        $scopeIds = $cluster->scopes->getIds();
75
76        $archivedProcesses =
77            (new ProcessStatusArchived())->readListByScopesAndDates($scopeIds, $dates);
78
79        if ($archivedProcesses instanceof ProcessListCollection) {
80            $allArchivedProcesses = $archivedProcesses;
81        } else {
82            \App::$log->error('Expected ProcessListCollection, received different type', [
83                'received_type' => gettype($archivedProcesses),
84                'cluster_id' => $args['id'],
85            ]);
86        }
87
88        $queueList = $queueList->toProcessList()->withResolveLevel(2);
89        foreach ($queueList as $queue) {
90            if (!$queue->scope->id) {
91                continue;
92            }
93
94            $queue->scope->shortName = $shortNames[$queue->scope->id];
95        }
96
97        $message = Response\Message::create($request);
98        $message->data = $queueList;
99
100        // Add all archived processes to the response data
101        $message->data->addData($allArchivedProcesses);
102
103        $response = Render::withLastModified($response, time(), '0');
104        $response = Render::withJson($response, $message, 200);
105        return $response;
106    }
107}