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 DateTime; |
||
6 | use Carbon\Carbon; |
||
7 | use Google_Service_Calendar_Event; |
||
8 | use Illuminate\Support\Collection; |
||
9 | use Google_Service_Calendar_EventDateTime; |
||
10 | |||
11 | class Event |
||
12 | { |
||
13 | /** @var \Google_Service_Calendar_Event */ |
||
14 | public $googleEvent; |
||
15 | |||
16 | /** @var string */ |
||
17 | protected $calendarId; |
||
18 | |||
19 | /** @var array */ |
||
20 | protected $attendees; |
||
21 | |||
22 | public function __construct() |
||
23 | { |
||
24 | $this->attendees = []; |
||
25 | $this->googleEvent = new Google_Service_Calendar_Event; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param \Google_Service_Calendar_Event $googleEvent |
||
30 | * @param $calendarId |
||
31 | * |
||
32 | * @return static |
||
33 | */ |
||
34 | public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) |
||
35 | { |
||
36 | $event = new static; |
||
37 | |||
38 | $event->googleEvent = $googleEvent; |
||
39 | $event->calendarId = $calendarId; |
||
40 | |||
41 | return $event; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param array $properties |
||
46 | * @param string|null $calendarId |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public static function create(array $properties, string $calendarId = null) |
||
51 | { |
||
52 | $event = new static; |
||
53 | |||
54 | $event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId(); |
||
55 | |||
56 | foreach ($properties as $name => $value) { |
||
57 | $event->$name = $value; |
||
58 | } |
||
59 | |||
60 | return $event->save('insertEvent'); |
||
61 | } |
||
62 | |||
63 | public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection |
||
64 | { |
||
65 | $googleCalendar = static::getGoogleCalendar($calendarId); |
||
66 | |||
67 | $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
||
68 | |||
69 | $useUserOrder = isset($queryParameters['orderBy']); |
||
70 | |||
71 | return collect($googleEvents) |
||
72 | ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
||
73 | return static::createFromGoogleCalendarEvent($event, $calendarId); |
||
74 | }) |
||
75 | ->sortBy(function (Event $event, $index) use ($useUserOrder) { |
||
76 | if ($useUserOrder) { |
||
77 | return $index; |
||
78 | } |
||
79 | |||
80 | return $event->sortDate; |
||
0 ignored issues
–
show
|
|||
81 | }) |
||
82 | ->values(); |
||
83 | } |
||
84 | |||
85 | public static function find($eventId, string $calendarId = null): Event |
||
86 | { |
||
87 | $googleCalendar = static::getGoogleCalendar($calendarId); |
||
88 | |||
89 | $googleEvent = $googleCalendar->getEvent($eventId); |
||
90 | |||
91 | return static::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
||
92 | } |
||
93 | |||
94 | public function __get($name) |
||
95 | { |
||
96 | $name = $this->getFieldName($name); |
||
97 | |||
98 | if ($name === 'sortDate') { |
||
99 | return $this->getSortDate(); |
||
100 | } |
||
101 | |||
102 | $value = array_get($this->googleEvent, $name); |
||
103 | |||
104 | if (in_array($name, ['start.date', 'end.date']) && $value) { |
||
105 | $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
||
106 | } |
||
107 | |||
108 | if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
||
109 | $value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
||
110 | } |
||
111 | |||
112 | return $value; |
||
113 | } |
||
114 | |||
115 | public function __set($name, $value) |
||
116 | { |
||
117 | $name = $this->getFieldName($name); |
||
118 | |||
119 | if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
||
120 | $this->setDateProperty($name, $value); |
||
121 | |||
122 | return; |
||
123 | } |
||
124 | |||
125 | array_set($this->googleEvent, $name, $value); |
||
126 | } |
||
127 | |||
128 | public function exists(): bool |
||
129 | { |
||
130 | return $this->id != ''; |
||
0 ignored issues
–
show
The property
id does not exist on object<Spatie\GoogleCalendar\Event> . Since you implemented __get , maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
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.
Loading history...
|
|||
131 | } |
||
132 | |||
133 | public function isAllDayEvent(): bool |
||
134 | { |
||
135 | return is_null($this->googleEvent['start']['dateTime']); |
||
136 | } |
||
137 | |||
138 | public function save(string $method = null): Event |
||
139 | { |
||
140 | $method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent'); |
||
141 | |||
142 | $googleCalendar = $this->getGoogleCalendar($this->calendarId); |
||
143 | |||
144 | $this->googleEvent->setAttendees($this->attendees); |
||
145 | |||
146 | $googleEvent = $googleCalendar->$method($this); |
||
147 | |||
148 | return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
||
149 | } |
||
150 | |||
151 | public function update(array $attributes): Event |
||
152 | { |
||
153 | foreach ($attributes as $name => $value) { |
||
154 | $this->$name = $value; |
||
155 | } |
||
156 | |||
157 | return $this->save('updateEvent'); |
||
158 | } |
||
159 | |||
160 | public function delete(string $eventId = null) |
||
161 | { |
||
162 | $this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id); |
||
163 | } |
||
164 | |||
165 | public function addAttendee(array $attendees) |
||
166 | { |
||
167 | $this->attendees[] = $attendees; |
||
168 | } |
||
169 | |||
170 | public function getSortDate(): string |
||
171 | { |
||
172 | if ($this->startDate) { |
||
173 | return $this->startDate; |
||
174 | } |
||
175 | |||
176 | if ($this->startDateTime) { |
||
177 | return $this->startDateTime; |
||
178 | } |
||
179 | |||
180 | return ''; |
||
181 | } |
||
182 | |||
183 | protected static function getGoogleCalendar(string $calendarId = null): GoogleCalendar |
||
184 | { |
||
185 | $calendarId = $calendarId ?? config('google-calendar.calendar_id'); |
||
186 | |||
187 | return GoogleCalendarFactory::createForCalendarId($calendarId); |
||
188 | } |
||
189 | |||
190 | protected function setDateProperty(string $name, Carbon $date) |
||
191 | { |
||
192 | $eventDateTime = new Google_Service_Calendar_EventDateTime; |
||
193 | |||
194 | if (in_array($name, ['start.date', 'end.date'])) { |
||
195 | $eventDateTime->setDate($date->format('Y-m-d')); |
||
196 | $eventDateTime->setTimezone($date->getTimezone()); |
||
197 | } |
||
198 | |||
199 | if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
||
200 | $eventDateTime->setDateTime($date->format(DateTime::RFC3339)); |
||
201 | $eventDateTime->setTimezone($date->getTimezone()); |
||
202 | } |
||
203 | |||
204 | if (starts_with($name, 'start')) { |
||
205 | $this->googleEvent->setStart($eventDateTime); |
||
206 | } |
||
207 | |||
208 | if (starts_with($name, 'end')) { |
||
209 | $this->googleEvent->setEnd($eventDateTime); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | protected function getFieldName(string $name): string |
||
214 | { |
||
215 | return [ |
||
216 | 'name' => 'summary', |
||
217 | 'description' => 'description', |
||
218 | 'startDate' => 'start.date', |
||
219 | 'endDate' => 'end.date', |
||
220 | 'startDateTime' => 'start.dateTime', |
||
221 | 'endDateTime' => 'end.dateTime', |
||
222 | ][$name] ?? $name; |
||
223 | } |
||
224 | } |
||
225 |
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.