Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
57.69% |
30 / 52 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
OverallCalendar | |
57.69% |
30 / 52 |
|
50.00% |
3 / 6 |
15.13 | |
0.00% |
0 / 1 |
insertSlot | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
deleteFreeRange | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
deleteOlderThan | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
book | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
2 | |||
unbook | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
readSlots | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsdb\Query\OverallCalendar as Calender; |
6 | use DateInterval; |
7 | use DateTimeImmutable; |
8 | use DateTimeInterface; |
9 | |
10 | class OverallCalendar extends Base |
11 | { |
12 | public function insertSlot( |
13 | int $scopeId, |
14 | int $availabilityId, |
15 | DateTimeInterface $time, |
16 | int $seat, |
17 | string $status = 'free' |
18 | ): void { |
19 | $this->perform(Calender::INSERT, [ |
20 | 'scope_id' => $scopeId, |
21 | 'availability_id' => $availabilityId, |
22 | 'time' => $time->format('Y-m-d H:i:s'), |
23 | 'seat' => $seat, |
24 | 'status' => $status, |
25 | ]); |
26 | } |
27 | |
28 | public function deleteFreeRange( |
29 | int $scopeId, |
30 | int $availabilityId, |
31 | DateTimeInterface $begin, |
32 | DateTimeInterface $finish |
33 | ): void { |
34 | $this->perform(Calender::DELETE_FREE_RANGE, [ |
35 | 'scope_id' => $scopeId, |
36 | 'availability_id' => $availabilityId, |
37 | 'begin' => $begin->format('Y-m-d H:i:s'), |
38 | 'finish' => $finish->format('Y-m-d H:i:s'), |
39 | ]); |
40 | } |
41 | |
42 | public function deleteOlderThan(DateTimeInterface $date): bool |
43 | { |
44 | return (bool) $this->perform(Calender::DELETE_ALL_BEFORE, [ |
45 | 'threshold' => $date->format('Y-m-d 00:00:00'), |
46 | ]); |
47 | } |
48 | |
49 | public function book( |
50 | int $scopeId, |
51 | string $startTime, |
52 | int $processId, |
53 | int $slotUnits |
54 | ): void { |
55 | $start = new DateTimeImmutable($startTime); |
56 | $end = $start->add(new DateInterval('PT' . ($slotUnits * 5) . 'M')); |
57 | |
58 | $seat = $this->fetchValue(Calender::FIND_FREE_SEAT, [ |
59 | 'scope' => $scopeId, |
60 | 'start' => $start->format('Y-m-d H:i:s'), |
61 | 'end' => $end ->format('Y-m-d H:i:s'), |
62 | 'units' => $slotUnits, |
63 | ]); |
64 | |
65 | if (!$seat) { |
66 | error_log("Failed to book a seat for scope ID {$scopeId} from {$start->format('Y-m-d H:i:s')} to {$end->format('Y-m-d H:i:s')}. No free seats available."); |
67 | return; |
68 | } |
69 | |
70 | $this->perform(Calender::BLOCK_SEAT_RANGE, [ |
71 | 'pid' => $processId, |
72 | 'units' => $slotUnits, |
73 | 'scope' => $scopeId, |
74 | 'seat' => $seat, |
75 | 'start' => $start->format('Y-m-d H:i:s'), |
76 | 'end' => $end ->format('Y-m-d H:i:s'), |
77 | ]); |
78 | } |
79 | |
80 | public function unbook(int $scopeId, int $processId): void |
81 | { |
82 | $this->perform(Calender::UNBOOK_PROCESS, [ |
83 | 'scope_id' => $scopeId, |
84 | 'process_id' => $processId, |
85 | ]); |
86 | } |
87 | |
88 | public function readSlots( |
89 | array $scopeIds, |
90 | string $from, |
91 | string $until, |
92 | ?string $updatedAfter = null |
93 | ): array { |
94 | if (empty($scopeIds)) { |
95 | return []; |
96 | } |
97 | |
98 | $in_list = implode(',', array_map('intval', $scopeIds)); |
99 | |
100 | if ($updatedAfter === null) { |
101 | $sql = sprintf(Calender::SELECT_RANGE, $in_list); |
102 | $params = ['from' => $from, 'until' => $until]; |
103 | } else { |
104 | $sql = sprintf(Calender::SELECT_RANGE_UPDATED, $in_list); |
105 | $params = [ |
106 | 'from' => $from, |
107 | 'until' => $until, |
108 | 'updatedAfter' => $updatedAfter |
109 | ]; |
110 | } |
111 | |
112 | return $this->fetchAll($sql, $params); |
113 | } |
114 | } |