Completed
Pull Request — master (#30)
by nonanerz
07:53
created

GoogleCalendarManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 43.02 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 2
cbo 6
dl 37
loc 86
ccs 0
cts 68
cp 0
rs 10
c 2
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A deleteEvent() 0 4 1
A clear() 0 11 2
A editEvent() 19 19 1
A createEvent() 17 17 1
A getEventList() 0 11 1
A getEventById() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AppBundle\Services;
4
5
use AppBundle\Entity\DTO\DtoEvent;
6
7
class GoogleCalendarManager
8
{
9
    private $calendar;
10
11
    public function __construct(GoogleClientFactory $factory)
12
    {
13
        $this->calendar = $factory->createCalendar('user', 'owner');
14
    }
15
16 View Code Duplication
    public function createEvent(DtoEvent $dtoEvent, $data = [])
0 ignored issues
show
Duplication introduced by
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...
17
    {
18
        $description = ['user' => $dtoEvent->getUser(), 'description' => $dtoEvent->getDescription()];
19
        $event = new \Google_Service_Calendar_Event();
20
        $event->setSummary($dtoEvent->getSummary());
21
        $event->setDescription(json_encode($description));
22
        $event->setLocation($dtoEvent->getLocation());
23
        $event->setVisibility('public');
24
        $start = new \Google_Service_Calendar_EventDateTime();
25
        $start->setDateTime($dtoEvent->getStart());
26
        $event->setStart($start);
27
        $end = new \Google_Service_Calendar_EventDateTime();
28
        $end->setDateTime($dtoEvent->getEnd());
29
        $event->setEnd($end);
30
31
        return $this->calendar->events->insert('primary', $event, $data);
32
    }
33
34
    public function getEventList($query = [])
35
    {
36
        $events = $this->calendar
37
            ->events
38
            ->listEvents('primary', $query);
39
40
        return [
41
            'pageToken' => $events->getNextPageToken(),
42
            'events' => $events->getItems(),
43
        ];
44
    }
45
46
    public function getEventById($id)
47
    {
48
        $event = $this->calendar->events->get('primary', $id);
49
        if ($event->status == 'cancelled') {
50
            return;
51
        }
52
53
        return $event;
54
    }
55
56
    public function deleteEvent($id)
57
    {
58
        return $this->calendar->events->delete('primary', $id);
59
    }
60
61 View Code Duplication
    public function editEvent(DtoEvent $dtoEvent, $id, $data = [])
0 ignored issues
show
Duplication introduced by
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...
62
    {
63
        $description = ['user' => $dtoEvent->getUser(), 'description' => $dtoEvent->getDescription()];
64
        $event = $this->getEventById($id);
65
        $event->setSummary($dtoEvent->getSummary());
66
        $event->setDescription(json_encode($description));
67
        $event->setLocation($dtoEvent->getLocation());
68
        $event->setVisibility('public');
69
70
        $start = new \Google_Service_Calendar_EventDateTime();
71
        $start->setDateTime($dtoEvent->getStart());
72
        $event->setStart($start);
73
74
        $end = new \Google_Service_Calendar_EventDateTime();
75
        $end->setDateTime($dtoEvent->getEnd());
76
        $event->setEnd($end);
77
78
        return $this->calendar->events->patch('primary', $id, $event, $data);
79
    }
80
81
    public function clear()
82
    {
83
        $events = $this->calendar
84
            ->events
85
            ->listEvents('primary')
86
            ->getItems();
87
88
        foreach ($events as $event) {
89
            $this->deleteEvent($event->getId());
90
        }
91
    }
92
}
93