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    public function withLessData(array $keepArray = [])
111    {
112        $scopeList = new self();
113        foreach ($this as $scope) {
114            $scopeList->addEntity(clone $scope->withLessData($keepArray));
115        }
116        return $scopeList;
117    }
118
119    public function sortByName()
120    {
121        $this->uasort(function ($a, $b) {
122            $nameA = (isset($a->provider['name'])) ? $a->provider['name'] : ($a->shortName ?? '');
123            $nameB = (isset($b->provider['name'])) ? $b->provider['name'] : ($b->shortName ?? '');
124            return strcmp(
125                Sorter::toSortableString(ucfirst($nameA ?? '')),
126                Sorter::toSortableString(ucfirst($nameB ?? ''))
127            );
128        });
129        return $this;
130    }
131
132    public function withProviderID($source, $providerID)
133    {
134        $list = new ScopeList();
135        foreach ($this as $scope) {
136            if ($scope->provider['source'] == $source && $scope->provider['id'] == $providerID) {
137                $list->addEntity(clone $scope);
138            }
139        }
140        return $list;
141    }
142
143    public function addRequiredSlots($source, $providerID, $slotsRequired)
144    {
145        $scopeList = $this->withProviderID($source, $providerID);
146        foreach ($scopeList as $scope) {
147            if (!isset($this->slotsByID[$scope->id])) {
148                $this->slotsByID[$scope->id] = 0;
149            }
150            $this->slotsByID[$scope->id] += $slotsRequired;
151        }
152        return $this;
153    }
154
155    public function getRequiredSlotsByScope(Scope $scope)
156    {
157        if (isset($this->slotsByID[$scope->id])) {
158            return $this->slotsByID[$scope->id];
159        }
160        return 0;
161    }
162
163    public function hasOpenedScope()
164    {
165        $isOpened = false;
166        foreach ($this as $entity) {
167            if ($entity->getStatus('availability', 'isOpened')) {
168                $isOpened = true;
169            }
170        }
171        return $isOpened;
172    }
173
174    public function getProviderList()
175    {
176        $list = new ProviderList();
177        foreach ($this as $scope) {
178            $provider = $scope->getProvider();
179            $list->addEntity($provider);
180        }
181        return $list->withUniqueProvider();
182    }
183}