Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.78% covered (success)
97.78%
44 / 45
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CalldisplayCollections
97.78% covered (success)
97.78%
44 / 45
83.33% covered (warning)
83.33%
5 / 6
19
0.00% covered (danger)
0.00%
0 / 1
 prepareForGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepareForQueue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepare
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 filterClusters
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 filterScopes
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
 assertAnythingLeft
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
7.14
1<?php
2
3namespace BO\Zmsbackend\Calldisplay\Helper;
4
5use BO\Zmsentities\Calldisplay as Entity;
6use BO\Zmsentities\Collection\ClusterList;
7use BO\Zmsentities\Collection\ScopeList;
8use BO\Zmsentities\Scope;
9
10/**
11 * Drop missing scope/cluster ids from a calldisplay request so one bad id
12 * cannot take down the whole display.
13 */
14class CalldisplayCollections
15{
16    public static function prepareForGet(Entity $calldisplay): void
17    {
18        self::prepare($calldisplay, 'Calldisplay', 0, false);
19    }
20
21    /**
22     * @return array<int|string, Scope>
23     */
24    public static function prepareForQueue(Entity $calldisplay, int $resolveReferences): array
25    {
26        return self::prepare($calldisplay, 'Calldisplay queue', $resolveReferences, true);
27    }
28
29    /**
30     * @return array<int|string, Scope>
31     */
32    private static function prepare(
33        Entity $calldisplay,
34        string $logPrefix,
35        int $resolveReferences,
36        bool $withWorkstationCount
37    ): array {
38        $hadScopes = $calldisplay->hasScopeList();
39        $hadClusters = $calldisplay->hasClusterList();
40        if (! $hadScopes && ! $hadClusters) {
41            throw new \BO\Zmsbackend\Calldisplay\Exception\ScopeAndClusterNotFound();
42        }
43
44        self::filterClusters($calldisplay, $logPrefix);
45        $scopeCache = self::filterScopes($calldisplay, $logPrefix, $resolveReferences, $withWorkstationCount);
46        self::assertAnythingLeft($calldisplay, $hadScopes, $hadClusters);
47
48        return $scopeCache;
49    }
50
51    private static function filterClusters(Entity $calldisplay, string $logPrefix): void
52    {
53        $clusterList = new ClusterList();
54        $clusterService = new \BO\Zmsbackend\Cluster\Service\Cluster();
55
56        foreach ($calldisplay->getClusterList() as $clusterRef) {
57            $cluster = $clusterService->readEntity($clusterRef->getId());
58            if ($cluster) {
59                $clusterList->addEntity($cluster);
60                continue;
61            }
62            \App::$log->warning($logPrefix . ': skip missing cluster id', [
63                'clusterId' => $clusterRef->getId(),
64            ]);
65        }
66
67        $calldisplay['clusters'] = $clusterList;
68    }
69
70    /**
71     * @return array<int|string, Scope>
72     */
73    private static function filterScopes(
74        Entity $calldisplay,
75        string $logPrefix,
76        int $resolveReferences,
77        bool $withWorkstationCount
78    ): array {
79        $scopeList = new ScopeList();
80        $scopeCache = [];
81        $scopeService = new \BO\Zmsbackend\Scope\Service\Scope();
82
83        foreach ($calldisplay->getScopeList() as $scopeRef) {
84            $scopeId = $scopeRef->getId();
85            $scope = $withWorkstationCount
86                ? $scopeService->readWithWorkstationCount($scopeId, \App::$now, $resolveReferences)
87                : $scopeService->readEntity($scopeId);
88
89            if ($scope instanceof Scope) {
90                $scopeList->addEntity($scope);
91                $scopeCache[$scope->getId()] = $scope;
92                continue;
93            }
94            \App::$log->warning($logPrefix . ': skip missing scope id', [
95                'scopeId' => $scopeId,
96            ]);
97        }
98
99        $calldisplay['scopes'] = $scopeList;
100        return $scopeCache;
101    }
102
103    private static function assertAnythingLeft(Entity $calldisplay, bool $hadScopes, bool $hadClusters): void
104    {
105        if ($calldisplay->hasScopeList() || $calldisplay->hasClusterList()) {
106            return;
107        }
108        if ($hadScopes && ! $hadClusters) {
109            throw new \BO\Zmsbackend\Scope\Exception\ScopeNotFound();
110        }
111        if ($hadClusters && ! $hadScopes) {
112            throw new \BO\Zmsbackend\Cluster\Exception\ClusterNotFound();
113        }
114        throw new \BO\Zmsbackend\Calldisplay\Exception\ScopeAndClusterNotFound();
115    }
116}