These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\GoogleCalendar; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Carbon\CarbonInterface; |
||
7 | use DateTime; |
||
8 | use Google_Service_Calendar_Event; |
||
9 | use Google_Service_Calendar_EventDateTime; |
||
10 | use Illuminate\Support\Arr; |
||
11 | use Illuminate\Support\Collection; |
||
12 | use Illuminate\Support\Str; |
||
13 | |||
14 | class Event |
||
15 | { |
||
16 | /** @var \Google_Service_Calendar_Event */ |
||
17 | public $googleEvent; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $calendarId; |
||
21 | |||
22 | /** @var array */ |
||
23 | protected $attendees; |
||
24 | |||
25 | public function __construct() |
||
26 | { |
||
27 | $this->attendees = []; |
||
28 | $this->googleEvent = new Google_Service_Calendar_Event; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param \Google_Service_Calendar_Event $googleEvent |
||
33 | * @param $calendarId |
||
34 | * |
||
35 | * @return static |
||
36 | */ |
||
37 | public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) |
||
38 | { |
||
39 | $event = new static; |
||
40 | |||
41 | $event->googleEvent = $googleEvent; |
||
42 | $event->calendarId = $calendarId; |
||
43 | |||
44 | return $event; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param array $properties |
||
49 | * @param string|null $calendarId |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public static function create(array $properties, string $calendarId = null, $optParams = []) |
||
54 | { |
||
55 | $event = new static; |
||
56 | |||
57 | $event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId(); |
||
58 | |||
59 | foreach ($properties as $name => $value) { |
||
60 | $event->$name = $value; |
||
61 | } |
||
62 | |||
63 | return $event->save('insertEvent', $optParams); |
||
64 | } |
||
65 | |||
66 | public static function quickCreate(string $text) |
||
67 | { |
||
68 | $event = new static; |
||
69 | |||
70 | $event->calendarId = static::getGoogleCalendar()->getCalendarId(); |
||
71 | |||
72 | return $event->quickSave($text); |
||
73 | } |
||
74 | |||
75 | public static function get(CarbonInterface $startDateTime = null, CarbonInterface $endDateTime = null, array $queryParameters = [], string $calendarId = null): Collection |
||
76 | { |
||
77 | $googleCalendar = static::getGoogleCalendar($calendarId); |
||
78 | |||
79 | $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
||
80 | |||
81 | $googleEventsList = $googleEvents->getItems(); |
||
82 | |||
83 | while ($googleEvents->getNextPageToken()) { |
||
84 | $queryParameters['pageToken'] = $googleEvents->getNextPageToken(); |
||
85 | |||
86 | $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
||
87 | |||
88 | $googleEventsList = array_merge($googleEventsList, $googleEvents->getItems()); |
||
89 | } |
||
90 | |||
91 | $useUserOrder = isset($queryParameters['orderBy']); |
||
92 | |||
93 | return collect($googleEventsList) |
||
94 | ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
||
95 | return static::createFromGoogleCalendarEvent($event, $calendarId); |
||
96 | }) |
||
97 | ->sortBy(function (self $event, $index) use ($useUserOrder) { |
||
98 | if ($useUserOrder) { |
||
99 | return $index; |
||
100 | } |
||
101 | |||
102 | return $event->sortDate; |
||
103 | }) |
||
104 | ->values(); |
||
105 | } |
||
106 | |||
107 | public static function find($eventId, string $calendarId = null): self |
||
108 | { |
||
109 | $googleCalendar = static::getGoogleCalendar($calendarId); |
||
110 | |||
111 | $googleEvent = $googleCalendar->getEvent($eventId); |
||
112 | |||
113 | return static::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
||
114 | } |
||
115 | |||
116 | public function __get($name) |
||
117 | { |
||
118 | $name = $this->getFieldName($name); |
||
119 | |||
120 | if ($name === 'sortDate') { |
||
121 | return $this->getSortDate(); |
||
122 | } |
||
123 | |||
124 | $value = Arr::get($this->googleEvent, $name); |
||
125 | |||
126 | if (in_array($name, ['start.date', 'end.date']) && $value) { |
||
127 | $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
||
128 | } |
||
129 | |||
130 | if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
||
131 | $value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
||
132 | } |
||
133 | |||
134 | return $value; |
||
135 | } |
||
136 | |||
137 | public function __set($name, $value) |
||
138 | { |
||
139 | $name = $this->getFieldName($name); |
||
140 | |||
141 | if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
||
142 | $this->setDateProperty($name, $value); |
||
143 | |||
144 | return; |
||
145 | } |
||
146 | |||
147 | Arr::set($this->googleEvent, $name, $value); |
||
148 | } |
||
149 | |||
150 | public function exists(): bool |
||
151 | { |
||
152 | return $this->id != ''; |
||
153 | } |
||
154 | |||
155 | public function isAllDayEvent(): bool |
||
156 | { |
||
157 | return is_null($this->googleEvent['start']['dateTime']); |
||
158 | } |
||
159 | |||
160 | public function save(string $method = null, $optParams = []): self |
||
161 | { |
||
162 | $method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent'); |
||
163 | |||
164 | $googleCalendar = $this->getGoogleCalendar($this->calendarId); |
||
165 | |||
166 | $this->googleEvent->setAttendees($this->attendees); |
||
167 | |||
168 | $googleEvent = $googleCalendar->$method($this, $optParams); |
||
169 | |||
170 | return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
||
171 | } |
||
172 | |||
173 | public function quickSave(string $text): self |
||
174 | { |
||
175 | $googleCalendar = $this->getGoogleCalendar($this->calendarId); |
||
176 | |||
177 | $googleEvent = $googleCalendar->insertEventFromText($text); |
||
178 | |||
179 | return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
||
180 | } |
||
181 | |||
182 | public function update(array $attributes, $optParams = []): self |
||
183 | { |
||
184 | foreach ($attributes as $name => $value) { |
||
185 | $this->$name = $value; |
||
186 | } |
||
187 | |||
188 | return $this->save('updateEvent', $optParams); |
||
189 | } |
||
190 | |||
191 | public function delete(string $eventId = null) |
||
192 | { |
||
193 | $this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id); |
||
194 | } |
||
195 | |||
196 | public function addAttendee(array $attendees) |
||
197 | { |
||
198 | $this->attendees[] = $attendees; |
||
199 | } |
||
200 | |||
201 | public function getSortDate(): string |
||
202 | { |
||
203 | if ($this->startDate) { |
||
204 | return $this->startDate; |
||
205 | } |
||
206 | |||
207 | if ($this->startDateTime) { |
||
208 | return $this->startDateTime; |
||
209 | } |
||
210 | |||
211 | return ''; |
||
212 | } |
||
213 | |||
214 | public function getCalendarId(): string |
||
215 | { |
||
216 | return $this->calendarId; |
||
217 | } |
||
218 | |||
219 | protected static function getGoogleCalendar(string $calendarId = null): GoogleCalendar |
||
220 | { |
||
221 | $calendarId = $calendarId ?? config('google-calendar.calendar_id'); |
||
222 | |||
223 | return GoogleCalendarFactory::createForCalendarId($calendarId); |
||
0 ignored issues
–
show
|
|||
224 | } |
||
225 | |||
226 | protected function setDateProperty(string $name, Carbon $date) |
||
227 | { |
||
228 | $eventDateTime = new Google_Service_Calendar_EventDateTime; |
||
229 | |||
230 | if (in_array($name, ['start.date', 'end.date'])) { |
||
231 | $eventDateTime->setDate($date->format('Y-m-d')); |
||
232 | $eventDateTime->setTimezone($date->getTimezone()); |
||
233 | } |
||
234 | |||
235 | if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
||
236 | $eventDateTime->setDateTime($date->format(DateTime::RFC3339)); |
||
237 | $eventDateTime->setTimezone($date->getTimezone()); |
||
238 | } |
||
239 | |||
240 | if (Str::startsWith($name, 'start')) { |
||
241 | $this->googleEvent->setStart($eventDateTime); |
||
242 | } |
||
243 | |||
244 | if (Str::startsWith($name, 'end')) { |
||
245 | $this->googleEvent->setEnd($eventDateTime); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | protected function getFieldName(string $name): string |
||
250 | { |
||
251 | return [ |
||
252 | 'name' => 'summary', |
||
253 | 'description' => 'description', |
||
254 | 'startDate' => 'start.date', |
||
255 | 'endDate' => 'end.date', |
||
256 | 'startDateTime' => 'start.dateTime', |
||
257 | 'endDateTime' => 'end.dateTime', |
||
258 | ][$name] ?? $name; |
||
259 | } |
||
260 | } |
||
261 |
This check looks for function calls that miss required arguments.