Completed
Pull Request — dev (#26)
by nonanerz
04:03
created

GoogleCalendarManager::editEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.4285
cc 1
eloc 13
nc 1
nop 2
crap 2
1
<?php
2
3
namespace AppBundle\Services;
4
5
use AppBundle\Entity\DTO\DtoEvent;
6
7
class GoogleCalendarManager implements GoogleCalendarInterface
8
{
9
    private $calendar;
10
11
    public function __construct(GoogleClientFactory $factory)
12
    {
13
        $this->calendar = $factory->createCalendar('default', 'reader');
14
    }
15
16
    public function createEvent($dtoEvent)
17
    {
18
        $event = new \Google_Service_Calendar_Event();
19
20
        $event->setSummary($dtoEvent->getSummary());
21
        $event->setDescription($dtoEvent->getDescription());
22
        $event->setLocation($dtoEvent->getLocation());
23
        $event->setVisibility('public');
24
25
        $start = new \Google_Service_Calendar_EventDateTime();
26
        $start->setDateTime($dtoEvent->getStart());
27
        $event->setStart($start);
28
29
        $end = new \Google_Service_Calendar_EventDateTime();
30
        $end->setDateTime($dtoEvent->getEnd());
31
        $event->setEnd($end);
32
33
        return $this->calendar->events->insert('primary', $event);
34
    }
35
36
    public function getEventList($calendarId = 'primary')
37
    {
38
        return $this->calendar
39
            ->events
40
            ->listEvents($calendarId)
41
            ->getItems();
42
    }
43
44
    public function getEventById($id)
45
    {
46
        return $this->calendar->events->get('primary', $id);
47
    }
48
49
    public function deleteEvent($id)
50
    {
51
        return $this->calendar->events->delete('primary', $id);
52
    }
53
54
    public function editEvent($id, $data)
55
    {
56
        $event = $this->getEventById($id);
57
        $event->setSummary($data['title']);
58
        $event->setDescription($data['description']);
59
        $event->setLocation($data['location']);
60
        $event->setVisibility('public');
61
62
        $start = new \Google_Service_Calendar_EventDateTime();
63
        $start->setDateTime($data['start']);
64
        $event->setStart($start);
65
66
        $end = new \Google_Service_Calendar_EventDateTime();
67
        $end->setDateTime($data['end']);
68
        $event->setEnd($end);
69
70
        return $this->calendar->events->patch('primary', $id, $event);
71
    }
72
73
    public function clear()
74
    {
75
        $events = $this->getEventList();
76
77
        foreach ($events as $event) {
78
            $this->deleteEvent($event->getId());
79
        }
80
    }
81
}
82