Completed
Push — master ( 95d481...0401d4 )
by Freek
01:07
created

Event::getCalendarId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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