Completed
Push — master ( c70706...03cd9a )
by Sebastian
06:17 queued 03:59
created

Event::addAttendee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 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 freek
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);
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...
88
    }
89
90
    public function exists(): bool
91
    {
92
        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...
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;
0 ignored issues
show
Documentation introduced by freek
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...
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);
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...
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) {
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...
223
            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...
224
        }
225
226
        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...
227
            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...
228
        }
229
230
        return '';
231
    }
232
}
233