/
BirthdayCalendar.php
120 lines (104 loc) · 3.6 KB
/
BirthdayCalendar.php
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
<?php
namespace LaravelEnso\Calendar\Calendars;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use LaravelEnso\Calendar\Contracts\CustomCalendar;
use LaravelEnso\Calendar\Enums\Colors;
use LaravelEnso\People\Models\Person;
class BirthdayCalendar implements CustomCalendar
{
private Carbon $startDate;
private Carbon $endDate;
public function getKey()
{
return 'birthday-calendar';
}
public function name(): string
{
return 'Birthdays';
}
public function color(): string
{
return Colors::Purple;
}
public function private(): bool
{
return false;
}
public function readonly(): bool
{
return true;
}
public function events(Carbon $startDate, Carbon $endDate): Collection
{
$this->startDate = $startDate;
$this->endDate = $endDate;
$roles = Config::get('enso.calendar.birthdays.roles');
return Person::query()
->unless($roles === ['*'], fn ($query) => $query
->whereHas('user', fn ($query) => $query->whereIn('role_id', $roles)))
->when(! $this->withinSameYear(), $this->differentYearQuery())
->when(
$this->withinSameYear() && $this->withinSameMonth(),
$this->sameMonthQuery()
)->when(
$this->withinSameYear() && ! $this->withinSameMonth(),
$this->differentMonthQuery()
)->get()
->map(fn ($person) => new BirthdayEvent($person, $this->year($person)));
}
private function sameMonthQuery()
{
return function ($query) {
$query->whereMonth('birthday', '=', $this->startDate)
->whereDay('birthday', '>=', $this->startDate)
->whereDay('birthday', '<=', $this->endDate);
};
}
private function differentMonthQuery()
{
return function ($query) {
$query->where(function ($query) {
$query->whereMonth('birthday', $this->startDate)
->whereDay('birthday', '>=', $this->startDate);
})->orWhere(function ($query) {
$query->whereMonth('birthday', $this->endDate)
->whereDay('birthday', '<=', $this->endDate);
})->orWhere(function ($query) {
$query->whereMonth('birthday', '>', $this->startDate)
->whereMonth('birthday', '<', $this->endDate);
});
};
}
private function differentYearQuery()
{
return function ($query) {
$query->where(function ($query) {
$query->whereMonth('birthday', $this->startDate)
->whereDay('birthday', '>=', $this->startDate);
})->orWhere(function ($query) {
$query->whereMonth('birthday', '>', $this->startDate);
})->orWhere(function ($query) {
$query->whereMonth('birthday', $this->endDate)
->whereDay('birthday', '<=', $this->endDate);
})->orWhere(function ($query) {
$query->whereMonth('birthday', '<', $this->endDate);
});
};
}
private function year($person)
{
return $person->birthday->month === $this->startDate->month
? $this->startDate->year
: $this->endDate->year;
}
private function withinSameMonth(): bool
{
return $this->endDate->month === $this->startDate->month;
}
private function withinSameYear(): bool
{
return $this->endDate->month >= $this->startDate->month;
}
}