Completed
Push — master ( c4a284...c64a87 )
by Freek
07:27 queued 05:53
created

src/Event.php (1 issue)

Checks property assignments for possibly missing type casts

Bug Documentation Minor

Upgrade to new PHP Analysis Engine

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 int */
17
    protected $calendarId;
18
19
    protected $attendees;
20
21
    public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId)
22
    {
23
        $event = new static();
24
25
        $event->googleEvent = $googleEvent;
26
27
        $event->calendarId = $calendarId;
28
29
        return $event;
30
    }
31
32
    public static function create(array $properties, string $calendarId = null)
33
    {
34
        $event = new static();
35
36
        $event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId();
0 ignored issues
show
Documentation Bug introduced by
The property $calendarId was declared of type integer, but static::getGoogleCalenda...darId)->getCalendarId() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
37
38
        foreach ($properties as $name => $value) {
39
            $event->$name = $value;
40
        }
41
42
        return $event->save('insertEvent');
43
    }
44
45
    public function __construct()
46
    {
47
        $this->attendees = [];
48
        $this->googleEvent = new Google_Service_Calendar_Event();
49
    }
50
51
    /**
52
     * @param string $name
53
     *
54
     * @return mixed
55
     */
56
    public function __get($name)
57
    {
58
        $name = $this->getFieldName($name);
59
60
        if ($name === 'sortDate') {
61
            return $this->getSortDate();
62
        }
63
64
        $value = array_get($this->googleEvent, $name);
65
66
        if (in_array($name, ['start.date', 'end.date']) && $value) {
67
            $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
68
        }
69
70
        if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) {
71
            $value = Carbon::createFromFormat(DateTime::RFC3339, $value);
72
        }
73
74
        return $value;
75
    }
76
77
    public function __set($name, $value)
78
    {
79
        $name = $this->getFieldName($name);
80
81
        if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) {
82
            $this->setDateProperty($name, $value);
83
84
            return;
85
        }
86
87
        array_set($this->googleEvent, $name, $value);
88
    }
89
90
    public function exists(): bool
91
    {
92
        return $this->id != '';
93
    }
94
95
    public function isAllDayEvent(): bool
96
    {
97
        return is_null($this->googleEvent['start']['dateTime']);
98
    }
99
100
    /**
101
     * @param \Carbon\Carbon|null $startDateTime
102
     * @param \Carbon\Carbon|null $endDateTime
103
     * @param array               $queryParameters
104
     * @param string|null         $calendarId
105
     *
106
     * @return \Illuminate\Support\Collection
107
     */
108
    public static function get(
109
        Carbon $startDateTime = null,
110
        Carbon $endDateTime = null,
111
        array $queryParameters = [],
112
        string $calendarId = null
113
    ): Collection {
114
        $googleCalendar = static::getGoogleCalendar($calendarId);
115
116
        $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
117
118
        return collect($googleEvents)
119
            ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) {
120
                return static::createFromGoogleCalendarEvent($event, $calendarId);
121
            })
122
            ->sortBy(function (Event $event) {
123
                return $event->sortDate;
124
            })
125
            ->values();
126
    }
127
128
    /**
129
     * @param string $eventId
130
     * @param string $calendarId
131
     *
132
     * @return \Spatie\GoogleCalendar\Event
133
     */
134
    public static function find($eventId, $calendarId = null): Event
135
    {
136
        $googleCalendar = static::getGoogleCalendar($calendarId);
137
138
        $googleEvent = $googleCalendar->getEvent($eventId);
139
140
        return static::createFromGoogleCalendarEvent($googleEvent, $calendarId);
141
    }
142
143
    public function save($method = null): Event
144
    {
145
        $method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent');
146
147
        $googleCalendar = $this->getGoogleCalendar($this->calendarId);
148
149
        $this->googleEvent->setAttendees($this->attendees);
150
151
        $googleEvent = $googleCalendar->$method($this);
152
153
        return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId());
154
    }
155
156
    /**
157
     * @param string $eventId
158
     */
159
    public function delete(string $eventId = null)
160
    {
161
        $this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id);
162
    }
163
164
    /**
165
     * @param string $calendarId
166
     *
167
     * @return \Spatie\GoogleCalendar\GoogleCalendar
168
     */
169
    protected static function getGoogleCalendar($calendarId = null)
170
    {
171
        $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
172
173
        return GoogleCalendarFactory::createForCalendarId($calendarId);
174
    }
175
176
    /**
177
     * @param string         $name
178
     * @param \Carbon\Carbon $date
179
     */
180
    protected function setDateProperty(string $name, Carbon $date)
181
    {
182
        $eventDateTime = new Google_Service_Calendar_EventDateTime();
183
184
        if (in_array($name, ['start.date', 'end.date'])) {
185
            $eventDateTime->setDate($date->format('Y-m-d'));
186
            $eventDateTime->setTimezone($date->getTimezone());
187
        }
188
189
        if (in_array($name, ['start.dateTime', 'end.dateTime'])) {
190
            $eventDateTime->setDateTime($date->format(DateTime::RFC3339));
191
            $eventDateTime->setTimezone($date->getTimezone());
192
        }
193
194
        if (starts_with($name, 'start')) {
195
            $this->googleEvent->setStart($eventDateTime);
196
        }
197
198
        if (starts_with($name, 'end')) {
199
            $this->googleEvent->setEnd($eventDateTime);
200
        }
201
    }
202
203
    public function addAttendee(array $attendees)
204
    {
205
        $this->attendees[] = $attendees;
206
    }
207
208
    protected function getFieldName(string $name): string
209
    {
210
        return [
211
            'name'          => 'summary',
212
            'description'   => 'description',
213
            'startDate'     => 'start.date',
214
            'endDate'       => 'end.date',
215
            'startDateTime' => 'start.dateTime',
216
            'endDateTime'   => 'end.dateTime',
217
        ][$name] ?? $name;
218
    }
219
220
    public function getSortDate(): string
221
    {
222
        if ($this->startDate) {
223
            return $this->startDate;
224
        }
225
226
        if ($this->startDateTime) {
227
            return $this->startDateTime;
228
        }
229
230
        return '';
231
    }
232
}
233