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, $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 | * @return mixed |
||
51 | */ |
||
52 | public function __get($name) |
||
53 | { |
||
54 | $name = $this->translateFieldName($name); |
||
55 | |||
56 | if ($name === 'sortDate') { |
||
57 | return $this->getSortDate(); |
||
58 | } |
||
59 | |||
60 | $value = array_get($this->googleEvent, $name); |
||
61 | |||
62 | if (in_array($name, ['startDate', 'end.date']) && $value) { |
||
63 | $value = Carbon::createFromFormat('Y-m-d', $value); |
||
64 | } |
||
65 | |||
66 | if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
||
67 | $value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
||
68 | } |
||
69 | |||
70 | return $value; |
||
71 | } |
||
72 | |||
73 | public function __set($name, $value) |
||
74 | { |
||
75 | $name = $this->translateFieldName($name); |
||
76 | |||
77 | if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
||
78 | $this->setDateProperty($name, $value); |
||
79 | |||
80 | return; |
||
81 | } |
||
82 | |||
83 | array_set($this->googleEvent, $name, $value); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return bool |
||
88 | */ |
||
89 | public function exists() |
||
90 | { |
||
91 | return $this->id != ''; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function isAllDayEvent() |
||
98 | { |
||
99 | return is_null($this->googleEvent['start']['dateTime']); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param \Carbon\Carbon|null $startDateTime |
||
104 | * @param \Carbon\Carbon|null $endDateTime |
||
105 | * @param array $queryParameters |
||
106 | * @param string|null $calendarId |
||
107 | * |
||
108 | * @return \Illuminate\Support\Collection |
||
109 | */ |
||
110 | public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection |
||
111 | { |
||
112 | $googleCalendar = self::getGoogleCalendar($calendarId); |
||
113 | |||
114 | $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
||
115 | |||
116 | return collect($googleEvents) |
||
117 | ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
||
118 | return Event::createFromGoogleCalendarEvent($event, $calendarId); |
||
119 | }); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param string $id |
||
124 | * @param string $calendarId |
||
125 | * |
||
126 | * @return \Spatie\GoogleCalendar\Event |
||
127 | */ |
||
128 | public static function find($id, $calendarId = null) : Event |
||
129 | { |
||
130 | $googleCalendar = self::getGoogleCalendar($calendarId); |
||
131 | |||
132 | $googleEvent = $googleCalendar->getEvent($id); |
||
133 | |||
134 | return Event::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
||
0 ignored issues
–
show
|
|||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return mixed |
||
139 | */ |
||
140 | public function save() : Event |
||
141 | { |
||
142 | $method = $this->exists() ? 'updateEvent' : 'insertEvent'; |
||
143 | |||
144 | $googleCalendar = $this->getGoogleCalendar(); |
||
145 | |||
146 | $googleEvent = $googleCalendar->$method($this); |
||
147 | |||
148 | return Event::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $id |
||
153 | * |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function delete($id = null) |
||
157 | { |
||
158 | return $this->getGoogleCalendar($this->calendarId)->deleteEvent($id ?? $this->id); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param string $calendarId |
||
163 | * |
||
164 | * @return \Spatie\GoogleCalendar\GoogleCalendar |
||
165 | */ |
||
166 | protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar |
||
167 | { |
||
168 | $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id'); |
||
169 | |||
170 | return GoogleCalendarFactory::createForCalendarId($calendarId); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param string $name |
||
175 | * @param \Carbon\Carbon $date |
||
176 | */ |
||
177 | protected function setDateProperty($name, Carbon $date) |
||
178 | { |
||
179 | $eventDateTime = new Google_Service_Calendar_EventDateTime(); |
||
180 | |||
181 | if (in_array($name, ['start.date', 'end.date'])) { |
||
182 | $eventDateTime->setDate($date->format('Y-m-d')); |
||
183 | } |
||
184 | |||
185 | if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
||
186 | $eventDateTime->setDate($date->format(DateTime::RFC3339)); |
||
187 | } |
||
188 | |||
189 | if (starts_with($name, 'start')) { |
||
190 | $this->googleEvent->setStart($eventDateTime); |
||
191 | } |
||
192 | |||
193 | if (starts_with($name, 'end')) { |
||
194 | $this->googleEvent->setEnd($eventDateTime); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param $name |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | protected function translateFieldName($name) |
||
204 | { |
||
205 | if ($name === 'name') { |
||
206 | return 'summary'; |
||
207 | } |
||
208 | |||
209 | if ($name === 'startDate') { |
||
210 | return 'start.date'; |
||
211 | } |
||
212 | |||
213 | if ($name === 'endDate') { |
||
214 | return 'end.date'; |
||
215 | } |
||
216 | |||
217 | if ($name === 'startDateTime') { |
||
218 | return 'start.dateTime'; |
||
219 | } |
||
220 | |||
221 | if ($name === 'endDateTime') { |
||
222 | return 'end.dateTime'; |
||
223 | } |
||
224 | |||
225 | return $name; |
||
226 | } |
||
227 | |||
228 | public function getSortDate() : Carbon |
||
229 | { |
||
230 | if ($this->startDate) { |
||
231 | return $this->startDate; |
||
232 | } |
||
233 | |||
234 | return $this->startDateTime; |
||
235 | } |
||
236 | } |
||
237 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: