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 DateTime; |
||
7 | use Google_Service_Calendar_Event; |
||
8 | use Google_Service_Calendar_EventDateTime; |
||
9 | use Illuminate\Support\Collection; |
||
10 | |||
11 | class Event |
||
12 | { |
||
13 | /** @var Google_Service_Calendar_Event */ |
||
14 | public $googleEvent; |
||
15 | |||
16 | /** @var int */ |
||
17 | protected $calendarId; |
||
18 | |||
19 | public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) |
||
20 | { |
||
21 | $event = new static(); |
||
22 | |||
23 | $event->googleEvent = $googleEvent; |
||
24 | |||
25 | $event->calendarId = $calendarId; |
||
26 | |||
27 | return $event; |
||
28 | } |
||
29 | |||
30 | public static function create(array $properties, string $calendarId = null) |
||
31 | { |
||
32 | $event = new static(); |
||
33 | |||
34 | $event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId(); |
||
35 | |||
36 | foreach ($properties as $name => $value) { |
||
37 | $event->$name = $value; |
||
38 | } |
||
39 | |||
40 | return $event->save(); |
||
41 | } |
||
42 | |||
43 | public function __construct() |
||
44 | { |
||
45 | $this->googleEvent = new Google_Service_Calendar_Event(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $name |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function __get($name) |
||
54 | { |
||
55 | $name = $this->translateFieldName($name); |
||
56 | |||
57 | if ($name === 'sortDate') { |
||
58 | return $this->getSortDate(); |
||
59 | } |
||
60 | |||
61 | $value = array_get($this->googleEvent, $name); |
||
62 | |||
63 | if (in_array($name, ['start.date', 'end.date']) && $value) { |
||
64 | $value = Carbon::createFromFormat('Y-m-d', $value); |
||
65 | } |
||
66 | |||
67 | if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
||
68 | $value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
||
69 | } |
||
70 | |||
71 | return $value; |
||
72 | } |
||
73 | |||
74 | public function __set($name, $value) |
||
75 | { |
||
76 | $name = $this->translateFieldName($name); |
||
77 | |||
78 | if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
||
79 | $this->setDateProperty($name, $value); |
||
80 | |||
81 | return; |
||
82 | } |
||
83 | |||
84 | array_set($this->googleEvent, $name, $value); |
||
85 | } |
||
86 | |||
87 | public function exists() : bool |
||
88 | { |
||
89 | return $this->id != ''; |
||
90 | } |
||
91 | |||
92 | public function isAllDayEvent() : bool |
||
93 | { |
||
94 | return is_null($this->googleEvent['start']['dateTime']); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param \Carbon\Carbon|null $startDateTime |
||
99 | * @param \Carbon\Carbon|null $endDateTime |
||
100 | * @param array $queryParameters |
||
101 | * @param string|null $calendarId |
||
102 | * |
||
103 | * @return \Illuminate\Support\Collection |
||
104 | */ |
||
105 | public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection |
||
106 | { |
||
107 | $googleCalendar = self::getGoogleCalendar($calendarId); |
||
108 | |||
109 | $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
||
110 | |||
111 | return collect($googleEvents) |
||
112 | ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
||
113 | return Event::createFromGoogleCalendarEvent($event, $calendarId); |
||
114 | }) |
||
115 | ->sortBy(function (Event $event) { |
||
116 | return $event->sortDate; |
||
117 | }) |
||
118 | ->values(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $eventId |
||
123 | * @param string $calendarId |
||
124 | * |
||
125 | * @return \Spatie\GoogleCalendar\Event |
||
126 | */ |
||
127 | public static function find($eventId, $calendarId = null) : Event |
||
128 | { |
||
129 | $googleCalendar = self::getGoogleCalendar($calendarId); |
||
130 | |||
131 | $googleEvent = $googleCalendar->getEvent($eventId); |
||
132 | |||
133 | return self::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
||
134 | } |
||
135 | |||
136 | public function save() : Event |
||
137 | { |
||
138 | $method = $this->exists() ? 'updateEvent' : 'insertEvent'; |
||
139 | |||
140 | $googleCalendar = $this->getGoogleCalendar(); |
||
141 | |||
142 | $googleEvent = $googleCalendar->$method($this); |
||
143 | |||
144 | return self::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param string $eventId |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function delete(string $eventId = null) |
||
153 | { |
||
154 | return $this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param string $calendarId |
||
159 | * |
||
160 | * @return \Spatie\GoogleCalendar\GoogleCalendar |
||
161 | */ |
||
162 | protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar |
||
163 | { |
||
164 | $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id'); |
||
165 | |||
166 | return GoogleCalendarFactory::createForCalendarId($calendarId); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param string $name |
||
171 | * @param \Carbon\Carbon $date |
||
172 | */ |
||
173 | protected function setDateProperty(string $name, Carbon $date) |
||
174 | { |
||
175 | $eventDateTime = new Google_Service_Calendar_EventDateTime(); |
||
176 | |||
177 | if (in_array($name, ['start.date', 'end.date'])) { |
||
178 | $eventDateTime->setDate($date->format('Y-m-d')); |
||
179 | } |
||
180 | |||
181 | if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
||
182 | $eventDateTime->setDateTime($date->format(DateTime::RFC3339)); |
||
183 | } |
||
184 | |||
185 | if (starts_with($name, 'start')) { |
||
186 | $this->googleEvent->setStart($eventDateTime); |
||
187 | } |
||
188 | |||
189 | if (starts_with($name, 'end')) { |
||
190 | $this->googleEvent->setEnd($eventDateTime); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | protected function translateFieldName(string $name) : string |
||
195 | { |
||
196 | $translations = [ |
||
197 | 'name' => 'summary', |
||
198 | 'startDate' => 'start.date', |
||
199 | 'endDate' => 'end.date', |
||
200 | 'startDateTime' => 'start.dateTime', |
||
201 | 'endDateTime' => 'end.dateTime', |
||
202 | ]; |
||
203 | |||
204 | return $translations[$name] ?? $name; |
||
205 | } |
||
206 | |||
207 | public function getSortDate() |
||
208 | { |
||
209 | if ($this->startDate) { |
||
210 | return $this->startDate; |
||
211 | } |
||
212 | |||
213 | if ($this->startDateTime) { |
||
0 ignored issues
–
show
|
|||
214 | return $this->startDateTime; |
||
215 | } |
||
216 | |||
217 | return; |
||
218 | } |
||
219 | } |
||
220 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.