Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
77 / 77
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
Calldisplay
100.00% covered (success)
100.00%
77 / 77
100.00% covered (success)
100.00%
12 / 12
38
100.00% covered (success)
100.00%
1 / 1
 getDefaults
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 withResolvedCollections
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 hasScopeList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasClusterList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setServerTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFullScopeList
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 getScopeList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getClusterList
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getImageName
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 withOutClusterDuplicates
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
8
 getScopeListFromCsv
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getClusterListFromCsv
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace BO\Zmsentities;
4
5use BO\Zmsentities\Helper\Property;
6
7/**
8 * Schema-backed entity (ArrayObject::ARRAY_AS_PROPS); document dynamic keys for Psalm.
9 *
10 * @property Collection\ClusterList|array $clusters
11 * @property Collection\ScopeList|array $scopes
12 * @property Organisation $organisation
13 * @property int|float $serverTime
14 */
15class Calldisplay extends Schema\Entity
16{
17    public const string PRIMARY = 'serverTime';
18
19    public static $schema = "calldisplay.json";
20
21    /**
22     * @return (Collection\ClusterList|Collection\ScopeList|Organisation|int)[]
23     *
24     */
25    #[\Override]
26    public function getDefaults()
27    {
28        return [
29            'serverTime' => (new \DateTime())->getTimestamp(),
30            'clusters' => new Collection\ClusterList(),
31            'scopes' => new Collection\ScopeList(),
32            'organisation' => new Organisation(),
33        ];
34    }
35
36    public function withResolvedCollections(mixed $input): static
37    {
38        $input =  (is_object($input)) ? $input->getArrayCopy() : $input;
39        if (Property::__keyExists('scopelist', $input)) {
40            $this->scopes = $this->getScopeListFromCsv($input['scopelist']);
41        }
42        if (Property::__keyExists('clusterlist', $input)) {
43            $this->clusters = $this->getClusterListFromCsv($input['clusterlist']);
44        }
45        return $this;
46    }
47
48    public function hasScopeList(): bool
49    {
50        return (0 < $this->getScopeList()->count());
51    }
52
53    public function hasClusterList(): bool
54    {
55        return (0 < $this->getClusterList()->count());
56    }
57
58    public function setServerTime(mixed $timestamp): static
59    {
60        $this->serverTime = $timestamp;
61        return $this;
62    }
63
64    public function getFullScopeList(): \BO\Zmsentities\Collection\ScopeList
65    {
66        $scopeList = $this->getScopeList();
67        foreach ($this->clusters as $cluster) {
68            if (Property::__keyExists('scopes', $cluster)) {
69                foreach ($cluster['scopes'] as $clusterScope) {
70                    $scope = new Scope($clusterScope);
71                    if (! $scopeList->hasEntity($scope['id'])) {
72                        $scopeList->addEntity($scope);
73                    }
74                }
75            }
76        }
77        return $scopeList;
78    }
79
80    public function getScopeList(): Collection\ScopeList
81    {
82        if (!$this->scopes instanceof Collection\ScopeList) {
83            $scopeList = new Collection\ScopeList();
84            foreach ($this->scopes as $scope) {
85                $scopeList->addEntity(new Scope($scope));
86            }
87            $this->scopes = $scopeList;
88        }
89        return $this->scopes;
90    }
91
92    public function getClusterList(): Collection\ClusterList
93    {
94        if (!$this->clusters instanceof Collection\ClusterList) {
95            $clusterList = new Collection\ClusterList();
96            foreach ($this->clusters as $cluster) {
97                $clusterList->addEntity(new Cluster($cluster));
98            }
99            $this->clusters = $clusterList;
100        }
101        return $this->clusters;
102    }
103
104    public function getImageName(): string
105    {
106        $name = '';
107        if (1 == $this->getScopeList()->count()) {
108            $scope = $this->getScopeList()->getFirst();
109            if ($scope !== null) {
110                $name = "s_" . $scope->id . "_bild";
111            }
112        } elseif (1 == $this->getClusterList()->count()) {
113            $cluster = $this->getClusterList()->getFirst();
114            if ($cluster !== null) {
115                $name = "c_" . $cluster->id . "_bild";
116            }
117        }
118        return $name;
119    }
120
121    public function withOutClusterDuplicates(): self
122    {
123        $calldisplay = new self($this);
124        if ($calldisplay->hasClusterList() && $calldisplay->hasScopeList()) {
125            $clusterScopeList = new Collection\ScopeList();
126            foreach ($calldisplay->clusters as $cluster) {
127                if (Property::__keyExists('scopes', $cluster)) {
128                    foreach ($cluster['scopes'] as $clusterScope) {
129                        $scope = new Scope($clusterScope);
130                        $clusterScopeList->addEntity($scope);
131                    }
132                }
133            }
134            $scopeList = new Collection\ScopeList();
135            foreach ($calldisplay->scopes as $scope) {
136                if (! $clusterScopeList->hasEntity($scope['id'])) {
137                    $scope = new Scope($scope);
138                    $scopeList->addEntity($scope);
139                }
140            }
141            $calldisplay->scopes = $scopeList;
142        }
143        return $calldisplay;
144    }
145
146    protected function getScopeListFromCsv(string $scopeIds = ''): Collection\ScopeList
147    {
148        $scopeList = new Collection\ScopeList();
149        $scopeIds = explode(',', $scopeIds);
150        foreach ($scopeIds as $scopeId) {
151            if ($scopeId === '') {
152                continue;
153            }
154            $scope = new Scope(array('id' => $scopeId));
155            $scopeList->addEntity($scope);
156        }
157        return $scopeList;
158    }
159
160    protected function getClusterListFromCsv(string $clusterIds = ''): Collection\ClusterList
161    {
162        $clusterList = new Collection\ClusterList();
163        $clusterIds = explode(',', $clusterIds);
164        foreach ($clusterIds as $clusterId) {
165            if ($clusterId === '') {
166                continue;
167            }
168            $cluster = new Cluster(array('id' => $clusterId));
169            $clusterList->addEntity($cluster);
170        }
171        return $clusterList;
172    }
173}