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