1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\GoogleCalendar; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Google_Service_Calendar; |
8
|
|
|
use Google_Service_Calendar_Event; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
|
11
|
|
|
class GoogleCalendar |
12
|
|
|
{ |
13
|
|
|
/** @var \Google_Service_Calendar */ |
14
|
|
|
protected $calendarService; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $calendarId; |
18
|
|
|
|
19
|
|
|
/** @var iterable */ |
20
|
|
|
protected $batchRequests; |
21
|
|
|
|
22
|
|
|
public function __construct(Google_Service_Calendar $calendarService, string $calendarId) |
23
|
|
|
{ |
24
|
|
|
$this->calendarService = $calendarService; |
25
|
|
|
|
26
|
|
|
$this->calendarId = $calendarId; |
27
|
|
|
|
28
|
|
|
$this->batchRequests = $this->calendarService->createBatch(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getCalendarId(): string |
32
|
|
|
{ |
33
|
|
|
return $this->calendarId; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/list |
38
|
|
|
*/ |
39
|
|
|
public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = []): array |
40
|
|
|
{ |
41
|
|
|
$parameters = ['singleEvents' => true]; |
42
|
|
|
|
43
|
|
|
if (is_null($startDateTime)) { |
44
|
|
|
$startDateTime = Carbon::now()->startOfDay(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339); |
48
|
|
|
|
49
|
|
|
if (is_null($endDateTime)) { |
50
|
|
|
$endDateTime = Carbon::now()->addYear()->endOfDay(); |
51
|
|
|
} |
52
|
|
|
$parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339); |
53
|
|
|
|
54
|
|
|
$parameters = array_merge($parameters, $queryParameters); |
55
|
|
|
|
56
|
|
|
return $this |
57
|
|
|
->calendarService |
58
|
|
|
->events |
59
|
|
|
->listEvents($this->calendarId, $parameters) |
60
|
|
|
->getItems(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $eventId |
66
|
|
|
* @param array $optParams |
67
|
|
|
* |
68
|
|
|
* @return Google_Service_Calendar_Event|RequestInterface |
69
|
|
|
*/ |
70
|
|
|
public function getEvent(string $eventId, $optParams = []) |
71
|
|
|
{ |
72
|
|
|
return $this->calendarService->events->get($this->calendarId, $eventId, $optParams); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function getEvents(iterable $eventIds, $optParams = []) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->enableBatch(true); |
78
|
|
|
|
79
|
|
|
collect($eventIds) |
80
|
|
|
->each( |
81
|
|
|
function ($eventId, $batchIdentifier) use ($optParams) { |
82
|
|
|
$this->batchRequests->add($this->getEvent($eventId, $optParams), "get-".$batchIdentifier); |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $event |
90
|
|
|
* @param array $optParams |
91
|
|
|
* |
92
|
|
|
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert |
93
|
|
|
* @return Google_Service_Calendar_Event|RequestInterface |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function insertEvent($event, $optParams = []) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if ($event instanceof Event) { |
98
|
|
|
$event = $event->googleEvent; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->calendarService->events->insert($this->calendarId, $event, $optParams); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function insertEvents(iterable $events, $optParams = []) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->enableBatch(true); |
107
|
|
|
|
108
|
|
|
collect($events) |
109
|
|
|
->each( |
110
|
|
|
function ($event, $batchIdentifier) use ($optParams) { |
111
|
|
|
$this->batchRequests->add($this->insertEvent($event, $optParams), "insert-".$batchIdentifier); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $event |
119
|
|
|
* @param array $optParams |
120
|
|
|
* |
121
|
|
|
* @return Google_Service_Calendar_Event|RequestInterface |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function updateEvent($event, $optParams = []) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
if ($event instanceof Event) { |
126
|
|
|
$event = $event->googleEvent; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->calendarService->events->update($this->calendarId, $event->id, $event, $optParams); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
public function updateEvents(iterable $events, $optParams = []) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->enableBatch(true); |
135
|
|
|
|
136
|
|
|
collect($events) |
137
|
|
|
->each( |
138
|
|
|
function ($event, $batchIdentifier) use ($optParams) { |
139
|
|
|
$this->batchRequests->add($this->updateEvent($event, $optParams), "update-".$batchIdentifier); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $eventId |
147
|
|
|
* @param array $optParams |
148
|
|
|
* |
149
|
|
|
* @return RequestInterface |
150
|
|
|
*/ |
151
|
|
|
public function deleteEvent($eventId, $optParams = []) |
152
|
|
|
{ |
153
|
|
|
if ($eventId instanceof Event) { |
154
|
|
|
$eventId = $eventId->id; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->calendarService->events->delete($this->calendarId, $eventId, $optParams); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param iterable $eventIds |
162
|
|
|
* @param array $optParams |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function deleteEvents(iterable $eventIds, $optParams = []) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->enableBatch(true); |
169
|
|
|
|
170
|
|
|
collect($eventIds) |
171
|
|
|
->each( |
172
|
|
|
function ($eventId, $batchIdentifier) use ($optParams) { |
173
|
|
|
$this->batchRequests->add($this->deleteEvent($eventId, $optParams), "delete-".$batchIdentifier); |
174
|
|
|
}); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getService(): Google_Service_Calendar |
180
|
|
|
{ |
181
|
|
|
return $this->calendarService; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function enableBatch(bool $bool) |
185
|
|
|
{ |
186
|
|
|
$this->calendarService->getClient()->setUseBatch($bool); |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function sendBatch():array |
192
|
|
|
{ |
193
|
|
|
return $this->batchRequests->execute(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.