Completed
Pull Request — master (#89)
by Jonathan
09:52
created

GoogleCalendar::getEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
use Psr\Http\Message\RequestInterface;
10
11
class GoogleCalendar
12
{
13
    /** @var \Google_Service_Calendar */
14
    protected $calendarService;
15
16
    /** @var string */
17
    protected $calendarId;
18
19
    /** @var iterable */
20
    protected $batchRequests;
21
22
    public function __construct(Google_Service_Calendar $calendarService, string $calendarId)
23
    {
24
        $this->calendarService = $calendarService;
25
26
        $this->calendarId = $calendarId;
27
28
        $this->batchRequests = $this->calendarService->createBatch();
29
    }
30
31
    public function getCalendarId(): string
32
    {
33
        return $this->calendarId;
34
    }
35
36
    /*
37
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
38
     */
39
    public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = []): array
40
    {
41
        $parameters = ['singleEvents' => true];
42
43
        if (is_null($startDateTime)) {
44
            $startDateTime = Carbon::now()->startOfDay();
45
        }
46
47
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
48
49
        if (is_null($endDateTime)) {
50
            $endDateTime = Carbon::now()->addYear()->endOfDay();
51
        }
52
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
53
54
        $parameters = array_merge($parameters, $queryParameters);
55
56
        return $this
57
            ->calendarService
58
            ->events
59
            ->listEvents($this->calendarId, $parameters)
60
            ->getItems();
61
    }
62
63
64
    /**
65
     * @param string $eventId
66
     * @param array  $optParams
67
     *
68
     * @return Google_Service_Calendar_Event|RequestInterface
69
     */
70
    public function getEvent(string $eventId, $optParams = [])
71
    {
72
        return $this->calendarService->events->get($this->calendarId, $eventId, $optParams);
73
    }
74
75 View Code Duplication
    public function getEvents(iterable $eventIds, $optParams = [])
0 ignored issues
show
Duplication introduced by
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...
76
    {
77
        collect($eventIds)
78
            ->each(
79
                function ($eventId, $batchIdentifier) use ($optParams) {
80
                    $this->batchRequests->add($this->getEvent($eventId, $optParams), $batchIdentifier);
81
                });
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param       $event
88
     * @param array $optParams
89
     *
90
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
91
     * @return Google_Service_Calendar_Event|RequestInterface
92
     */
93 View Code Duplication
    public function insertEvent($event, $optParams = [])
0 ignored issues
show
Duplication introduced by
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...
94
    {
95
        if ($event instanceof Event) {
96
            $event = $event->googleEvent;
97
        }
98
99
        return $this->calendarService->events->insert($this->calendarId, $event, $optParams);
100
    }
101
102 View Code Duplication
    public function insertEvents(iterable $events, $optParams = [])
0 ignored issues
show
Duplication introduced by
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
        collect($events)
105
            ->each(
106
                function ($event, $batchIdentifier) use ($optParams) {
107
                    $this->batchRequests->add($this->insertEvent($event, $optParams), $batchIdentifier);
108
                });
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param       $event
115
     * @param array $optParams
116
     *
117
     * @return Google_Service_Calendar_Event|RequestInterface
118
     */
119 View Code Duplication
    public function updateEvent($event, $optParams = [])
0 ignored issues
show
Duplication introduced by
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...
120
    {
121
        if ($event instanceof Event) {
122
            $event = $event->googleEvent;
123
        }
124
125
        return $this->calendarService->events->update($this->calendarId, $event->id, $event, $optParams);
126
    }
127
128 View Code Duplication
    public function updateEvents(iterable $events, $optParams = [])
0 ignored issues
show
Duplication introduced by
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...
129
    {
130
        collect($events)
131
            ->each(
132
                function ($event, $batchIdentifier) use ($optParams) {
133
                    $this->batchRequests->add($this->updateEvent($event, $optParams), $batchIdentifier);
134
                });
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param       $eventId
141
     * @param array $optParams
142
     *
143
     * @return RequestInterface
144
     */
145
    public function deleteEvent($eventId,  $optParams = [])
146
    {
147
        if ($eventId instanceof Event) {
148
            $eventId = $eventId->id;
0 ignored issues
show
Documentation introduced by
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...
149
        }
150
151
        return $this->calendarService->events->delete($this->calendarId, $eventId, $optParams);
152
    }
153
154
    /**
155
     * @param iterable $eventIds
156
     * @param array    $optParams
157
     *
158
     * @return $this
159
     */
160 View Code Duplication
    public function deleteEvents(iterable $eventIds, $optParams = [])
0 ignored issues
show
Duplication introduced by
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...
161
    {
162
        collect($eventIds)
163
            ->each(
164
                function ($eventId, $batchIdentifier) use ($optParams) {
165
                    $this->batchRequests->add($this->deleteEvent($eventId, $optParams), $batchIdentifier);
166
                });
167
168
        return $this;
169
    }
170
171
    public function getService(): Google_Service_Calendar
172
    {
173
        return $this->calendarService;
174
    }
175
176
    public function enableBatch(bool $bool)
177
    {
178
        $this->calendarService->getClient()->setUseBatch($bool);
179
180
        return $this;
181
    }
182
183
    public function sendBatch():array
184
    {
185
        return $this->batchRequests->execute();
186
    }
187
}
188