Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.11% covered (warning)
71.11%
32 / 45
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CalldisplayQueue
71.11% covered (warning)
71.11%
32 / 45
33.33% covered (danger)
33.33%
2 / 6
23.97
0.00% covered (danger)
0.00%
0 / 1
 readResponse
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
2.00
 testScopeAndCluster
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
7
 readCalculatedQueueListFromScope
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 readFullQueueList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 readQueueListFromScopeAndStatus
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 readQueueListByStatus
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
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;
12
13/**
14 * @SuppressWarnings(Coupling)
15 * @return \Psr\Http\Message\ResponseInterface
16 */
17class CalldisplayQueue 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        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(0)->getValue();
30        $statusList = Validator::param('statusList')->isArray()->setDefault([])->getValue();
31
32        $input = Validator::input()->isJson()->assertValid()->getValue();
33        $calldisplay = (new \BO\Zmsentities\Calldisplay($input))->withOutClusterDuplicates();
34        $this->testScopeAndCluster($calldisplay, $resolveReferences);
35
36        //read full list if no statusList exists
37        $queueList = (count($statusList)) ?
38            $this->readQueueListByStatus($calldisplay, $statusList, $resolveReferences) :
39            $this->readFullQueueList($calldisplay, $resolveReferences);
40
41
42
43        $message = Response\Message::create($request);
44        $message->data = $queueList->withoutDublicates();
45
46        $response = Render::withLastModified($response, time(), '0');
47        $response = Render::withJson($response, $message, 200);
48        return $response;
49    }
50
51    protected $scopeCache = [];
52
53    protected function testScopeAndCluster($calldisplay, $resolveReferences)
54    {
55        if (! $calldisplay->hasScopeList() && ! $calldisplay->hasClusterList()) {
56            throw new Exception\Calldisplay\ScopeAndClusterNotFound();
57        }
58        foreach ($calldisplay->getClusterList() as $cluster) {
59            $cluster = (new \BO\Zmsdb\Cluster())->readEntity($cluster->getId());
60            if (! $cluster) {
61                throw new Exception\Cluster\ClusterNotFound();
62            }
63        }
64        foreach ($calldisplay->getScopeList() as $scope) {
65            $scope = (new \BO\Zmsdb\Scope())->readWithWorkstationCount($scope->getId(), \App::$now, $resolveReferences);
66            if (! $scope) {
67                throw new Exception\Scope\ScopeNotFound();
68            }
69            $this->scopeCache[$scope->getId()] = $scope;
70        }
71    }
72
73    protected function readCalculatedQueueListFromScope($scope, $resolveReferences)
74    {
75        $query = new \BO\Zmsdb\Scope();
76        $scope = (isset($this->scopeCache[$scope->id])) ?
77            $this->scopeCache[$scope->id] :
78            $query->readWithWorkstationCount($scope->id, \App::$now, $resolveReferences);
79
80        return $query
81            ->readQueueListWithWaitingTime($scope, \App::$now, $resolveReferences);
82    }
83
84    // full queueList for calculation optimistic and estimated waiting Time and number of waiting clients
85    protected function readFullQueueList($calldisplay, $resolveReferences)
86    {
87        $queueList = new \BO\Zmsentities\Collection\QueueList();
88        foreach ($calldisplay->getFullScopeList() as $scope) {
89            $queueList->addList($this->readCalculatedQueueListFromScope($scope, $resolveReferences));
90        }
91        return $queueList;
92    }
93
94    protected function readQueueListFromScopeAndStatus($scope, $status, $resolveReferences)
95    {
96        $query = new \BO\Zmsdb\Process();
97        return $query
98            ->readProcessListByScopeAndStatus($scope->getId(), $status, $resolveReferences)
99            ->withinExactDate(\App::$now)
100            ->toQueueList(\App::$now);
101    }
102
103    // short queueList only with status called and processing
104    protected function readQueueListByStatus($calldisplay, $statusList, $resolveReferences)
105    {
106        $queueList = new \BO\Zmsentities\Collection\QueueList();
107        foreach ($calldisplay->getFullScopeList() as $scope) {
108            foreach ($statusList as $status) {
109                $queueList
110                    ->addList($this->readQueueListFromScopeAndStatus($scope, $status, $resolveReferences));
111            }
112        }
113        return $queueList;
114    }
115}