Completed
Pull Request — master (#147)
by
unknown
01:19
created

Event   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 36
lcom 2
cbo 9
dl 0
loc 238
rs 9.52
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createFromGoogleCalendarEvent() 0 9 1
A create() 0 12 2
A get() 0 31 3
A find() 0 8 1
B __get() 0 20 6
A __set() 0 12 2
A exists() 0 4 1
A isAllDayEvent() 0 4 1
A save() 0 12 2
A quickSave() 0 8 1
A update() 0 8 2
A delete() 0 4 1
A addAttendee() 0 4 1
A getSortDate() 0 12 3
A getCalendarId() 0 4 1
A getGoogleCalendar() 0 6 1
A setDateProperty() 0 22 5
A getFieldName() 0 11 1
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 get(CarbonInterface $startDateTime = null, CarbonInterface $endDateTime = null, array $queryParameters = [], string $calendarId = null): Collection
67
    {
68
        $googleCalendar = static::getGoogleCalendar($calendarId);
69
70
        $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
71
72
        $googleEventsList = $googleEvents->getItems();
73
74
        while ($googleEvents->getNextPageToken()) {
75
            $queryParameters['pageToken'] = $googleEvents->getNextPageToken();
76
77
            $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
78
79
            $googleEventsList = array_merge($googleEventsList, $googleEvents->getItems());
80
        }
81
82
        $useUserOrder = isset($queryParameters['orderBy']);
83
84
        return collect($googleEventsList)
85
            ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) {
86
                return static::createFromGoogleCalendarEvent($event, $calendarId);
87
            })
88
            ->sortBy(function (self $event, $index) use ($useUserOrder) {
89
                if ($useUserOrder) {
90
                    return $index;
91
                }
92
93
                return $event->sortDate;
94
            })
95
            ->values();
96
    }
97
98
    public static function find($eventId, string $calendarId = null): self
99
    {
100
        $googleCalendar = static::getGoogleCalendar($calendarId);
101
102
        $googleEvent = $googleCalendar->getEvent($eventId);
103
104
        return static::createFromGoogleCalendarEvent($googleEvent, $calendarId);
105
    }
106
107
    public function __get($name)
108
    {
109
        $name = $this->getFieldName($name);
110
111
        if ($name === 'sortDate') {
112
            return $this->getSortDate();
113
        }
114
115
        $value = Arr::get($this->googleEvent, $name);
116
117
        if (in_array($name, ['start.date', 'end.date']) && $value) {
118
            $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
119
        }
120
121
        if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) {
122
            $value = Carbon::createFromFormat(DateTime::RFC3339, $value);
123
        }
124
125
        return $value;
126
    }
127
128
    public function __set($name, $value)
129
    {
130
        $name = $this->getFieldName($name);
131
132
        if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) {
133
            $this->setDateProperty($name, $value);
134
135
            return;
136
        }
137
138
        Arr::set($this->googleEvent, $name, $value);
0 ignored issues
show
Documentation introduced by
$this->googleEvent is of type object<Google_Service_Calendar_Event>, but the function expects a array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
    }
140
141
    public function exists(): bool
142
    {
143
        return $this->id != '';
0 ignored issues
show
Documentation introduced by
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 _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.

<?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...
144
    }
145
146
    public function isAllDayEvent(): bool
147
    {
148
        return is_null($this->googleEvent['start']['dateTime']);
149
    }
150
151
    public function save(string $method = null, $optParams = []): self
152
    {
153
        $method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent');
154
155
        $googleCalendar = $this->getGoogleCalendar($this->calendarId);
156
157
        $this->googleEvent->setAttendees($this->attendees);
158
159
        $googleEvent = $googleCalendar->$method($this, $optParams);
160
161
        return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId());
162
    }
163
164
    public function quickSave(string $event): self
165
    {
166
        $googleCalendar = $this->getGoogleCalendar($this->calendarId);
167
168
        $googleEvent = $googleCalendar->insertEventFromText($event);
169
170
        return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId());
171
    }
172
173
    public function update(array $attributes, $optParams = []): self
174
    {
175
        foreach ($attributes as $name => $value) {
176
            $this->$name = $value;
177
        }
178
179
        return $this->save('updateEvent', $optParams);
180
    }
181
182
    public function delete(string $eventId = null)
183
    {
184
        $this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id);
0 ignored issues
show
Documentation introduced by
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 _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.

<?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...
185
    }
186
187
    public function addAttendee(array $attendees)
188
    {
189
        $this->attendees[] = $attendees;
190
    }
191
192
    public function getSortDate(): string
193
    {
194
        if ($this->startDate) {
0 ignored issues
show
Documentation introduced by
The property startDate 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 _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.

<?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...
195
            return $this->startDate;
0 ignored issues
show
Documentation introduced by
The property startDate 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 _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.

<?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...
196
        }
197
198
        if ($this->startDateTime) {
0 ignored issues
show
Documentation introduced by
The property startDateTime 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 _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.

<?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...
199
            return $this->startDateTime;
0 ignored issues
show
Documentation introduced by
The property startDateTime 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 _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.

<?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...
200
        }
201
202
        return '';
203
    }
204
205
    public function getCalendarId(): string
206
    {
207
        return $this->calendarId;
208
    }
209
210
    protected static function getGoogleCalendar(string $calendarId = null): GoogleCalendar
211
    {
212
        $calendarId = $calendarId ?? config('google-calendar.calendar_id');
213
214
        return GoogleCalendarFactory::createForCalendarId($calendarId);
215
    }
216
217
    protected function setDateProperty(string $name, Carbon $date)
218
    {
219
        $eventDateTime = new Google_Service_Calendar_EventDateTime;
220
221
        if (in_array($name, ['start.date', 'end.date'])) {
222
            $eventDateTime->setDate($date->format('Y-m-d'));
223
            $eventDateTime->setTimezone($date->getTimezone());
224
        }
225
226
        if (in_array($name, ['start.dateTime', 'end.dateTime'])) {
227
            $eventDateTime->setDateTime($date->format(DateTime::RFC3339));
228
            $eventDateTime->setTimezone($date->getTimezone());
229
        }
230
231
        if (Str::startsWith($name, 'start')) {
232
            $this->googleEvent->setStart($eventDateTime);
233
        }
234
235
        if (Str::startsWith($name, 'end')) {
236
            $this->googleEvent->setEnd($eventDateTime);
237
        }
238
    }
239
240
    protected function getFieldName(string $name): string
241
    {
242
        return [
243
            'name' => 'summary',
244
            'description' => 'description',
245
            'startDate' => 'start.date',
246
            'endDate' => 'end.date',
247
            'startDateTime' => 'start.dateTime',
248
            'endDateTime' => 'end.dateTime',
249
        ][$name] ?? $name;
250
    }
251
}
252