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

GoogleCalendarManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 2
cbo 5
dl 0
loc 75
ccs 0
cts 43
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createEvent() 0 19 1
A getEventList() 0 7 1
A getEventById() 0 4 1
A deleteEvent() 0 4 1
A editEvent() 0 18 1
A clear() 0 8 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