Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
74 / 74
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
ScopeList
100.00% covered (success)
100.00%
74 / 74
100.00% covered (success)
100.00%
14 / 14
43
100.00% covered (success)
100.00%
1 / 1
 getShortestBookableStart
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getGreatestBookableEnd
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getShortestBookableStartOnOpenedScope
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getGreatestBookableEndOnOpenedScope
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 withoutDublicates
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 withUniqueScopes
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 addScopeList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 withLessData
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 sortByName
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 withProviderID
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 addRequiredSlots
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getRequiredSlotsByScope
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hasOpenedScope
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getProviderList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsentities\Collection;
4
5use BO\Zmsentities\Helper\Sorter;
6use BO\Zmsentities\Scope;
7
8class ScopeList extends Base
9{
10    public const ENTITY_CLASS = '\BO\Zmsentities\Scope';
11
12    protected $slotsByID = [];
13
14    /**
15    * Get shortest bookable start date of a scope in scopelist
16    *
17    * @return \DateTimeImmutable $date
18    */
19    public function getShortestBookableStart($now)
20    {
21        $date = $now;
22        foreach ($this as $scope) {
23            $startDate = $scope->getBookableStartDate($now);
24            $date = ($startDate > $date) ? $startDate : $date;
25        }
26        return $date;
27    }
28
29    /**
30    * Get longest bookable end date of a scope in scopelist
31    *
32    * @return \DateTimeImmutable $date
33    */
34    public function getGreatestBookableEnd($now)
35    {
36        $date = $now;
37        foreach ($this as $scope) {
38            $endDate = $scope->getBookableEndDate($now);
39            $date = ($endDate > $date) ? $endDate : $date;
40        }
41        return $date;
42    }
43
44    /**
45    * Get shortest bookable start date of a opened scope in scopelist
46    *
47    * @return \DateTimeImmutable $date, null
48    */
49    public function getShortestBookableStartOnOpenedScope($now)
50    {
51        $date = null;
52        foreach ($this as $scope) {
53            if ($scope->getStatus('availability', 'isOpened')) {
54                $date = $now;
55                $startDate = $scope->getBookableStartDate($date);
56                $date = ($startDate > $date) ? $startDate : $date;
57            }
58        }
59        return $date;
60    }
61
62    /**
63    * Get longest bookable end date of a opened scope in scopelist
64    *
65    * @return \DateTimeImmutable $date
66    */
67    public function getGreatestBookableEndOnOpenedScope($now)
68    {
69        $date = null;
70        foreach ($this as $scope) {
71            if ($scope->getStatus('availability', 'isOpened')) {
72                $date = $now;
73                $endDate = $scope->getBookableEndDate($date);
74                $date = ($endDate > $date) ? $endDate : $date;
75            }
76        }
77        return $date;
78    }
79
80    public function withoutDublicates($scopeList = null)
81    {
82        $collection = new self();
83        foreach ($this as $scope) {
84            if (! $scopeList || ! $scopeList->hasEntity($scope->getId())) {
85                $collection->addEntity(clone $scope);
86            }
87        }
88        return $collection;
89    }
90
91    public function withUniqueScopes()
92    {
93        $scopeList = new self();
94        foreach ($this as $scope) {
95            if ($scope->getId() && ! $scopeList->hasEntity($scope->getId())) {
96                $scopeList->addEntity(clone $scope);
97            }
98        }
99        return $scopeList;
100    }
101
102    public function addScopeList($scopeList)
103    {
104        foreach ($scopeList as $scope) {
105            $this->addEntity($scope);
106        }
107        return $this;
108    }
109
110    #[\Override]
111    public function withLessData(array $keepArray = [])
112    {
113        $scopeList = new self();
114        foreach ($this as $scope) {
115            $scopeList->addEntity(clone $scope->withLessData($keepArray));
116        }
117        return $scopeList;
118    }
119
120    #[\Override]
121    public function sortByName()
122    {
123        $this->uasort(function ($a, $b) {
124            $nameA = (isset($a->provider['name'])) ? $a->provider['name'] : ($a->shortName ?? '');
125            $nameB = (isset($b->provider['name'])) ? $b->provider['name'] : ($b->shortName ?? '');
126            return strcmp(
127                Sorter::toSortableString(ucfirst($nameA ?? '')),
128                Sorter::toSortableString(ucfirst($nameB ?? ''))
129            );
130        });
131        return $this;
132    }
133
134    public function withProviderID($source, $providerID)
135    {
136        $list = new ScopeList();
137        foreach ($this as $scope) {
138            if ($scope->provider['source'] == $source && $scope->provider['id'] == $providerID) {
139                $list->addEntity(clone $scope);
140            }
141        }
142        return $list;
143    }
144
145    public function addRequiredSlots($source, $providerID, $slotsRequired)
146    {
147        $scopeList = $this->withProviderID($source, $providerID);
148        foreach ($scopeList as $scope) {
149            if (!isset($this->slotsByID[$scope->id])) {
150                $this->slotsByID[$scope->id] = 0;
151            }
152            $this->slotsByID[$scope->id] += $slotsRequired;
153        }
154        return $this;
155    }
156
157    public function getRequiredSlotsByScope(Scope $scope)
158    {
159        if (isset($this->slotsByID[$scope->id])) {
160            return $this->slotsByID[$scope->id];
161        }
162        return 0;
163    }
164
165    public function hasOpenedScope()
166    {
167        $isOpened = false;
168        foreach ($this as $entity) {
169            if ($entity->getStatus('availability', 'isOpened')) {
170                $isOpened = true;
171            }
172        }
173        return $isOpened;
174    }
175
176    public function getProviderList()
177    {
178        $list = new ProviderList();
179        foreach ($this as $scope) {
180            $provider = $scope->getProvider();
181            $list->addEntity($provider);
182        }
183        return $list->withUniqueProvider();
184    }
185}