Lines
99.42%
172 / 173
Functions and Methods
95.83%
23 / 24
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Calendar | 99.42% | 172 / 173 | 95.83% | 23 / 24 | 57 | 0.00% | 0 / 1 | |||
| getDefaults | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 1 | |||||
| addDates | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| addFirstAndLastDay | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 1 | |||||
| addProvider | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | |||||
| addCluster | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| addRequest | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | |||||
| addScope | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| getScopeList | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | |||||
| getRequestList | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| getProviderList | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| getDayList | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| hasDay | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getDay | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getDayByDateTime | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getDateTimeFromDate | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| hasFirstAndLastDay | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | |||||
| getFirstDay | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 2 | |||||
| getLastDay | 91.66% | 11 / 12 | 0.00% | 0 / 1 | 4.01 | |||||
| setLastDayTime | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| setFirstDayTime | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| getMonthList | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 2 | |||||
| withLessData | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 5 | |||||
| withFilledEmptyDays | 100.00% | 22 / 22 | 100.00% | 1 / 1 | 4 | |||||
| __toString | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 5 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsentities; | |
| 4 | ||
| 5 | /** | |
| 6 | * | |
| 7 | * @SuppressWarnings(CouplingBetweenObjects) | |
| 8 | * @SuppressWarnings(TooManyPublicMethods) | |
| 9 | * @SuppressWarnings(Complexity) | |
| 10 | */ | |
| 11 | class Calendar extends Schema\Entity | |
| 12 | { | |
| 13 | public const string PRIMARY = 'days'; | |
| 14 | ||
| 15 | public static $schema = "calendar.json"; | |
| 16 | ||
| 17 | /** | |
| 18 | * @return (Collection\DayList|Day|array|null)[] | |
| 19 | * | |
| 20 | */ | |
| 21 | #[\Override] | |
| 22 | public function getDefaults() | |
| 23 | { | |
| 24 | return [ | |
| 25 | 'firstDay' => new Day(), | |
| 26 | 'lastDay' => null, | |
| 27 | 'days' => new Collection\DayList(), | |
| 28 | 'clusters' => [ ], | |
| 29 | 'providers' => [ ], | |
| 30 | 'scopes' => [ ], | |
| 31 | 'requests' => [ ] | |
| 32 | ]; | |
| 33 | } | |
| 34 | ||
| 35 | public function addDates(mixed $date, \DateTimeInterface $now, mixed $timeZone): static | |
| 36 | { | |
| 37 | $validDate = \BO\Mellon\Validator::value($date)->isDate(); | |
| 38 | $date = (! $validDate->hasFailed()) ? $validDate->getValue() : $now->format('U'); | |
| 39 | if (! $this->toProperty()->firstDay->day->get()) { | |
| 40 | $this->addFirstAndLastDay($date, $timeZone); | |
| 41 | } | |
| 42 | return $this; | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Returns calendar with first and last day | |
| 47 | * | |
| 48 | * @return $this | |
| 49 | */ | |
| 50 | public function addFirstAndLastDay(mixed $date, mixed $timeZone) | |
| 51 | { | |
| 52 | $timeZone = new \DateTimeZone($timeZone); | |
| 53 | $dateTime = Helper\DateTime::create()->setTimezone($timeZone)->setTimestamp($date); | |
| 54 | $firstDay = $dateTime->setTime(0, 0, 0); | |
| 55 | $lastDay = $dateTime->modify('last day of next month')->setTime(23, 59, 59); | |
| 56 | $this->firstDay = array( | |
| 57 | 'year' => $firstDay->format('Y'), | |
| 58 | 'month' => $firstDay->format('m'), | |
| 59 | 'day' => $firstDay->format('d') | |
| 60 | ); | |
| 61 | $this->lastDay = array( | |
| 62 | 'year' => $lastDay->format('Y'), | |
| 63 | 'month' => $lastDay->format('m'), | |
| 64 | 'day' => $lastDay->format('d') | |
| 65 | ); | |
| 66 | return $this; | |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Returns calendar with added Providers | |
| 71 | * | |
| 72 | * @return $this | |
| 73 | */ | |
| 74 | public function addProvider(mixed $source, mixed $idList) | |
| 75 | { | |
| 76 | foreach (explode(',', $idList) as $id) { | |
| 77 | if ($id) { | |
| 78 | $provider = new Provider(); | |
| 79 | $provider->source = $source; | |
| 80 | $provider->id = $id; | |
| 81 | $this->providers[] = $provider; | |
| 82 | } | |
| 83 | } | |
| 84 | return $this; | |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Returns calendar with added Clusters | |
| 89 | * | |
| 90 | * @return $this | |
| 91 | */ | |
| 92 | public function addCluster(mixed $idList) | |
| 93 | { | |
| 94 | foreach (explode(',', $idList) as $id) { | |
| 95 | $cluster = new Cluster(); | |
| 96 | $cluster->id = $id; | |
| 97 | $this->clusters[] = $cluster; | |
| 98 | } | |
| 99 | return $this; | |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Returns calendar with added requests | |
| 104 | * | |
| 105 | * @return $this | |
| 106 | */ | |
| 107 | public function addRequest(mixed $source, mixed $requestList) | |
| 108 | { | |
| 109 | foreach (explode(',', $requestList) as $id) { | |
| 110 | if ($id) { | |
| 111 | $request = new Request(); | |
| 112 | $request->source = $source; | |
| 113 | $request->id = $id; | |
| 114 | $this->requests[] = $request; | |
| 115 | } | |
| 116 | } | |
| 117 | return $this; | |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Returns calendar with added scopes | |
| 122 | * | |
| 123 | * @return $this | |
| 124 | */ | |
| 125 | public function addScope(mixed $scopeList) | |
| 126 | { | |
| 127 | foreach (explode(',', $scopeList) as $id) { | |
| 128 | if ($id) { | |
| 129 | $scope = new Scope(); | |
| 130 | $scope->id = $id; | |
| 131 | $this->scopes[] = $scope; | |
| 132 | } | |
| 133 | } | |
| 134 | return $this; | |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * Returns a list of associated scope ids | |
| 139 | * | |
| 140 | * @return Collection\ScopeList | |
| 141 | */ | |
| 142 | public function getScopeList(): Collection\ScopeList | |
| 143 | { | |
| 144 | $scopeList = new \BO\Zmsentities\Collection\ScopeList(); | |
| 145 | if (isset($this->scopes)) { | |
| 146 | foreach ($this->scopes as $scope) { | |
| 147 | $scope = new Scope($scope); | |
| 148 | $scopeList->addEntity($scope); | |
| 149 | } | |
| 150 | } | |
| 151 | return $scopeList; | |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * Returns a list of associated request entities | |
| 156 | * | |
| 157 | * @return Collection\RequestList | |
| 158 | */ | |
| 159 | public function getRequestList(): Collection\RequestList | |
| 160 | { | |
| 161 | $requestList = new \BO\Zmsentities\Collection\RequestList(); | |
| 162 | foreach ($this->requests as $request) { | |
| 163 | $request = new Request($request); | |
| 164 | $requestList->addEntity($request); | |
| 165 | } | |
| 166 | return $requestList; | |
| 167 | } | |
| 168 | ||
| 169 | /** | |
| 170 | * Returns a list of associated provider ids | |
| 171 | * | |
| 172 | * @return Collection\ProviderList | |
| 173 | */ | |
| 174 | public function getProviderList(): Collection\ProviderList | |
| 175 | { | |
| 176 | $providerList = new \BO\Zmsentities\Collection\ProviderList(); | |
| 177 | foreach ($this->providers as $provider) { | |
| 178 | $entity = new Provider($provider); | |
| 179 | $providerList->addEntity($entity); | |
| 180 | } | |
| 181 | return $providerList; | |
| 182 | } | |
| 183 | ||
| 184 | public function getDayList(): Collection\DayList | |
| 185 | { | |
| 186 | $days = $this->days; | |
| 187 | if (!$days instanceof Collection\DayList) { | |
| 188 | $days = new Collection\DayList($days); | |
| 189 | $this->days = $days; | |
| 190 | } | |
| 191 | return $days->setSortByDate(); | |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * Check if given day exists in calendar | |
| 196 | * | |
| 197 | * @return bool | |
| 198 | */ | |
| 199 | public function hasDay(mixed $year, mixed $month, mixed $dayNumber) | |
| 200 | { | |
| 201 | return $this->getDayList()->hasDay($year, $month, $dayNumber); | |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * Returns a day by given year, month and daynumber | |
| 206 | * | |
| 207 | * @return \ArrayObject | |
| 208 | */ | |
| 209 | public function getDay(mixed $year, mixed $month, mixed $dayNumber) | |
| 210 | { | |
| 211 | return $this->getDayList()->getDay($year, $month, $dayNumber); | |
| 212 | } | |
| 213 | ||
| 214 | public function getDayByDateTime(\DateTimeInterface $datetime): mixed | |
| 215 | { | |
| 216 | return $this->getDayList()->getDayByDateTime($datetime); | |
| 217 | } | |
| 218 | ||
| 219 | public function getDateTimeFromDate(array $date): \BO\Zmsentities\Helper\DateTime | |
| 220 | { | |
| 221 | $day = (isset($date['day'])) ? $date['day'] : 1; | |
| 222 | $date = Helper\DateTime::createFromFormat('Y-m-d', $date['year'] . '-' . $date['month'] . '-' . $day); | |
| 223 | return Helper\DateTime::create($date); | |
| 224 | } | |
| 225 | ||
| 226 | /** | |
| 227 | * Simple quick check, if first and last day are defined | |
| 228 | */ | |
| 229 | public function hasFirstAndLastDay(): bool | |
| 230 | { | |
| 231 | if (!$this->toProperty()->firstDay->day->get()) { | |
| 232 | return false; | |
| 233 | } | |
| 234 | if (!$this->toProperty()->lastDay->day->get()) { | |
| 235 | return false; | |
| 236 | } | |
| 237 | return true; | |
| 238 | } | |
| 239 | ||
| 240 | public function getFirstDay(): mixed | |
| 241 | { | |
| 242 | if (isset($this['firstDay'])) { | |
| 243 | $dateTime = $this->getDateTimeFromDate( | |
| 244 | array( | |
| 245 | 'year' => $this['firstDay']['year'], | |
| 246 | 'month' => $this['firstDay']['month'], | |
| 247 | 'day' => $this['firstDay']['day'] | |
| 248 | ) | |
| 249 | ); | |
| 250 | } else { | |
| 251 | $dateTime = Helper\DateTime::create(); | |
| 252 | } | |
| 253 | return $dateTime->modify('00:00:00'); | |
| 254 | } | |
| 255 | ||
| 256 | public function getLastDay(bool $createIfNotProvided = true): mixed | |
| 257 | { | |
| 258 | if (! $createIfNotProvided && ! isset($this['lastDay'])) { | |
| 259 | return null; | |
| 260 | } | |
| 261 | ||
| 262 | if (isset($this['lastDay'])) { | |
| 263 | $dateTime = $this->getDateTimeFromDate( | |
| 264 | array( | |
| 265 | 'year' => $this['lastDay']['year'], | |
| 266 | 'month' => $this['lastDay']['month'], | |
| 267 | 'day' => $this['lastDay']['day'] | |
| 268 | ) | |
| 269 | ); | |
| 270 | } else { | |
| 271 | $dateTime = Helper\DateTime::create(); | |
| 272 | } | |
| 273 | return $dateTime->modify('23:59:59'); | |
| 274 | } | |
| 275 | ||
| 276 | public function setLastDayTime(mixed $date): static | |
| 277 | { | |
| 278 | $day = new Day(); | |
| 279 | $day->setDateTime($date); | |
| 280 | $this['lastDay'] = $day; | |
| 281 | return $this; | |
| 282 | } | |
| 283 | ||
| 284 | public function setFirstDayTime(mixed $date): static | |
| 285 | { | |
| 286 | $day = new Day(); | |
| 287 | $day->setDateTime($date); | |
| 288 | $this['firstDay'] = $day; | |
| 289 | return $this; | |
| 290 | } | |
| 291 | ||
| 292 | /** | |
| 293 | * Returns a list of contained month given by firstDay and lastDay | |
| 294 | * The return value is a month entity object for the first day of the month | |
| 295 | */ | |
| 296 | public function getMonthList(): Collection\MonthList | |
| 297 | { | |
| 298 | $firstDay = Helper\DateTime::create($this->getFirstDay())->modify('first day of this month')->modify('00:00:00'); | |
| 299 | $lastDay = Helper\DateTime::create($this->getLastDay())->modify('last day of this month')->modify('23:59:59'); | |
| 300 | $currentDate = $firstDay; | |
| 301 | if ($firstDay->getTimestamp() > $lastDay->getTimestamp()) { | |
| 302 | // switch first and last day if necessary | |
| 303 | $currentDate = $lastDay; | |
| 304 | $lastDay = $firstDay; | |
| 305 | } | |
| 306 | $monthList = new Collection\MonthList(); | |
| 307 | do { | |
| 308 | $monthList->addEntity(Month::createForDateFromDayList($currentDate, $this->days)); | |
| 309 | $currentDate = $currentDate->modify('+1 month'); | |
| 310 | } while ($currentDate->getTimestamp() < $lastDay->getTimestamp()); | |
| 311 | return $monthList; | |
| 312 | } | |
| 313 | ||
| 314 | /** | |
| 315 | * Reduce data of dereferenced entities to a required minimum | |
| 316 | * | |
| 317 | * @return static | |
| 318 | */ | |
| 319 | #[\Override] | |
| 320 | public function withLessData() | |
| 321 | { | |
| 322 | $entity = clone $this; | |
| 323 | ||
| 324 | foreach ($entity['scopes'] as $scope) { | |
| 325 | if ($scope->toProperty()->provider->data->isAvailable()) { | |
| 326 | $payment = $scope->toProperty()->provider->data->payment->get(); | |
| 327 | unset($scope['provider']['data']); | |
| 328 | $scope['provider']['data'] = ['payment' => $payment]; | |
| 329 | unset($scope['dayoff']); | |
| 330 | unset($scope['status']); | |
| 331 | unset($scope['preferences']); | |
| 332 | } | |
| 333 | } | |
| 334 | foreach ($entity['days'] as $day) { | |
| 335 | if (isset($day['allAppointments'])) { | |
| 336 | unset($day['allAppointments']); | |
| 337 | } | |
| 338 | } | |
| 339 | unset($entity['providers']); | |
| 340 | unset($entity['clusters']); | |
| 341 | unset($entity['freeProcesses']); | |
| 342 | return $entity; | |
| 343 | } | |
| 344 | ||
| 345 | public function withFilledEmptyDays(): static | |
| 346 | { | |
| 347 | $entity = clone $this; | |
| 348 | ||
| 349 | $firstDay = Helper\DateTime::create($this->getFirstDay())->modify('first day of this month')->modify('00:00:00'); | |
| 350 | $lastDay = Helper\DateTime::create($this->getLastDay())->modify('last day of this month')->modify('23:59:59'); | |
| 351 | $currentDate = $firstDay; | |
| 352 | $dayList = new Collection\DayList($entity->days); | |
| 353 | ||
| 354 | do { | |
| 355 | $day = new Day([ | |
| 356 | 'year' => $currentDate->format('Y'), | |
| 357 | 'month' => $currentDate->format('m'), | |
| 358 | 'day' => $currentDate->format('d') | |
| 359 | ]); | |
| 360 | $dayTimestamp = $day->toDateTime()->getTimestamp(); | |
| 361 | $dayFound = false; | |
| 362 | ||
| 363 | foreach ($dayList as $checkingDay) { | |
| 364 | $checkingTimestamp = $checkingDay->toDateTime()->getTimestamp(); | |
| 365 | if ($checkingTimestamp === $dayTimestamp) { | |
| 366 | $dayFound = true; | |
| 367 | } | |
| 368 | } | |
| 369 | ||
| 370 | if (!$dayFound) { | |
| 371 | $dayList->addEntity($day); | |
| 372 | } | |
| 373 | ||
| 374 | $currentDate = $currentDate->modify('+1 day'); | |
| 375 | } while ($currentDate->getTimestamp() < $lastDay->getTimestamp()); | |
| 376 | ||
| 377 | $entity->days = $dayList; | |
| 378 | ||
| 379 | return $entity; | |
| 380 | } | |
| 381 | ||
| 382 | public function __toString() | |
| 383 | { | |
| 384 | $string = ''; | |
| 385 | foreach ($this->days as $day) { | |
| 386 | $day = ($day instanceof Day) ? $day : new Day($day); | |
| 387 | $string .= "$day\n"; | |
| 388 | } | |
| 389 | foreach ($this->scopes as $scope) { | |
| 390 | $scope = ($scope instanceof Scope) ? $scope : new Scope($scope); | |
| 391 | $string .= "$scope\n"; | |
| 392 | } | |
| 393 | return $string; | |
| 394 | } | |
| 395 | } |