Completed
Push — master ( d2257e...bd294a )
by Freek
02:08
created

Event   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 28
lcom 1
cbo 6
dl 0
loc 203
rs 10

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 14 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
A translateFieldName() 0 12 1
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, string $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, ['start.date', '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
    public function exists() : bool
87
    {
88
        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...
89
    }
90
91
    public function isAllDayEvent() : bool
92
    {
93
        return is_null($this->googleEvent['start']['dateTime']);
94
    }
95
96
    /**
97
     * @param \Carbon\Carbon|null $startDateTime
98
     * @param \Carbon\Carbon|null $endDateTime
99
     * @param array $queryParameters
100
     * @param string|null $calendarId
101
     *
102
     * @return \Illuminate\Support\Collection
103
     */
104
    public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection
105
    {
106
        $googleCalendar = self::getGoogleCalendar($calendarId);
107
108
        $googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
109
110
        return collect($googleEvents)
111
            ->map(function (Google_Service_Calendar_Event $event) use ($calendarId) {
112
                return Event::createFromGoogleCalendarEvent($event, $calendarId);
113
            })
114
            ->sortBy(function(Event $event) {
115
                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...
116
            });
117
    }
118
119
    /**
120
     * @param string $eventId
121
     * @param string $calendarId
122
     *
123
     * @return \Spatie\GoogleCalendar\Event
124
     */
125
    public static function find($eventId, $calendarId = null) : Event
126
    {
127
        $googleCalendar = self::getGoogleCalendar($calendarId);
128
129
        $googleEvent = $googleCalendar->getEvent($eventId);
130
        
131
        return Event::createFromGoogleCalendarEvent($googleEvent, $calendarId);
132
    }
133
134
    public function save() : Event
135
    {
136
        $method = $this->exists() ? 'updateEvent' : 'insertEvent';
137
138
        $googleCalendar = $this->getGoogleCalendar();
139
140
        $googleEvent =  $googleCalendar->$method($this);
141
        
142
        return Event::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId());
143
    }
144
145
    /**
146
     * @param string $eventId
147
     *
148
     * @return mixed
149
     */
150
    public function delete(string $eventId = null)
151
    {
152
        return $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...
153
    }
154
155
    /**
156
     * @param string $calendarId
157
     *
158
     * @return \Spatie\GoogleCalendar\GoogleCalendar
159
     */
160
    protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar
161
    {
162
        $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
163
164
        return GoogleCalendarFactory::createForCalendarId($calendarId);
165
    }
166
167
    /**
168
     * @param string $name
169
     * @param \Carbon\Carbon $date
170
     */
171
    protected function setDateProperty(string $name, Carbon $date)
172
    {
173
        $eventDateTime = new Google_Service_Calendar_EventDateTime();
174
175
        if (in_array($name, ['start.date', 'end.date'])) {
176
            $eventDateTime->setDate($date->format('Y-m-d'));
177
        }
178
179
        if (in_array($name, ['start.dateTime', 'end.dateTime'])) {
180
            $eventDateTime->setDate($date->format(DateTime::RFC3339));
181
        }
182
183
        if (starts_with($name, 'start')) {
184
            $this->googleEvent->setStart($eventDateTime);
185
        }
186
187
        if (starts_with($name, 'end')) {
188
            $this->googleEvent->setEnd($eventDateTime);
189
        }
190
    }
191
192
    protected function translateFieldName(string $name) : string
193
    {
194
        $translations = [
195
            'name' => 'summary',
196
            'startDate' => 'start.date',
197
            'endDate' => 'end.date',
198
            'startDateTime' => 'start.dateTime',
199
            'endDateTime' => 'endDateTime',
200
        ];
201
202
        return $translations[$name] ?? $name;
203
    }
204
205
    public function getSortDate() : Carbon
206
    {
207
        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...
208
            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...
209
        }
210
211
        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...
212
    }
213
}
214