Completed
Push — master ( 3d895d...d2257e )
by Freek
02:06
created

Event   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 33
lcom 1
cbo 6
dl 0
loc 226
rs 9.3999

15 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromGoogleCalendarEvent() 0 10 1
A create() 0 12 2
A __construct() 0 4 1
B __get() 0 20 6
A __set() 0 12 2
A exists() 0 4 1
A isAllDayEvent() 0 4 1
A get() 0 11 1
A find() 0 8 1
A save() 0 10 2
A delete() 0 4 1
A getGoogleCalendar() 0 6 1
B setDateProperty() 0 20 5
B translateFieldName() 0 24 6
A getSortDate() 0 8 2
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, $calendarId = null)
31
    {
32
        $event = new static;
33
34
        $event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId();
0 ignored issues
show
Documentation Bug introduced by
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
     * @return mixed
51
     */
52
    public function __get($name)
53
    {
54
        $name = $this->translateFieldName($name);
55
56
        if ($name === 'sortDate') {
57
            return $this->getSortDate();
58
        }
59
60
        $value = array_get($this->googleEvent, $name);
61
62
        if (in_array($name, ['startDate', 'end.date']) && $value) {
63
            $value = Carbon::createFromFormat('Y-m-d', $value);
64
        }
65
66
        if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) {
67
            $value = Carbon::createFromFormat(DateTime::RFC3339, $value);
68
        }
69
70
        return $value;
71
    }
72
73
    public function __set($name, $value)
74
    {
75
        $name = $this->translateFieldName($name);
76
77
        if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) {
78
            $this->setDateProperty($name, $value);
79
80
            return;
81
        }
82
83
        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...
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function exists()
90
    {
91
        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...
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function isAllDayEvent()
98
    {
99
        return is_null($this->googleEvent['start']['dateTime']);
100
    }
101
102
    /**
103
     * @param \Carbon\Carbon|null $startDateTime
104
     * @param \Carbon\Carbon|null $endDateTime
105
     * @param array $queryParameters
106
     * @param string|null $calendarId
107
     *
108
     * @return \Illuminate\Support\Collection
109
     */
110
    public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection
111
    {
112
        $googleCalendar = self::getGoogleCalendar($calendarId);
113
114
        $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
115
116
        return collect($googleEvents)
117
            ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) {
118
                return Event::createFromGoogleCalendarEvent($event, $calendarId);
119
            });
120
    }
121
122
    /**
123
     * @param string $id
124
     * @param string $calendarId
125
     *
126
     * @return \Spatie\GoogleCalendar\Event
127
     */
128
    public static function find($id, $calendarId = null) : Event
129
    {
130
        $googleCalendar = self::getGoogleCalendar($calendarId);
131
132
        $googleEvent = $googleCalendar->getEvent($id);
133
        
134
        return Event::createFromGoogleCalendarEvent($googleEvent, $calendarId);
0 ignored issues
show
Documentation introduced by
$googleEvent is of type object<Spatie\GoogleCalendar\GoogleCalendar>, but the function expects a object<Google_Service_Calendar_Event>.

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...
135
    }
136
137
    /**
138
     * @return mixed
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 Event::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId());
149
    }
150
151
    /**
152
     * @param string $id
153
     *
154
     * @return mixed
155
     */
156
    public function delete($id = null)
157
    {
158
        return $this->getGoogleCalendar($this->calendarId)->deleteEvent($id ?? $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...
159
    }
160
161
    /**
162
     * @param string $calendarId
163
     *
164
     * @return \Spatie\GoogleCalendar\GoogleCalendar
165
     */
166
    protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar
167
    {
168
        $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
169
170
        return GoogleCalendarFactory::createForCalendarId($calendarId);
171
    }
172
173
    /**
174
     * @param string $name
175
     * @param \Carbon\Carbon $date
176
     */
177
    protected function setDateProperty($name, Carbon $date)
178
    {
179
        $eventDateTime = new Google_Service_Calendar_EventDateTime();
180
181
        if (in_array($name, ['start.date', 'end.date'])) {
182
            $eventDateTime->setDate($date->format('Y-m-d'));
183
        }
184
185
        if (in_array($name, ['start.dateTime', 'end.dateTime'])) {
186
            $eventDateTime->setDate($date->format(DateTime::RFC3339));
187
        }
188
189
        if (starts_with($name, 'start')) {
190
            $this->googleEvent->setStart($eventDateTime);
191
        }
192
193
        if (starts_with($name, 'end')) {
194
            $this->googleEvent->setEnd($eventDateTime);
195
        }
196
    }
197
198
    /**
199
     * @param $name
200
     *
201
     * @return string
202
     */
203
    protected function translateFieldName($name)
204
    {
205
        if ($name === 'name') {
206
            return 'summary';
207
        }
208
209
        if ($name === 'startDate') {
210
            return 'start.date';
211
        }
212
213
        if ($name === 'endDate') {
214
            return 'end.date';
215
        }
216
217
        if ($name === 'startDateTime') {
218
            return 'start.dateTime';
219
        }
220
221
        if ($name === 'endDateTime') {
222
            return 'end.dateTime';
223
        }
224
225
        return $name;
226
    }
227
228
    public function getSortDate() : Carbon
229
    {
230
        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...
231
            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...
232
        }
233
234
        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...
235
    }
236
}
237