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