-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlotManager.php
More file actions
150 lines (114 loc) · 4.15 KB
/
Copy pathSlotManager.php
File metadata and controls
150 lines (114 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
/**
* Anchor Framework
*
* Managing slots, schedules, and bookings.
*
* @author BenIyke <beniyke34@gmail.com> | Twitter: @BigBeniyke
*/
namespace Slot;
use Database\BaseModel;
use InvalidArgumentException;
use RuntimeException;
use Slot\Enums\ScheduleType;
use Slot\Interfaces\SlotServiceInterface;
use Slot\Models\SlotBooking;
use Slot\Models\SlotSchedule;
use Slot\Services\SlotAnalyticsService;
class SlotManager
{
private SlotServiceInterface $service;
private ?object $model = null;
public function __construct(SlotServiceInterface $service)
{
$this->service = $service;
}
public function forModel(object $model): self
{
$this->model = $model;
return $this;
}
public function analytics(): SlotAnalyticsService
{
return resolve(SlotAnalyticsService::class);
}
public function schedule(Period $period, array $options = []): SlotSchedule
{
$this->ensureModelSet();
$type = $period->options['type'] ?? null;
if (! $type) {
throw new InvalidArgumentException('Schedule type must be set on the Period object using asAppointment(), asAvailability(), etc.');
}
return $this->service->createSchedule($this->model, $type, $period, $options);
}
public function availability(Period $period, array $options = []): SlotSchedule
{
$this->ensureModelSet();
return $this->service->createSchedule($this->model, ScheduleType::Availability, $period, $options);
}
public function appointment(Period $period, array $options = []): SlotSchedule
{
$this->ensureModelSet();
return $this->service->createSchedule($this->model, ScheduleType::Appointment, $period, $options);
}
public function blocked(Period $period, array $options = []): SlotSchedule
{
$this->ensureModelSet();
return $this->service->createSchedule($this->model, ScheduleType::Blocked, $period, $options);
}
public function custom(Period $period, array $options = []): SlotSchedule
{
$this->ensureModelSet();
return $this->service->createSchedule($this->model, ScheduleType::Custom, $period, $options);
}
public function getAvailableSlots(Period $range, ?int $duration = null, array $constraints = []): array
{
$this->ensureModelSet();
return $this->service->generateSlots($this->model, $range, $duration, $constraints);
}
public function checkConflicts(Period $period, ScheduleType|string|null $type = null): array
{
$this->ensureModelSet();
return $this->service->getConflicts($period, $type, $this->model);
}
public function book(SlotSchedule $schedule, object $bookable, Period $period, array $options = []): SlotBooking
{
return $this->service->createBooking($schedule, $bookable, $period, $options);
}
public function updateSchedule(SlotSchedule $schedule, Period|array $data): bool
{
return $this->service->updateSchedule($schedule, $data);
}
public function deleteSchedule(SlotSchedule $schedule): bool
{
return $this->service->deleteSchedule($schedule);
}
public function getUpcomingBookings(int $minutes): array
{
return $this->service->getUpcomingBookings($minutes);
}
public function getBooking(int $id): ?SlotBooking
{
return $this->service->getBooking($id);
}
public function findSchedule(int $id): ?SlotSchedule
{
return $this->service->findSchedule($id);
}
public function getSchedules(?ScheduleType $type = null, ?Period $overlapping = null): array
{
$this->ensureModelSet();
return $this->service->getSchedules($this->model, $type, $overlapping);
}
public function bookByScheduleId(int $scheduleId, BaseModel $bookable, Period $period, array $options = []): SlotBooking
{
return $this->service->createBookingById($scheduleId, $bookable, $period, $options);
}
private function ensureModelSet(): void
{
if (! $this->model) {
throw new RuntimeException('No model set. Call forModel() first.');
}
}
}