Completed
Pull Request — dev (#23)
by nonanerz
02:42
created

GoogleCalendarManager::newEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 15
cp 0
cc 1
eloc 13
nc 1
nop 0
crap 2
1
<?php
2
3
namespace AppBundle\Services;
4
5
class GoogleCalendarManager
6
{
7
    private $client;
8
9
    private $calendar;
10
11
    public function __construct()
12
    {
13
        $this->client = new \Google_Client();
14
        $this->client->setApplicationName('seattle');
15
        $this->client->setScopes([\Google_Service_Calendar::CALENDAR]);
16
        $this->client->setAuthConfig(__DIR__.'/../../../app/config/credentials/credentials.json');
17
18
        $this->calendar = new \Google_Service_Calendar($this->client);
19
20
        $scope = new \Google_Service_Calendar_AclRuleScope();
21
        $scope->setType('default');
22
23
        $rule = new \Google_Service_Calendar_AclRule();
24
        $rule->setRole('reader');
25
        $rule->setScope($scope);
26
27
        $this->calendar->acl->insert('primary', $rule);
28
    }
29
30
    public function newEvent()
31
    {
32
        $event = new \Google_Service_Calendar_Event();
33
34
        $event->setSummary('TITLE');
35
        $event->setDescription('DESCRIPTION');
36
        $event->setLocation('God know where');
37
        $event->setVisibility('public');
38
39
        $start = new \Google_Service_Calendar_EventDateTime();
40
        $start->setDateTime('2017-03-10T09:00:00-07:00');
41
        $event->setStart($start);
42
43
        $end = new \Google_Service_Calendar_EventDateTime();
44
        $end->setDateTime('2017-03-11T09:00:00-07:00');
45
        $event->setEnd($end);
46
47
        return $this->calendar->events->insert('primary', $event);
48
    }
49
50
    public function getEventList($calendarId = 'primary')
51
    {
52
        return $this->calendar
53
           ->events
54
           ->listEvents($calendarId)
55
           ->getItems();
56
    }
57
58
    public function getEventById($id)
59
    {
60
        return $this->calendar->events->get('primary', $id);
61
    }
62
63
    public function deleteEvent($id)
64
    {
65
        return $this->calendar->events->delete('primary', $id);
66
    }
67
68
    public function editEvent($id)
69
    {
70
        $event = $this->getEventById($id);
71
        //do something
72
        return $this->calendar->events->patch('primary', $id, $event);
73
    }
74
75
    public function clear()
76
    {
77
        $events = $this->getEventList();
78
79
        foreach ($events as $event) {
80
            $this->deleteEvent($event->getId());
81
        }
82
    }
83
}
84