Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.57% covered (warning)
86.57%
187 / 216
81.48% covered (warning)
81.48%
22 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueueList
86.57% covered (warning)
86.57%
187 / 216
81.48% covered (warning)
81.48%
22 / 27
108.17
0.00% covered (danger)
0.00%
0 / 1
 setWaitingTimePreferences
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setTransferedProcessList
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getProcessTimeAverage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWorkstationCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 withEstimatedWaitingTime
85.96% covered (warning)
85.96%
49 / 57
0.00% covered (danger)
0.00%
0 / 1
16.71
 getSortPriority
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
 withWaitingTime
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 withSortedArrival
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 withSortedWaitingTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 withAppointment
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 withOutAppointment
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getEstimatedWaitingTime
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 withFakeWaitingnumber
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 getFakeOrLastWaitingnumber
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getQueueByNumber
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getNextProcess
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
7
 getQueuePositionByNumber
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getCountWithWaitingTime
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
7.18
 withStatus
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 withoutStatus
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 withShortNameDestinationHint
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 toProcessList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 withoutDublicates
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getWaitingNumberList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getWaitingNumberListCsv
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withSelectedProcessFirst
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 sortByCallTime
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace BO\Zmsentities\Collection;
4
5/**
6 * @SuppressWarnings(Complexity)
7 * @SuppressWarnings(PublicMethod)
8 *
9 * @extends Base<\BO\Zmsentities\Queue>
10 */
11class QueueList extends Base implements \BO\Zmsentities\Helper\NoSanitize
12{
13    public const ENTITY_CLASS = '\BO\Zmsentities\Queue';
14
15    public const FAKE_WAITINGNUMBER = -1;
16
17    public const STATUS_IGNORE = ['called', 'processing', 'missed', 'parked'];
18
19    public const STATUS_APPEND = ['missed', 'parked'];
20
21    public const STATUS_CALLED = ['called', 'processing'];
22
23    public const STATUS_FAKE = ['fake'];
24
25    public const DEFAULT_PRIORITY_WITHOUT_APPOINTMENT = 3;
26
27    public const DEFAULT_PRIORITY_WITH_APPOINTMENT = 2;
28
29    protected $processTimeAverage;
30
31    protected $workstationCount;
32
33    protected $fromProcessList = false;
34
35    public function setWaitingTimePreferences($processTimeAverage, $workstationCount)
36    {
37        if ($processTimeAverage <= 0) {
38            throw new \Exception("QueueList::withEstimatedWaitingTime() requires processTimeAverage");
39        }
40        $this->processTimeAverage = $processTimeAverage;
41        $this->workstationCount = $workstationCount;
42        return $this;
43    }
44
45    public function setTransferedProcessList($bool = true)
46    {
47        $this->fromProcessList = $bool;
48        return $this;
49    }
50
51    public function getProcessTimeAverage()
52    {
53        return $this->processTimeAverage;
54    }
55
56    public function getWorkstationCount()
57    {
58        return $this->workstationCount ? $this->workstationCount : 1;
59    }
60
61    public function withEstimatedWaitingTime(
62        $processTimeAverage,
63        $workstationCount,
64        \DateTimeInterface $dateTime,
65        $createFake = true
66    ) {
67        $this->setWaitingTimePreferences($processTimeAverage, $workstationCount);
68        $queueFull = $this->withWaitingTime($dateTime);
69        $queueWithWaitingTime = $queueFull->withStatus(self::STATUS_CALLED);
70        $queueAppend = $queueFull->withStatus(self::STATUS_APPEND);
71        $queueFull = $queueFull->withoutStatus(self::STATUS_IGNORE);
72        if ($createFake) {
73            $queueFull = $queueFull->withFakeWaitingnumber($dateTime);
74        }
75        $listWithAppointment = $queueFull->withAppointment()->withSortedArrival()->getArrayCopy();
76        $listNoAppointment = $queueFull->withOutAppointment()->withSortedArrival()->getArrayCopy();
77        $nextWithAppointment = array_shift($listWithAppointment);
78        $nextNoAppointment = array_shift($listNoAppointment);
79        $currentTime = $dateTime->getTimestamp() + 120;
80        $optimisticTime = $dateTime->getTimestamp();
81        $pessimisticTime = $dateTime->getTimestamp();
82
83        $waitingTime = 0;
84        $waitingTimeOpt = 0;
85        $timeSlot = ($workstationCount) ? $processTimeAverage * 60 / $workstationCount : $processTimeAverage * 60;
86        $timeSlotOptimistic = $timeSlot * 0.8;
87        $timeSlotPessimistic = $timeSlot * 1.0;
88        $workstationSkip = ceil($workstationCount * 1.0);
89        $pessimisticTime += $timeSlot;
90        $waitingTimePes = (int)floor(($pessimisticTime - $dateTime->getTimestamp()) / 60);
91        while ($nextWithAppointment || $nextNoAppointment) {
92            if ($nextNoAppointment && (int) $nextNoAppointment->priority === 1) {
93                $nextNoAppointment->waitingTimeEstimate = $waitingTimePes;
94                $nextNoAppointment->waitingTimeOptimistic = $waitingTimeOpt;
95                $queueWithWaitingTime->addEntity($nextNoAppointment);
96                $nextNoAppointment = array_shift($listNoAppointment);
97            } elseif (
98                $nextNoAppointment
99                && $nextWithAppointment
100                && (int) $nextNoAppointment->priority === 2
101                && $nextNoAppointment->arrivalTime < $nextWithAppointment->arrivalTime
102            ) {
103                $nextNoAppointment->waitingTimeEstimate = $waitingTimePes;
104                $nextNoAppointment->waitingTimeOptimistic = $waitingTimeOpt;
105                $queueWithWaitingTime->addEntity($nextNoAppointment);
106                $nextNoAppointment = array_shift($listNoAppointment);
107            } elseif ($nextWithAppointment && $currentTime >= $nextWithAppointment->arrivalTime) {
108                $nextWithAppointment->waitingTimeEstimate = $waitingTime + 1;
109                $nextWithAppointment->waitingTimeOptimistic =
110                    floor(($nextWithAppointment->arrivalTime - $dateTime->getTimestamp()) / 60);
111                if ($optimisticTime >= $nextWithAppointment->arrivalTime) {
112                    $nextWithAppointment->waitingTimeOptimistic = $waitingTimeOpt;
113                    $nextWithAppointment->waitingTimeEstimate = $waitingTimePes;
114                }
115                $queueWithWaitingTime->addEntity($nextWithAppointment);
116                $nextWithAppointment = array_shift($listWithAppointment);
117            } elseif ($nextNoAppointment) {
118                $nextNoAppointment->waitingTimeEstimate = $waitingTimePes;
119                $nextNoAppointment->waitingTimeOptimistic = $waitingTimeOpt;
120                $queueWithWaitingTime->addEntity($nextNoAppointment);
121                $nextNoAppointment = array_shift($listNoAppointment);
122            }
123            $optimisticTime += (--$workstationSkip > 0) ? 0 : $timeSlotOptimistic;
124            $pessimisticTime += $timeSlotPessimistic;
125            $currentTime += $timeSlot;
126            $waitingTime = (int)ceil(($currentTime - $dateTime->getTimestamp()) / 60);
127            $waitingTimeOpt = (int)floor(($optimisticTime - $dateTime->getTimestamp()) / 60);
128            $waitingTimePes = (int)floor(($pessimisticTime - $dateTime->getTimestamp()) / 60);
129        }
130        $queueWithWaitingTime->addList($queueAppend);
131        return $queueWithWaitingTime;
132    }
133
134    private function getSortPriority($queue): int
135    {
136        $priority = self::DEFAULT_PRIORITY_WITHOUT_APPOINTMENT;
137        if (empty($queue['priority']) && $queue['withAppointment']) {
138            $priority = self::DEFAULT_PRIORITY_WITH_APPOINTMENT;
139        }
140        if (!empty($queue['priority'])) {
141            $priority = (int) $queue['priority'];
142        }
143
144        return $priority;
145    }
146
147    public function withWaitingTime(\DateTimeInterface $dateTime)
148    {
149        $queueList = $this;
150        $timestamp = $dateTime->getTimestamp();
151        foreach ($queueList as $entity) {
152            if ($timestamp > $entity->arrivalTime) {
153                $entity->waitingTime = floor(($timestamp - $entity->arrivalTime) / 60);
154            }
155        }
156        return $queueList;
157    }
158
159    public function withSortedArrival()
160    {
161        $queueList = $this;
162        $queueList->uasort(function ($first, $second) {
163            $firstPriority = $this->getSortPriority($first);
164            $secondPriority = $this->getSortPriority($second);
165
166            $firstSort = sprintf("%01d%011d%011d", $firstPriority, $first['arrivalTime'], $first['number']);
167            $secondSort = sprintf("%01d%011d%011d", $secondPriority, $second['arrivalTime'], $second['number']);
168
169            return strcmp($firstSort, $secondSort);
170        });
171        return $queueList;
172    }
173
174    public function withSortedWaitingTime()
175    {
176        $queueList = $this;
177        return $queueList->sortByCustomKey('waitingTimeEstimate');
178    }
179
180    public function withAppointment()
181    {
182        $queueList = new self();
183        foreach ($this as $entity) {
184            if ($entity->withAppointment) {
185                $queueList->addEntity($entity);
186            }
187        }
188        return $queueList;
189    }
190
191    public function withOutAppointment()
192    {
193        $queueList = new self();
194        foreach ($this as $entity) {
195            if (! $entity->withAppointment) {
196                $queueList->addEntity($entity);
197            }
198        }
199        return $queueList;
200    }
201
202    public function getEstimatedWaitingTime($processTimeAverage, $workstationCount, \DateTimeInterface $dateTime)
203    {
204        $queueList = $this->withFakeWaitingnumber($dateTime);
205        $queueList = $queueList
206          ->withEstimatedWaitingTime($processTimeAverage, $workstationCount, $dateTime);
207        $newEntity = $queueList->getFakeOrLastWaitingnumber();
208        $dataOfFackedEntity = array(
209            'amountBefore' => $queueList->getQueuePositionByNumber($newEntity->number),
210            'waitingTimeEstimate' => $newEntity->waitingTimeEstimate
211        );
212        return $dataOfFackedEntity;
213    }
214
215    public function withFakeWaitingnumber(\DateTimeInterface $dateTime)
216    {
217        $queueList = $this;
218        $process = new \BO\Zmsentities\Process(['status' => 'deleted']);
219        $entity = (new \BO\Zmsentities\Queue())->setProcess($process);
220        $entity->number = self::FAKE_WAITINGNUMBER;
221        $entity->status = 'fake';
222        $entity->withAppointment = false;
223        $entity->destination = (string)$this->getProcessTimeAverage();
224        $entity->destinationHint = (string)$this->getWorkstationCount();
225        $entity->arrivalTime = $dateTime->getTimestamp();
226        $queueList->addEntity($entity);
227        return $queueList;
228    }
229
230    public function getFakeOrLastWaitingnumber()
231    {
232        $entity = $this->getQueueByNumber(self::FAKE_WAITINGNUMBER);
233        if (!$entity) {
234            $entity = $this->getLast();
235        }
236
237        return $entity;
238    }
239
240    public function getQueueByNumber($number)
241    {
242        foreach ($this as $entity) {
243            if ($entity->number == $number) {
244                return $entity;
245            }
246        }
247        return null;
248    }
249
250    public function getNextProcess(\DateTimeInterface $dateTime, $exclude = null)
251    {
252        if (is_array($exclude)) {
253            $excludeNumbers = $exclude;
254        } else {
255            $excludeNumbers = explode(',', $exclude === null ? '' : (string) $exclude);
256        }
257        $queueList = clone $this;
258        // sort by waiting time to get realistic next process
259        $queueList = $queueList
260            ->withStatus(['confirmed', 'queued'])
261            ->withEstimatedWaitingTime(10, 1, $dateTime, false)
262            ->getArrayCopy()
263            ;
264        $next = array_shift($queueList);
265        $currentTime = $dateTime->getTimestamp();
266        while ($next) {
267            if (
268                ! in_array($next->number, $excludeNumbers) &&
269                (0 == $next->lastCallTime || ($next->lastCallTime + (5 * 60)) <= $currentTime)
270            ) {
271                return $next->getProcess();
272            }
273            $next = array_shift($queueList);
274        }
275        return null;
276    }
277
278    public function getQueuePositionByNumber($number)
279    {
280        $list = array_values($this->getArrayCopy());
281        foreach ($list as $key => $entity) {
282            if ($entity->number == $number) {
283                return $key;
284            }
285        }
286        return null;
287    }
288
289    public function getCountWithWaitingTime(?\DateTimeInterface $dateTime = null)
290    {
291        $queueList = new self();
292        $timestamp = $dateTime ? $dateTime->getTimestamp() : null;
293
294        foreach ($this as $entity) {
295            if (! $entity->withAppointment) {
296                $queueList->addEntity($entity);
297                continue;
298            }
299
300            if ($timestamp !== null) {
301                if ($entity->arrivalTime <= $timestamp) {
302                    $queueList->addEntity($entity);
303                }
304
305                continue;
306            }
307
308            if ($entity->waitingTime) {
309                $queueList->addEntity($entity);
310            }
311        }
312        return $queueList;
313    }
314
315    /**
316     * @param array $statusList of possible strings in process.status
317     *
318     */
319    public function withStatus(array $statusList)
320    {
321        $queueList = new self();
322        foreach ($this as $entity) {
323            if ($entity->toProperty()->status->isAvailable() && in_array($entity->status, $statusList)) {
324                $queueList->addEntity($entity);
325            }
326        }
327        return $queueList;
328    }
329
330    /**
331     * @param array $statusList of excepted strings in process.status
332     *
333     */
334    public function withoutStatus(array $statusList)
335    {
336        $queueList = new self();
337        foreach ($this as $entity) {
338            if ($entity->toProperty()->status->isAvailable() && ! in_array($entity->status, $statusList)) {
339                $queueList->addEntity($entity);
340            }
341        }
342        return $queueList;
343    }
344
345    public function withShortNameDestinationHint(\BO\Zmsentities\Cluster $cluster, \BO\Zmsentities\Scope $scope)
346    {
347        $queueList = $this;
348        $list = new self();
349        foreach ($queueList as $entity) {
350            if ($cluster->shortNameEnabled && $scope->shortName) {
351                $entity->destinationHint = $scope->shortName;
352            }
353            $list->addEntity($entity);
354        }
355        return $list;
356    }
357
358    public function toProcessList()
359    {
360        $processList = new ProcessList();
361        foreach ($this as $queue) {
362            $process = $queue->getProcess();
363            if ($process) {
364                $processList->addEntity($process);
365            }
366        }
367        return $processList;
368    }
369
370    public function withoutDublicates()
371    {
372        $list = new self();
373        $exists = [];
374        foreach ($this as $entity) {
375            $key = "$entity->number-$entity->arrivalTime-$entity->withAppointment";
376            if (!isset($exists[$key])) {
377                $list[] = $entity;
378                $exists[$key] = true;
379            }
380        }
381        return $list; // Cloning with this function
382    }
383
384    public function getWaitingNumberList()
385    {
386        $list = [];
387        foreach ($this as $entity) {
388            $list[] = $entity->number;
389        }
390        return $list;
391    }
392
393    public function getWaitingNumberListCsv()
394    {
395        return implode(',', $this->getWaitingNumberList());
396    }
397
398    public function withSelectedProcessFirst(\BO\Zmsentities\Process $process)
399    {
400        $queueList = $this;
401        $list = new self();
402        $list->addEntity($process->queue);
403        foreach ($queueList as $entity) {
404            if ($entity->number != $process->queue->number) {
405                $list->addEntity($entity);
406            }
407        }
408        return $list;
409    }
410
411    public function sortByCallTime(string $order)
412    {
413        $queueListArray = [];
414        foreach ($this as $entity) {
415            $queueListArray[] = $entity;
416        }
417        usort($queueListArray, function ($a, $b) use ($order) {
418            if ($order === 'ascending') {
419                return $a->callTime <=> $b->callTime;
420            } elseif ($order === 'descending') {
421                return $b->callTime <=> $a->callTime;
422            }
423            throw new \InvalidArgumentException("Invalid sort order: $order. Use 'ascending' or 'descending'.");
424        });
425        return new self($queueListArray);
426    }
427}