Lines
65.69%
90 / 137
Functions and Methods
69.56%
16 / 23
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AvailabilityList | 65.69% | 90 / 137 | 69.56% | 16 / 23 | 241.88 | 0.00% | 0 / 1 | |||
| getMaxWorkstationCount | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| withCalculatedSlots | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| withType | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| withOutDoubles | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| hasMatchOf | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| withDateTime | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| withDateTimeInRange | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 20 | |||||
| getAvailableSecondsOnDateTime | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| isOpenedByDate | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| isOpened | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| hasAppointment | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| getSlotList | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| getSortedByDuration | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 3 | |||||
| getSlotListByAvailability | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | |||||
| getSlotListByType | 0.00% | 0 / 6 | 0.00% | 0 / 1 | 20 | |||||
| getConflicts | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 6 | |||||
| hasOverlapWith | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| hasDayOffOverride | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 12 | |||||
| validateTimeRangesAndRules | 0.00% | 0 / 16 | 0.00% | 0 / 1 | 6 | |||||
| getSummerizedSlotCount | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 12 | |||||
| getCalculatedSlotCount | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| withScope | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| withLessData | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG | |
| 5 | **/ | |
| 6 | ||
| 7 | namespace BO\Zmsentities\Collection; | |
| 8 | ||
| 9 | use BO\Zmsentities\Availability; | |
| 10 | ||
| 11 | /** | |
| 12 | * @SuppressWarnings(Complexity) | |
| 13 | * @extends Base<\BO\Zmsentities\Availability> | |
| 14 | */ | |
| 15 | class AvailabilityList extends Base | |
| 16 | { | |
| 17 | public const string ENTITY_CLASS = '\BO\Zmsentities\Availability'; | |
| 18 | ||
| 19 | public function getMaxWorkstationCount(): mixed | |
| 20 | { | |
| 21 | $max = 0; | |
| 22 | foreach ($this as $availability) { | |
| 23 | if ($availability['workstationCount']['intern'] > $max) { | |
| 24 | $max = $availability['workstationCount']['intern']; | |
| 25 | } | |
| 26 | } | |
| 27 | return $max; | |
| 28 | } | |
| 29 | ||
| 30 | public function withCalculatedSlots(): static | |
| 31 | { | |
| 32 | $list = clone $this; | |
| 33 | foreach ($list as $key => $availability) { | |
| 34 | $list[$key] = $availability->withCalculatedSlots(); | |
| 35 | } | |
| 36 | return $list; | |
| 37 | } | |
| 38 | ||
| 39 | public function withType(string $type): static | |
| 40 | { | |
| 41 | $collection = new static(); | |
| 42 | foreach ($this as $availability) { | |
| 43 | if ($availability->type == $type) { | |
| 44 | $collection[] = clone $availability; | |
| 45 | } | |
| 46 | } | |
| 47 | return $collection; | |
| 48 | } | |
| 49 | ||
| 50 | public function withOutDoubles(): static | |
| 51 | { | |
| 52 | $collection = new static(); | |
| 53 | foreach ($this as $availability) { | |
| 54 | if (false === $collection->hasMatchOf($availability)) { | |
| 55 | $collection[] = clone $availability; | |
| 56 | } | |
| 57 | } | |
| 58 | return $collection; | |
| 59 | } | |
| 60 | ||
| 61 | public function hasMatchOf(Availability $availability): Availability|false | |
| 62 | { | |
| 63 | foreach ($this as $item) { | |
| 64 | if ($item->isMatchOf($availability)) { | |
| 65 | return $item; | |
| 66 | } | |
| 67 | } | |
| 68 | return false; | |
| 69 | } | |
| 70 | ||
| 71 | public function withDateTime(\DateTimeInterface $dateTime): self | |
| 72 | { | |
| 73 | $list = new self(); | |
| 74 | foreach ($this as $availability) { | |
| 75 | if ($availability->isOpenedOnDate($dateTime)) { | |
| 76 | $list->addEntity($availability); | |
| 77 | } | |
| 78 | } | |
| 79 | return $list; | |
| 80 | } | |
| 81 | ||
| 82 | public function withDateTimeInRange(\DateTimeInterface $startDateTime, \DateTimeInterface $endDateTime): mixed | |
| 83 | { | |
| 84 | $list = new self(); | |
| 85 | $currentDateTime = \BO\Zmsentities\Helper\DateTime::create($startDateTime); | |
| 86 | while ($currentDateTime <= $endDateTime) { | |
| 87 | foreach ($this as $availability) { | |
| 88 | if ($availability->isOpenedOnDate($currentDateTime)) { | |
| 89 | $list->addEntity($availability); | |
| 90 | } | |
| 91 | } | |
| 92 | $currentDateTime = $currentDateTime->modify('+1 day'); | |
| 93 | } | |
| 94 | return $list->withOutDoubles(); | |
| 95 | } | |
| 96 | ||
| 97 | public function getAvailableSecondsOnDateTime(\DateTimeInterface $dateTime, string $type = "intern"): mixed | |
| 98 | { | |
| 99 | $seconds = 0; | |
| 100 | foreach ($this->withType('appointment')->withDateTime($dateTime) as $availability) { | |
| 101 | $seconds += $availability->getAvailableSecondsPerDay($type); | |
| 102 | } | |
| 103 | return $seconds; | |
| 104 | } | |
| 105 | ||
| 106 | /* | |
| 107 | * is opened on a day -> not specified by a time | |
| 108 | */ | |
| 109 | public function isOpenedByDate(\DateTimeInterface $dateTime, string|false $type = false): bool | |
| 110 | { | |
| 111 | foreach ($this as $availability) { | |
| 112 | if ($availability->isOpenedOnDate($dateTime, $type)) { | |
| 113 | return true; | |
| 114 | } | |
| 115 | } | |
| 116 | return false; | |
| 117 | } | |
| 118 | ||
| 119 | /* | |
| 120 | * is opened on a day with specified time | |
| 121 | */ | |
| 122 | public function isOpened(\DateTimeInterface $dateTime, string $type = "openinghours"): bool | |
| 123 | { | |
| 124 | foreach ($this as $availability) { | |
| 125 | if ($availability->isOpened($dateTime, $type)) { | |
| 126 | return true; | |
| 127 | } | |
| 128 | } | |
| 129 | return false; | |
| 130 | } | |
| 131 | ||
| 132 | public function hasAppointment(\BO\Zmsentities\Appointment $appointment): bool | |
| 133 | { | |
| 134 | foreach ($this as $availability) { | |
| 135 | if ($availability->hasAppointment($appointment)) { | |
| 136 | return true; | |
| 137 | } | |
| 138 | } | |
| 139 | return false; | |
| 140 | } | |
| 141 | ||
| 142 | public function getSlotList(): SlotList | |
| 143 | { | |
| 144 | $slotList = new SlotList(); | |
| 145 | foreach ($this as $availability) { | |
| 146 | foreach ($availability->getSlotList() as $slot) { | |
| 147 | $slotList->addEntity($slot); | |
| 148 | } | |
| 149 | } | |
| 150 | return $slotList; | |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Availabilities keyed by id (or tempId), shorter availabilities first (then lower id). | |
| 155 | * This is the only definition of which overlapping availability is preferred: the backend uses it to | |
| 156 | * choose the availability of a booking (Slot::getRequiredSlotsByAvailability()). The only copy is the SQL | |
| 157 | * fallback for unmapped bookings in Availability\Repository\Availability::getJoinExpression(). | |
| 158 | * | |
| 159 | * @return array<int|string, Availability> | |
| 160 | */ | |
| 161 | public function getSortedByDuration(): array | |
| 162 | { | |
| 163 | $availabilityList = iterator_to_array($this); | |
| 164 | usort($availabilityList, function (Availability $left, Availability $right) { | |
| 165 | $leftDuration = $left->getEndDateTime()->getSecondsOfDay() - $left->getStartDateTime()->getSecondsOfDay(); | |
| 166 | $rightDuration = $right->getEndDateTime()->getSecondsOfDay() | |
| 167 | - $right->getStartDateTime()->getSecondsOfDay(); | |
| 168 | return [$leftDuration, (int) $left->id] <=> [$rightDuration, (int) $right->id]; | |
| 169 | }); | |
| 170 | $sorted = []; | |
| 171 | foreach ($availabilityList as $index => $availability) { | |
| 172 | $key = $availability->id ?: ($availability->tempId ?? "index-$index"); | |
| 173 | $sorted[$key] = $availability; | |
| 174 | } | |
| 175 | return $sorted; | |
| 176 | } | |
| 177 | ||
| 178 | /** | |
| 179 | * One slot list per availability, keys and order as in getSortedByDuration() | |
| 180 | * | |
| 181 | * @return array<int|string, SlotList> | |
| 182 | */ | |
| 183 | public function getSlotListByAvailability(): array | |
| 184 | { | |
| 185 | $slotLists = []; | |
| 186 | foreach ($this->getSortedByDuration() as $key => $availability) { | |
| 187 | $slotLists[$key] = $availability->getSlotList(); | |
| 188 | } | |
| 189 | return $slotLists; | |
| 190 | } | |
| 191 | ||
| 192 | public function getSlotListByType(mixed $type): SlotList | |
| 193 | { | |
| 194 | $slotList = new SlotList(); | |
| 195 | foreach ($this as $availability) { | |
| 196 | if ($availability->type == $type) { | |
| 197 | foreach ($availability->getSlotList() as $slot) { | |
| 198 | $slotList->addEntity($slot); | |
| 199 | } | |
| 200 | } | |
| 201 | } | |
| 202 | return $slotList; | |
| 203 | } | |
| 204 | ||
| 205 | public function getConflicts(mixed $startDate, mixed $endDate): ProcessList | |
| 206 | { | |
| 207 | $processList = new ProcessList(); | |
| 208 | foreach ($this as $availability) { | |
| 209 | $conflict = $availability->getConflict(); | |
| 210 | $currentDate = \BO\Zmsentities\Helper\DateTime::create($startDate); | |
| 211 | $endDateTime = \BO\Zmsentities\Helper\DateTime::create($endDate); | |
| 212 | while ($currentDate <= $endDateTime) { | |
| 213 | if ($availability->isOpenedOnDate($currentDate)) { | |
| 214 | if ($conflict) { | |
| 215 | $conflictOnDay = clone $conflict; | |
| 216 | // to avoid overwrite time settings from availability getConflict lets modify | |
| 217 | $appointmentTime = $conflictOnDay->getFirstAppointment()->getStartTime()->format('H:i'); | |
| 218 | $newDate = \BO\Zmsentities\Helper\DateTime::create($currentDate); | |
| 219 | $conflictOnDay->getFirstAppointment()->setDateTime($newDate->modify($appointmentTime)); | |
| 220 | $processList->addEntity($conflictOnDay); | |
| 221 | } | |
| 222 | $overlapList = $this->hasOverlapWith($availability, $currentDate); | |
| 223 | if ($overlapList->count()) { | |
| 224 | $processList->addList($overlapList); | |
| 225 | } | |
| 226 | } | |
| 227 | $currentDate = $currentDate->modify('+1day'); | |
| 228 | } | |
| 229 | } | |
| 230 | return $processList; | |
| 231 | } | |
| 232 | ||
| 233 | public function hasOverlapWith(Availability $availability, \DateTimeInterface $currentDate): ProcessList | |
| 234 | { | |
| 235 | $processList = new ProcessList(); | |
| 236 | foreach ($this as $availabilityCompare) { | |
| 237 | if ($availabilityCompare->isOpenedOnDate($currentDate)) { | |
| 238 | $overlaps = $availability->getTimeOverlaps($availabilityCompare, $currentDate); | |
| 239 | $processList->addList($overlaps); | |
| 240 | } | |
| 241 | } | |
| 242 | return $processList; | |
| 243 | } | |
| 244 | ||
| 245 | public function hasDayOffOverride(): bool | |
| 246 | { | |
| 247 | foreach ($this as $availability) { | |
| 248 | if ($availability->overridesDayOff()) { | |
| 249 | return true; | |
| 250 | } | |
| 251 | } | |
| 252 | return false; | |
| 253 | } | |
| 254 | ||
| 255 | public function validateTimeRangesAndRules( | |
| 256 | \DateTimeImmutable $startDate, | |
| 257 | \DateTimeImmutable $endDate, | |
| 258 | \DateTimeImmutable $selectedDate, | |
| 259 | string $kind, | |
| 260 | mixed $startInDays, | |
| 261 | mixed $endInDays, | |
| 262 | array $weekday | |
| 263 | ): array { | |
| 264 | $errorList = []; | |
| 265 | ||
| 266 | $today = new \DateTimeImmutable('now', $selectedDate->getTimezone()); | |
| 267 | $yesterday = (clone $selectedDate)->modify('-1 day'); | |
| 268 | $tomorrow = (clone $selectedDate)->modify('+1 day'); | |
| 269 | ||
| 270 | foreach ($this as $availability) { | |
| 271 | $errorList = array_merge( | |
| 272 | $errorList, | |
| 273 | $availability->validateWeekdays($startDate, $endDate, $weekday, $kind), | |
| 274 | $availability->validateStartTime($today, $tomorrow, $startDate, $selectedDate, $kind), | |
| 275 | $availability->validateEndTime($startDate, $endDate), | |
| 276 | $availability->validateOriginEndTime($today, $yesterday, $endDate, $selectedDate, $kind), | |
| 277 | $availability->validateType($kind), | |
| 278 | $availability->validateSlotTime($startDate, $endDate), | |
| 279 | $availability->validateBookableDayRange((int) $startInDays, (int) $endInDays) | |
| 280 | ); | |
| 281 | } | |
| 282 | return $errorList; | |
| 283 | } | |
| 284 | ||
| 285 | /** | |
| 286 | * @return array | |
| 287 | */ | |
| 288 | public function getSummerizedSlotCount(): array | |
| 289 | { | |
| 290 | $slotCounts = []; | |
| 291 | foreach ($this as $item) { | |
| 292 | $itemId = ($item->id) ? $item->id : $item->tempId; | |
| 293 | $slotCounts[$itemId] = (int) $item->getSlotList()->getSummerizedSlot()->intern; | |
| 294 | } | |
| 295 | return $slotCounts; | |
| 296 | } | |
| 297 | ||
| 298 | /** | |
| 299 | * Booked slots per availability, a booking in overlapping availabilities counts for one of them | |
| 300 | * | |
| 301 | * @return array | |
| 302 | */ | |
| 303 | public function getCalculatedSlotCount(\BO\Zmsentities\Collection\ProcessList $processList): array | |
| 304 | { | |
| 305 | return $processList->getCalculatedSlotCountByAvailability($this); | |
| 306 | } | |
| 307 | ||
| 308 | ||
| 309 | public function withScope(\BO\Zmsentities\Scope $scope): static | |
| 310 | { | |
| 311 | $list = clone $this; | |
| 312 | foreach ($list as $key => $availability) { | |
| 313 | $list[$key] = $availability->withScope($scope); | |
| 314 | } | |
| 315 | return $list; | |
| 316 | } | |
| 317 | ||
| 318 | /** | |
| 319 | * @return static | |
| 320 | */ | |
| 321 | #[\Override] | |
| 322 | public function withLessData(array $keepArray = []) | |
| 323 | { | |
| 324 | /** @psalm-suppress UnsafeGenericInstantiation */ | |
| 325 | $list = new static(); | |
| 326 | foreach ($this as $availability) { | |
| 327 | $list->addEntity(clone $availability->withLessData($keepArray)); | |
| 328 | } | |
| 329 | return $list; | |
| 330 | } | |
| 331 | } |