Completed
Push — master ( e35df4...3aa3c0 )
by Freek
20:04
created

src/GoogleCalendar.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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;
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