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

src/GoogleCalendar.php (2 issues)

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 Carbon\Carbon;
6
use DateTime;
7
use Google_Service_Calendar;
8
use Google_Service_Calendar_Event;
9
use Illuminate\Support\Collection;
10
11
class GoogleCalendar
12
{
13
    /** @var \Google_Service_Calendar */
14
    protected $calendarService;
15
16
    /** @var string */
17
    protected $calendarId;
18
19
    public function __construct(Google_Service_Calendar $calendarService, $calendarId)
20
    {
21
        $this->calendarService = $calendarService;
22
23
        $this->calendarId = $calendarId;
24
    }
25
26
    public function getCalendarId() : string
27
    {
28
        return $this->calendarId;
29
    }
30
31
    /**
32
     * List events.
33
     *
34
     * @param Carbon $startDateTime
35
     * @param Carbon $endDateTime
36
     * @param array  $queryParameters
37
     *
38
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
39
     *
40
     * @return array
41
     */
42
    public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [])
43
    {
44
        $parameters = ['singleEvents' => true];
45
46
        if (is_null($startDateTime)) {
47
            $startDateTime = Carbon::now()->startOfDay();
48
        }
49
50
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
51
52
        if (is_null($endDateTime)) {
53
            $endDateTime = Carbon::now()->addYear()->endOfDay();
54
        }
55
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
56
57
        $parameters = array_merge($parameters, $queryParameters);
58
59
        return $this
60
            ->calendarService
61
            ->events
62
            ->listEvents($this->calendarId, $parameters)
63
            ->getItems();
64
    }
65
66
    /**
67
     * Get a single event.
68
     * 
69
     * @param string $eventId
70
     * @return \Google_Service_Calendar_Event
71
     */
72
    public function getEvent(string $eventId) : Google_Service_Calendar_Event
73
    {
74
        return $this->calendarService->events->get($this->calendarId, $eventId);
75
    }
76
77
    /**
78
     * Insert an event.
79
     *
80
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
81
     *
82
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
83
     *
84
     * @return \Google_Service_Calendar_Event
85
     */
86 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...
87
    {
88
        if ($event instanceof Event) {
89
            $event = $event->googleEvent;
90
        }
91
92
        return $this->calendarService->events->insert($this->calendarId, $event);
93
    }
94
95
    /**
96
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
97
     *
98
     * @return \Google_Service_Calendar_Event
99
     */
100 View Code Duplication
    public function updateEvent($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...
101
    {
102
        if ($event instanceof Event) {
103
            $event = $event->googleEvent;
104
        }
105
106
        return $this->calendarService->events->update($this->calendarId, $event->id, $event);
107
    }
108
109
    /**
110
     * @param string|\Spatie\GoogleCalendar\Event $eventId
111
     * 
112
     * @return mixed
113
     */
114
    public function deleteEvent($eventId)
115
    {
116
        if ($eventId instanceof Event) {
117
            $eventId = $eventId->id;
118
        }
119
120
        return $this->calendarService->events->delete($this->calendarId, $eventId);
121
    }
122
123
    public function getService() : Google_Service_Calendar
124
    {
125
        return $this->calendarService;
126
    }
127
}
128