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

GoogleCalendar::listEvents()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 27
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 18
nc 4
nop 3
1
<?php
2
3
namespace Spatie\GoogleCalendar;
4
5
use Carbon\Carbon;
6
use DateTime;
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, $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
     * @param \Carbon\Carbon $startDateTime
32
     * @param \Carbon\Carbon $endDateTime
33
     * @param array  $queryParameters
34
     *
35
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
36
     *
37
     * @return array
38
     */
39
    public function listEvents(
40
        Carbon $startDateTime = null,
41
        Carbon $endDateTime = null,
42
        array $queryParameters = []
43
    ): array 
44
    {
45
        $parameters = ['singleEvents' => true];
46
47
        if (is_null($startDateTime)) {
48
            $startDateTime = Carbon::now()->startOfDay();
49
        }
50
51
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
52
53
        if (is_null($endDateTime)) {
54
            $endDateTime = Carbon::now()->addYear()->endOfDay();
55
        }
56
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
57
58
        $parameters = array_merge($parameters, $queryParameters);
59
60
        return $this
61
            ->calendarService
62
            ->events
63
            ->listEvents($this->calendarId, $parameters)
64
            ->getItems();
65
    }
66
67
    /**
68
     * Get a single event.
69
     *
70
     * @param string $eventId
71
     *
72
     * @return \Google_Service_Calendar_Event
73
     */
74
    public function getEvent(string $eventId): Google_Service_Calendar_Event
75
    {
76
        return $this->calendarService->events->get($this->calendarId, $eventId);
77
    }
78
79
    /**
80
     * Insert an event.
81
     *
82
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
83
     *
84
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
85
     *
86
     * @return \Google_Service_Calendar_Event
87
     */
88 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...
89
    {
90
        if ($event instanceof Event) {
91
            $event = $event->googleEvent;
92
        }
93
94
        return $this->calendarService->events->insert($this->calendarId, $event);
95
    }
96
97
    /**
98
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
99
     *
100
     * @return \Google_Service_Calendar_Event
101
     */
102 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...
103
    {
104
        if ($event instanceof Event) {
105
            $event = $event->googleEvent;
106
        }
107
108
        return $this->calendarService->events->update($this->calendarId, $event->id, $event);
109
    }
110
111
    /**
112
     * @param string|\Spatie\GoogleCalendar\Event $eventId
113
     */
114
    public function deleteEvent($eventId)
115
    {
116
        if ($eventId instanceof Event) {
117
            $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...
118
        }
119
120
        $this->calendarService->events->delete($this->calendarId, $eventId);
121
    }
122
123
    public function getService(): Google_Service_Calendar
124
    {
125
        return $this->calendarService;
126
    }
127
}
128