Completed
Push — master ( c4a284...c64a87 )
by Freek
07:27 queued 05:53
created

GoogleCalendar::listEvents()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 3
1
<?php
2
3
namespace Spatie\GoogleCalendar;
4
5
use DateTime;
6
use Carbon\Carbon;
7
use Google_Service_Calendar;
8
use Google_Service_Calendar_Event;
9
10
class GoogleCalendar
11
{
12
    /** @var \Google_Service_Calendar */
13
    protected $calendarService;
14
15
    /** @var string */
16
    protected $calendarId;
17
18
    public function __construct(Google_Service_Calendar $calendarService, string $calendarId)
19
    {
20
        $this->calendarService = $calendarService;
21
22
        $this->calendarId = $calendarId;
23
    }
24
25
    public function getCalendarId(): string
26
    {
27
        return $this->calendarId;
28
    }
29
30
    /*
31
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
32
     */
33
    public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = []): array
34
    {
35
        $parameters = ['singleEvents' => true];
36
37
        if (is_null($startDateTime)) {
38
            $startDateTime = Carbon::now()->startOfDay();
39
        }
40
41
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
42
43
        if (is_null($endDateTime)) {
44
            $endDateTime = Carbon::now()->addYear()->endOfDay();
45
        }
46
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
47
48
        $parameters = array_merge($parameters, $queryParameters);
49
50
        return $this
51
            ->calendarService
52
            ->events
53
            ->listEvents($this->calendarId, $parameters)
54
            ->getItems();
55
    }
56
57
    public function getEvent(string $eventId): Google_Service_Calendar_Event
58
    {
59
        return $this->calendarService->events->get($this->calendarId, $eventId);
60
    }
61
62
    /*
63
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
64
     */
65 View Code Duplication
    public function insertEvent($event): Google_Service_Calendar_Event
0 ignored issues
show
Duplication introduced by freek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        if ($event instanceof Event) {
68
            $event = $event->googleEvent;
69
        }
70
71
        return $this->calendarService->events->insert($this->calendarId, $event);
72
    }
73
74 View Code Duplication
    public function updateEvent($event): Google_Service_Calendar_Event
0 ignored issues
show
Duplication introduced by freek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if ($event instanceof Event) {
77
            $event = $event->googleEvent;
78
        }
79
80
        return $this->calendarService->events->update($this->calendarId, $event->id, $event);
81
    }
82
83
    public function deleteEvent($eventId)
84
    {
85
        if ($eventId instanceof Event) {
86
            $eventId = $eventId->id;
0 ignored issues
show
Documentation introduced by freek
The property id does not exist on object<Spatie\GoogleCalendar\Event>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
87
        }
88
89
        $this->calendarService->events->delete($this->calendarId, $eventId);
90
    }
91
92
    public function getService(): Google_Service_Calendar
93
    {
94
        return $this->calendarService;
95
    }
96
}
97