Completed
Push — master ( 128382...6ef920 )
by Freek
02:04
created

Event::getFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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