Lines
96.15%
75 / 78
Functions and Methods
75.00%
6 / 8
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Day | 96.15% | 75 / 78 | 75.00% | 6 / 8 | 30 | 0.00% | 0 / 1 | |||
| writeTemporaryScopeList | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 5 | |||||
| readAppointmentIntervalByScope | 96.29% | 26 / 27 | 0.00% | 0 / 1 | 8 | |||||
| appointmentInterval | 75.00% | 6 / 8 | 0.00% | 0 / 1 | 6.56 | |||||
| greatestCommonDivisor | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| rewriteTemporaryScopeList | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| readByCalendar | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| readListFromPreparedTemporaryScopeList | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 4 | |||||
| __destruct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Day\Service; | |
| 4 | ||
| 5 | use BO\Zmsentities\Day as Entity; | |
| 6 | ||
| 7 | class Day extends \BO\Zmsbackend\Base | |
| 8 | { | |
| 9 | protected $tempScopeListExists = false; | |
| 10 | ||
| 11 | public function writeTemporaryScopeList(\BO\Zmsentities\Calendar $calendar, int|float|null $slotsRequiredForce = null): void | |
| 12 | { | |
| 13 | $this->getReader()->exec(\BO\Zmsbackend\Day\Repository\Day::QUERY_CREATE_TEMPORARY_SCOPELIST); | |
| 14 | $intervals = $this->readAppointmentIntervalByScope($calendar); | |
| 15 | $monthList = $calendar->getMonthList(); | |
| 16 | $slotsRequired = $slotsRequiredForce; | |
| 17 | foreach ($monthList as $month) { | |
| 18 | $dateTime = $month->getFirstDay(); | |
| 19 | foreach ($calendar->scopes as $scope) { | |
| 20 | if (!$slotsRequiredForce) { | |
| 21 | $slotsRequired = $calendar->scopes->getRequiredSlotsByScope($scope); | |
| 22 | } | |
| 23 | $scopeId = (int) $scope->id; | |
| 24 | $this->getReader()->perform(\BO\Zmsbackend\Day\Repository\Day::QUERY_INSERT_TEMPORARY_SCOPELIST, [ | |
| 25 | 'scopeID' => $scopeId, | |
| 26 | 'year' => $dateTime->format('Y'), | |
| 27 | 'month' => $dateTime->format('m'), | |
| 28 | 'slotsRequired' => $slotsRequired > 1 ? round($slotsRequired, 0) : 1, | |
| 29 | 'appointmentInterval' => $intervals[$scopeId] ?? 5, | |
| 30 | ]); | |
| 31 | } | |
| 32 | } | |
| 33 | $this->tempScopeListExists = true; | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * @return array<int, int> | |
| 38 | */ | |
| 39 | private function readAppointmentIntervalByScope(\BO\Zmsentities\Calendar $calendar): array | |
| 40 | { | |
| 41 | $scopeIds = []; | |
| 42 | foreach ($calendar->scopes as $scope) { | |
| 43 | $scopeIds[] = (int) $scope->id; | |
| 44 | } | |
| 45 | $scopeIds = array_values(array_unique(array_filter($scopeIds))); | |
| 46 | $intervals = []; | |
| 47 | foreach ($scopeIds as $scopeId) { | |
| 48 | $intervals[$scopeId] = 5; | |
| 49 | } | |
| 50 | if ($scopeIds === []) { | |
| 51 | return $intervals; | |
| 52 | } | |
| 53 | ||
| 54 | $placeholders = implode(',', array_fill(0, count($scopeIds), '?')); | |
| 55 | $rows = $this->fetchAll( | |
| 56 | sprintf( | |
| 57 | \BO\Zmsbackend\Day\Repository\Day::QUERY_SELECT_SCOPE_REQUEST_DURATIONS, | |
| 58 | $placeholders | |
| 59 | ), | |
| 60 | $scopeIds | |
| 61 | ); | |
| 62 | $durations = []; | |
| 63 | foreach ($rows as $row) { | |
| 64 | $minutes = (int) $row['minutes']; | |
| 65 | $slots = (int) $row['slots']; | |
| 66 | if ($minutes < 1 || $slots < 1) { | |
| 67 | continue; | |
| 68 | } | |
| 69 | $durations[(int) $row['scopeId']][] = $slots * $minutes; | |
| 70 | } | |
| 71 | foreach ($durations as $scopeId => $scopeDurations) { | |
| 72 | $intervals[$scopeId] = $this->appointmentInterval($scopeDurations); | |
| 73 | } | |
| 74 | ||
| 75 | return $intervals; | |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * @param list<int> $durationMinutes | |
| 80 | */ | |
| 81 | private function appointmentInterval(array $durationMinutes): int | |
| 82 | { | |
| 83 | $interval = 0; | |
| 84 | foreach ($durationMinutes as $duration) { | |
| 85 | if ($duration < 1) { | |
| 86 | continue; | |
| 87 | } | |
| 88 | $interval = $interval === 0 ? $duration : $this->greatestCommonDivisor($interval, $duration); | |
| 89 | } | |
| 90 | if ($interval >= 5 && $interval % 5 === 0) { | |
| 91 | return $interval; | |
| 92 | } | |
| 93 | ||
| 94 | return 5; | |
| 95 | } | |
| 96 | ||
| 97 | private function greatestCommonDivisor(int $a, int $b): int | |
| 98 | { | |
| 99 | while ($b !== 0) { | |
| 100 | $remainder = $a % $b; | |
| 101 | $a = $b; | |
| 102 | $b = $remainder; | |
| 103 | } | |
| 104 | ||
| 105 | return abs($a); | |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Drop and rebuild calendarscope for the calendar's current firstDay/lastDay months. | |
| 110 | * Used to shrink the daylist window (painted month) or scan one neighbor month at a time. | |
| 111 | */ | |
| 112 | public function rewriteTemporaryScopeList(\BO\Zmsentities\Calendar $calendar, int|float|null $slotsRequiredForce = null): void | |
| 113 | { | |
| 114 | if ($this->tempScopeListExists) { | |
| 115 | $this->getReader()->exec(\BO\Zmsbackend\Day\Repository\Day::QUERY_DROP_TEMPORARY_SCOPELIST); | |
| 116 | $this->tempScopeListExists = false; | |
| 117 | } | |
| 118 | $this->writeTemporaryScopeList($calendar, $slotsRequiredForce); | |
| 119 | } | |
| 120 | ||
| 121 | public function readByCalendar(\BO\Zmsentities\Calendar $calendar, int|float|null $slotsRequiredForce = null) | |
| 122 | { | |
| 123 | // We use a temporary table, so we can use create and insert on a readonly connection | |
| 124 | $this->writeTemporaryScopeList($calendar, $slotsRequiredForce); | |
| 125 | ||
| 126 | return $this->readListFromPreparedTemporaryScopeList($slotsRequiredForce); | |
| 127 | } | |
| 128 | ||
| 129 | public function readListFromPreparedTemporaryScopeList(int|float|null $slotsRequiredForce = null, ?string $daylistQuery = null): \BO\Zmsentities\Collection\DayList | |
| 130 | { | |
| 131 | $dayList = new \BO\Zmsentities\Collection\DayList(); | |
| 132 | $dayData = $this->getReader()->fetchAll( | |
| 133 | $daylistQuery ?? \BO\Zmsbackend\Day\Repository\Day::QUERY_DAYLIST_JOIN, | |
| 134 | [ | |
| 135 | 'forceRequiredSlots' => | |
| 136 | ($slotsRequiredForce === null || $slotsRequiredForce < 1) ? 1 : round($slotsRequiredForce), | |
| 137 | ] | |
| 138 | ); | |
| 139 | foreach ($dayData as $day) { | |
| 140 | $day = new \BO\Zmsentities\Day($day); | |
| 141 | $dayList[$day->getDayHash()] = $day; | |
| 142 | } | |
| 143 | ||
| 144 | return $dayList; | |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * Remove temporary scope list at destruct to allow other functions to use it | |
| 149 | */ | |
| 150 | public function __destruct() | |
| 151 | { | |
| 152 | if ($this->tempScopeListExists) { | |
| 153 | $this->getReader()->exec(\BO\Zmsbackend\Day\Repository\Day::QUERY_DROP_TEMPORARY_SCOPELIST); | |
| 154 | } | |
| 155 | } | |
| 156 | } |