Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.46% |
115 / 130 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| QueueTable | |
88.46% |
115 / 130 |
|
57.14% |
4 / 7 |
18.50 | |
0.00% |
0 / 1 |
| readResponse | |
100.00% |
80 / 80 |
|
100.00% |
1 / 1 |
1 | |||
| resolveSelectedDateTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| readProcessListForDateIfAllowed | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| readChangedProcess | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
2.86 | |||
| readWaitingClientsEffective | |
15.38% |
2 / 13 |
|
0.00% |
0 / 1 |
8.45 | |||
| readCalledQueueList | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
5 | |||
| getQueueListByPermission | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Zmsadmin |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsadmin; |
| 9 | |
| 10 | use BO\Zmsclient\WorkstationRequests; |
| 11 | use BO\Zmsentities\Collection\ProcessList; |
| 12 | use BO\Zmsentities\Collection\QueueList; |
| 13 | use BO\Zmsentities\Useraccount; |
| 14 | |
| 15 | class QueueTable extends BaseController |
| 16 | { |
| 17 | protected $processStatusList = ['preconfirmed', 'confirmed', 'queued', 'reserved', 'deleted']; |
| 18 | |
| 19 | private const QUEUE_VIEW_PERMISSIONS = [ |
| 20 | 'waitingqueue', |
| 21 | 'parkedqueue', |
| 22 | 'missedqueue', |
| 23 | 'finishedqueue', |
| 24 | ]; |
| 25 | |
| 26 | /** |
| 27 | * @SuppressWarnings(Param) |
| 28 | * @return \Psr\Http\Message\ResponseInterface |
| 29 | */ |
| 30 | #[\Override] |
| 31 | public function readResponse( |
| 32 | \Psr\Http\Message\RequestInterface $request, |
| 33 | \Psr\Http\Message\ResponseInterface $response, |
| 34 | array $args |
| 35 | ): \Psr\Http\Message\ResponseInterface { |
| 36 | $validator = $request->getAttribute('validator'); |
| 37 | $success = $validator->getParameter('success')->isString()->getValue(); |
| 38 | $withCalledList = $validator->getParameter('withCalled')->isBool()->getValue(); |
| 39 | $includeWaitingClientsEffective = $validator |
| 40 | ->getParameter('includeWaitingClientsEffective') |
| 41 | ->isBool() |
| 42 | ->getValue(); |
| 43 | $selectedDateTime = $this->resolveSelectedDateTime( |
| 44 | $validator->getParameter('selecteddate')->isString()->getValue() |
| 45 | ); |
| 46 | $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue(); |
| 47 | |
| 48 | $workstation = \App::$http->readGetResult('/workstation/', [ |
| 49 | 'resolveReferences' => 1, |
| 50 | 'gql' => Helper\GraphDefaults::getWorkstation() |
| 51 | ])->getEntity(); |
| 52 | $workstationRequest = new WorkstationRequests(\App::$http, $workstation); |
| 53 | $department = $workstationRequest->readDepartment(); |
| 54 | $useraccount = $workstation->getUseraccount(); |
| 55 | |
| 56 | $processList = $this->readProcessListForDateIfAllowed( |
| 57 | $workstationRequest, |
| 58 | $useraccount, |
| 59 | $selectedDateTime |
| 60 | ); |
| 61 | $changedProcess = $this->readChangedProcess($selectedProcessId); |
| 62 | $queueList = $processList->toQueueList(\App::$now); |
| 63 | $waitingClientsEffective = $this->readWaitingClientsEffective( |
| 64 | $includeWaitingClientsEffective, |
| 65 | $queueList, |
| 66 | $workstationRequest, |
| 67 | $useraccount, |
| 68 | $selectedDateTime |
| 69 | ); |
| 70 | |
| 71 | $queueListVisible = $this->getQueueListByPermission( |
| 72 | $queueList, |
| 73 | $useraccount, |
| 74 | 'waitingqueue', |
| 75 | $this->processStatusList |
| 76 | ); |
| 77 | $queueListMissed = $this->getQueueListByPermission( |
| 78 | $queueList, |
| 79 | $useraccount, |
| 80 | 'missedqueue', |
| 81 | ['missed'] |
| 82 | ); |
| 83 | $queueListParked = $this->getQueueListByPermission( |
| 84 | $queueList, |
| 85 | $useraccount, |
| 86 | 'parkedqueue', |
| 87 | ['parked'] |
| 88 | ); |
| 89 | $queueListFinished = $this->getQueueListByPermission( |
| 90 | $queueList, |
| 91 | $useraccount, |
| 92 | 'finishedqueue', |
| 93 | ['finished'] |
| 94 | ); |
| 95 | $queueListCalled = $this->readCalledQueueList($withCalledList, $useraccount); |
| 96 | |
| 97 | return \BO\Slim\Render::withHtml( |
| 98 | $response, |
| 99 | 'block/queue/table.twig', |
| 100 | array( |
| 101 | 'workstation' => $workstation->getArrayCopy(), |
| 102 | 'department' => $department, |
| 103 | 'source' => $workstation->getVariantName(), |
| 104 | 'selectedDate' => $selectedDateTime->format('Y-m-d'), |
| 105 | 'cluster' => $workstationRequest->readCluster(), |
| 106 | 'clusterEnabled' => $workstation->isClusterEnabled(), |
| 107 | 'processList' => $queueListVisible->toProcessList(), |
| 108 | 'waitingClientsEffective' => $waitingClientsEffective, |
| 109 | 'processListMissed' => $queueListMissed->toProcessList(), |
| 110 | 'processListParked' => $queueListParked->toProcessList(), |
| 111 | 'processListFinished' => $queueListFinished->toProcessList(), |
| 112 | 'showCalledList' => $withCalledList, |
| 113 | 'queueListCalled' => $queueListCalled, |
| 114 | 'changedProcess' => $changedProcess, |
| 115 | 'success' => $success, |
| 116 | 'debug' => \App::DEBUG, |
| 117 | 'allowClusterWideCall' => \App::$allowClusterWideCall |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | private function resolveSelectedDateTime(?string $selectedDate): \DateTimeImmutable |
| 123 | { |
| 124 | $selectedDateTime = $selectedDate ? new \DateTimeImmutable($selectedDate) : \App::$now; |
| 125 | return ($selectedDateTime < \App::$now) ? \App::$now : $selectedDateTime; |
| 126 | } |
| 127 | |
| 128 | private function readProcessListForDateIfAllowed( |
| 129 | WorkstationRequests $workstationRequest, |
| 130 | Useraccount $useraccount, |
| 131 | \DateTimeInterface $selectedDateTime |
| 132 | ): ProcessList { |
| 133 | if (! $useraccount->hasAnyPermission(self::QUEUE_VIEW_PERMISSIONS)) { |
| 134 | return new ProcessList(); |
| 135 | } |
| 136 | |
| 137 | return $workstationRequest->readProcessListByDate( |
| 138 | $selectedDateTime, |
| 139 | Helper\GraphDefaults::getProcess() |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | private function readChangedProcess(?int $selectedProcessId) |
| 144 | { |
| 145 | if (! $selectedProcessId) { |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | return \App::$http->readGetResult('/process/' . $selectedProcessId . '/', [ |
| 150 | 'gql' => Helper\GraphDefaults::getProcess() |
| 151 | ])->getEntity(); |
| 152 | } |
| 153 | |
| 154 | private function readWaitingClientsEffective( |
| 155 | ?bool $includeWaitingClientsEffective, |
| 156 | QueueList $queueList, |
| 157 | WorkstationRequests $workstationRequest, |
| 158 | Useraccount $useraccount, |
| 159 | \DateTimeInterface $selectedDateTime |
| 160 | ): ?int { |
| 161 | if (! $includeWaitingClientsEffective) { |
| 162 | return null; |
| 163 | } |
| 164 | |
| 165 | $waitingClientsQueueList = $queueList; |
| 166 | if ($selectedDateTime->format('Y-m-d') !== \App::$now->format('Y-m-d')) { |
| 167 | $waitingClientsQueueList = $this->readProcessListForDateIfAllowed( |
| 168 | $workstationRequest, |
| 169 | $useraccount, |
| 170 | \App::$now |
| 171 | )->toQueueList(\App::$now); |
| 172 | } |
| 173 | |
| 174 | return $waitingClientsQueueList |
| 175 | ->withStatus($this->processStatusList) |
| 176 | ->getCountWithWaitingTime(\App::$now) |
| 177 | ->count(); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @return QueueList|array |
| 182 | */ |
| 183 | private function readCalledQueueList(?bool $withCalledList, Useraccount $useraccount) |
| 184 | { |
| 185 | if (! $withCalledList || ! $useraccount->hasPermissions(['openqueue'])) { |
| 186 | return []; |
| 187 | } |
| 188 | |
| 189 | $queueListCalled = \App::$http |
| 190 | ->readGetResult( |
| 191 | '/useraccount/queue/', |
| 192 | [ |
| 193 | 'resolveReferences' => 2, |
| 194 | 'status' => 'called,processing', |
| 195 | ] |
| 196 | ) |
| 197 | ->getCollection() ?? []; |
| 198 | |
| 199 | if (! ($queueListCalled instanceof QueueList)) { |
| 200 | return []; |
| 201 | } |
| 202 | |
| 203 | $queueListCalled->uasort(function ($queueA, $queueB) { |
| 204 | $statusOrder = ['called' => 0, 'processing' => 1]; |
| 205 | |
| 206 | $statusValueA = $statusOrder[$queueA->status] ?? PHP_INT_MAX; |
| 207 | $statusValueB = $statusOrder[$queueB->status] ?? PHP_INT_MAX; |
| 208 | |
| 209 | $cmp = $statusValueA <=> $statusValueB; |
| 210 | return $cmp !== 0 ? $cmp : $queueB->callTime <=> $queueA->callTime; |
| 211 | }); |
| 212 | |
| 213 | return $queueListCalled; |
| 214 | } |
| 215 | |
| 216 | private function getQueueListByPermission( |
| 217 | QueueList $queueList, |
| 218 | Useraccount $useraccount, |
| 219 | string $permission, |
| 220 | array $statuses |
| 221 | ): QueueList { |
| 222 | if (! $useraccount->hasPermissions([$permission])) { |
| 223 | return new QueueList(); |
| 224 | } |
| 225 | |
| 226 | return $queueList->withStatus($statuses); |
| 227 | } |
| 228 | } |