Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.63% |
43 / 54 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ProcessListByClusterAndDate | |
79.63% |
43 / 54 |
|
0.00% |
0 / 1 |
10.85 | |
0.00% |
0 / 1 |
| readResponse | |
79.63% |
43 / 54 |
|
0.00% |
0 / 1 |
10.85 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package ZMS API |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsapi; |
| 9 | |
| 10 | use BO\Slim\Render; |
| 11 | use BO\Mellon\Validator; |
| 12 | use BO\Zmsdb\Cluster as Query; |
| 13 | use BO\Zmsdb\ProcessStatusArchived; |
| 14 | use BO\Zmsentities\Collection\ProcessList as ProcessListCollection; |
| 15 | use BO\Zmsentities\Collection\QueueList; |
| 16 | |
| 17 | class ProcessListByClusterAndDate extends BaseController |
| 18 | { |
| 19 | /** |
| 20 | * @SuppressWarnings(Param) |
| 21 | * @return \Psr\Http\Message\ResponseInterface |
| 22 | */ |
| 23 | #[\Override] |
| 24 | public function readResponse( |
| 25 | \Psr\Http\Message\RequestInterface $request, |
| 26 | \Psr\Http\Message\ResponseInterface $response, |
| 27 | array $args |
| 28 | ) { |
| 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 | (new Helper\User($request, 2))->checkPermissions( |
| 52 | 'appointment', |
| 53 | new \BO\Zmsentities\Useraccount\EntityAccess($cluster) |
| 54 | ); |
| 55 | |
| 56 | $shortNames = []; |
| 57 | foreach ($cluster->scopes as $scope) { |
| 58 | $shortNames[$scope->id] = $scope->shortName; |
| 59 | } |
| 60 | |
| 61 | $queueList = new QueueList(); |
| 62 | foreach ($dates as $date) { |
| 63 | $dateQueueList = $query->readQueueList( |
| 64 | $cluster->id, |
| 65 | $date, |
| 66 | 2, |
| 67 | ['availability', 'scope', 'scopeprovider'] |
| 68 | ); |
| 69 | |
| 70 | if (! $dateQueueList) { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | /** @var QueueList $dateQueueList */ |
| 75 | $queueList->addList($dateQueueList); |
| 76 | } |
| 77 | |
| 78 | $allArchivedProcesses = new ProcessListCollection(); |
| 79 | $scopeIds = $cluster->scopes->getIds(); |
| 80 | |
| 81 | $archivedProcesses = |
| 82 | (new ProcessStatusArchived())->readListByScopesAndDates($scopeIds, $dates); |
| 83 | |
| 84 | if ($archivedProcesses instanceof ProcessListCollection) { |
| 85 | $allArchivedProcesses = $archivedProcesses; |
| 86 | } else { |
| 87 | \App::$log->error('Expected ProcessListCollection, received different type', [ |
| 88 | 'received_type' => gettype($archivedProcesses), |
| 89 | 'cluster_id' => $args['id'], |
| 90 | ]); |
| 91 | } |
| 92 | |
| 93 | $queueList = $queueList->toProcessList()->sortByEstimatedWaitingTime()->withResolveLevel(2); |
| 94 | foreach ($queueList as $queue) { |
| 95 | if (!$queue->scope->id) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $queue->scope->shortName = $shortNames[$queue->scope->id]; |
| 100 | } |
| 101 | |
| 102 | $message = Response\Message::create($request); |
| 103 | $message->data = $queueList; |
| 104 | |
| 105 | // Add all archived processes to the response data |
| 106 | $message->data->addData($allArchivedProcesses); |
| 107 | |
| 108 | $response = Render::withLastModified($response, time(), '0'); |
| 109 | $response = Render::withJson($response, $message, 200); |
| 110 | return $response; |
| 111 | } |
| 112 | } |