Completed
Push — master ( ed2346...95b669 )
by Freek
01:17
created

Event   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 32
lcom 2
cbo 6
dl 0
loc 216
rs 9.6
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createFromGoogleCalendarEvent() 0 9 1
A create() 0 12 2
A get() 0 15 1
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 update() 0 8 2
A delete() 0 4 1
A addAttendee() 0 4 1
A getSortDate() 0 12 3
A getGoogleCalendar() 0 6 1
B setDateProperty() 0 22 5
A getFieldName() 0 11 1
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
        return collect($googleEvents)
70
            ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) {
71
                return static::createFromGoogleCalendarEvent($event, $calendarId);
72
            })
73
            ->sortBy(function (Event $event) {
74
                return $event->sortDate;
0 ignored issues
show
Documentation introduced by
The property sortDate 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...
75
            })
76
            ->values();
77
    }
78
79
    public static function find($eventId, string $calendarId = null): Event
80
    {
81
        $googleCalendar = static::getGoogleCalendar($calendarId);
82
83
        $googleEvent = $googleCalendar->getEvent($eventId);
84
85
        return static::createFromGoogleCalendarEvent($googleEvent, $calendarId);
86
    }
87
88
    public function __get($name)
89
    {
90
        $name = $this->getFieldName($name);
91
92
        if ($name === 'sortDate') {
93
            return $this->getSortDate();
94
        }
95
96
        $value = array_get($this->googleEvent, $name);
97
98
        if (in_array($name, ['start.date', 'end.date']) && $value) {
99
            $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
100
        }
101
102
        if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) {
103
            $value = Carbon::createFromFormat(DateTime::RFC3339, $value);
104
        }
105
106
        return $value;
107
    }
108
109
    public function __set($name, $value)
110
    {
111
        $name = $this->getFieldName($name);
112
113
        if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) {
114
            $this->setDateProperty($name, $value);
115
116
            return;
117
        }
118
119
        array_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...
120
    }
121
122
    public function exists(): bool
123
    {
124
        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...
125
    }
126
127
    public function isAllDayEvent(): bool
128
    {
129
        return is_null($this->googleEvent['start']['dateTime']);
130
    }
131
132
    public function save(string $method = null): Event
133
    {
134
        $method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent');
135
136
        $googleCalendar = $this->getGoogleCalendar($this->calendarId);
137
138
        $this->googleEvent->setAttendees($this->attendees);
139
140
        $googleEvent = $googleCalendar->$method($this);
141
142
        return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId());
143
    }
144
145
    /**
146
     * @param array $properties
147
     *
148
     * @return Event
149
     */
150
    public function update(array $properties): Event
151
    {
152
        foreach ($properties as $name => $value) {
153
            $this->$name = $value;
154
        }
155
156
        return $this->save('updateEvent');
157
    }
158
159
    /**
160
     * @param string $eventId
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
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
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
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
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
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
    protected static function getGoogleCalendar(string $calendarId = null): GoogleCalendar
186
    {
187
        $calendarId = $calendarId ?? config('google-calendar.calendar_id');
188
189
        return GoogleCalendarFactory::createForCalendarId($calendarId);
190
    }
191
192
    protected function setDateProperty(string $name, Carbon $date)
193
    {
194
        $eventDateTime = new Google_Service_Calendar_EventDateTime;
195
196
        if (in_array($name, ['start.date', 'end.date'])) {
197
            $eventDateTime->setDate($date->format('Y-m-d'));
198
            $eventDateTime->setTimezone($date->getTimezone());
199
        }
200
201
        if (in_array($name, ['start.dateTime', 'end.dateTime'])) {
202
            $eventDateTime->setDateTime($date->format(DateTime::RFC3339));
203
            $eventDateTime->setTimezone($date->getTimezone());
204
        }
205
206
        if (starts_with($name, 'start')) {
207
            $this->googleEvent->setStart($eventDateTime);
208
        }
209
210
        if (starts_with($name, 'end')) {
211
            $this->googleEvent->setEnd($eventDateTime);
212
        }
213
    }
214
215
    protected function getFieldName(string $name): string
216
    {
217
        return [
218
                   'name' => 'summary',
219
                   'description' => 'description',
220
                   'startDate' => 'start.date',
221
                   'endDate' => 'end.date',
222
                   'startDateTime' => 'start.dateTime',
223
                   'endDateTime' => 'end.dateTime',
224
               ][$name] ?? $name;
225
    }
226
}
227