-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathCalendarEventService.php
166 lines (142 loc) · 4.62 KB
/
CalendarEventService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace App\Services;
use Carbon\Carbon;
use Exception;
use Google_Client;
use Google_Service_Calendar;
use Google_Service_Calendar_Event;
class CalendarEventService
{
protected $attendees = [];
protected $summary;
protected $startDateTime;
protected $endDateTime;
protected $hangoutLink;
protected $service;
protected $id;
public function __construct()
{
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject(config('constants.gsuite.service-account-impersonate'));
$client->setScopes(Google_Service_Calendar::CALENDAR);
$this->service = new Google_Service_Calendar($client);
}
public function create($details, $calendarId = 'primary')
{
if (! isset($details['summary'], $details['attendees'], $details['start'], $details['end'])) {
throw new Exception('Invalid data for calendar creation');
}
$this->setSummary($details['summary']);
$this->setAttendees($details['attendees']);
$this->setStartDateTime($details['start']);
$this->setEndDateTime($details['end']);
$event = new Google_Service_Calendar_Event([
'summary' => $this->summary,
'attendees' => $this->attendees,
'start' => $this->startDateTime,
'end' => $this->endDateTime,
]);
$optprm = [
'sendNotifications' => true,
];
$event = $this->service->events->insert($calendarId, $event, $optprm);
$this->id = $event->id;
$this->setHangoutLink($event->hangoutLink);
}
public function fetch($eventId, $calendarId = 'primary')
{
$event = $this->service->events->get($calendarId, $eventId);
$this->setSummary($event->summary);
$attendees = [];
foreach ($event->attendees as $attendee) {
array_push($attendees, $attendee->email);
}
$this->setAttendees($attendees);
$this->setHangoutLink($event->hangoutLink);
$this->setStartDateTime($event->start->dateTime, $event->start->timeZone);
$this->setEndDateTime($event->end->dateTime, $event->end->timeZone);
$this->id = $eventId;
}
public function getHangoutLink()
{
return $this->hangoutLink;
}
public function setHangoutLink($hangoutLink)
{
$this->hangoutLink = $hangoutLink;
}
public function getSummary()
{
return $this->summary;
}
public function setSummary($summary)
{
$this->summary = $summary;
}
public function getAttendees()
{
$attendees = [];
foreach ($this->attendees as $attendee) {
array_push($attendees, $attendee['email']);
}
return $attendees;
}
public function setAttendees(array $attendees)
{
foreach ($attendees as $attendee) {
$this->attendees[] = [
'email' => $attendee,
];
}
}
public function getStartDateTime($withTimeZone = false)
{
return self::getDateTime($this->startDateTime, $withTimeZone);
}
public function setStartDateTime($dateTime, $timeZone = null)
{
$this->startDateTime = self::getCalendarDateTime($dateTime, $timeZone);
}
public function getEndDateTime($timeZone = false)
{
return self::getDateTime($this->endDateTime, $timeZone);
}
public function setEndDateTime($dateTime, $timeZone = null)
{
$this->endDateTime = self::getCalendarDateTime($dateTime, $timeZone);
}
/**
* Returns an array that is expected by the Google Calendar API.
*
* @param string $dateTime
* @param string $timeZone
*
* @return array
*/
protected static function getCalendarDateTime($dateTime, $timeZone)
{
$timeZone = $timeZone ?? config('app.timezone');
return [
'dateTime' => Carbon::parse($dateTime)->format(config('constants.calendar_datetime_format')),
'timeZone' => $timeZone,
];
}
/**
* Returns an array with formats expected by Portal modules.
*
* @param mixed $eventDateTime
* @param bool $withTimeZone defines whether to return the calendar event timeZone or not
*
* @return array
*/
protected static function getDateTime($eventDateTime, $withTimeZone)
{
$results = [];
$results['dateTime'] = Carbon::parse($eventDateTime['dateTime'])->format(config('constants.datetime_format'));
if ($withTimeZone) {
$results['timeZone'] = $eventDateTime['timeZone'];
}
return $results;
}
}