Completed
Push — master ( 3d895d...d2257e )
by Freek
02:06
created

GoogleCalendar   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 22.03 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 26
loc 118
rs 10
c 2
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCalendarId() 0 4 1
A listEvents() 0 23 3
A getEvent() 0 4 1
A insertEvent() 8 8 2
A updateEvent() 8 8 2
A deleteEvent() 10 10 2
A getService() 0 4 1

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 Spatie\GoogleCalendar;
4
5
use Carbon\Carbon;
6
use DateTime;
7
use Google_Service_Calendar;
8
use Google_Service_Calendar_Event;
9
use Illuminate\Support\Collection;
10
11
class GoogleCalendar
12
{
13
    /** @var \Google_Service_Calendar */
14
    protected $calendarService;
15
16
    /** @var string */
17
    protected $calendarId;
18
19
    public function __construct(Google_Service_Calendar $calendarService, $calendarId)
20
    {
21
        $this->calendarService = $calendarService;
22
23
        $this->calendarId = $calendarId;
24
    }
25
26
    public function getCalendarId() : string
27
    {
28
        return $this->calendarId;
29
    }
30
31
    /**
32
     * List events.
33
     *
34
     * @param Carbon $startDateTime
35
     * @param Carbon $endDateTime
36
     * @param array  $queryParameters
37
     *
38
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
39
     *
40
     * @return Collection
41
     */
42
    public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, $queryParameters = []) : Collection
43
    {
44
        $parameters = ['singleEvents' => true];
45
46
        if (is_null($startDateTime)) {
47
            $startDateTime = Carbon::now()->startOfDay();
48
        }
49
50
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
51
52
        if (is_null($endDateTime)) {
53
            $endDateTime = Carbon::now()->addYear()->endOfDay();
54
        }
55
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
56
57
        $parameters = array_merge($parameters, $queryParameters);
58
59
        return $this
60
            ->calendarService
61
            ->events
62
            ->listEvents($this->calendarId, $parameters)
63
            ->getItems();
64
    }
65
66
    /**
67
     * @param string $eventId
68
     *
69
     * @return static
70
     */
71
    public function getEvent($eventId)
72
    {
73
        return $this->calendarService->events->get($this->calendarId, $eventId);
74
    }
75
76
    /**
77
     * Insert an event.
78
     *
79
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
80
     *
81
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
82
     *
83
     * @return mixed
84
     */
85 View Code Duplication
    public function insertEvent($event)
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...
86
    {
87
        if ($event instanceof Event) {
88
            $event = $event->googleEvent;
89
        }
90
91
        return $this->calendarService->events->insert($this->calendarId, $event);
92
    }
93
94
    /**
95
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
96
     *
97
     * @return \Google_Service_Calendar_Event
98
     */
99 View Code Duplication
    public function updateEvent($event)
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...
100
    {
101
        if ($event instanceof Event) {
102
            $event = $event->googleEvent;
103
        }
104
105
        return $this->calendarService->events->update($this->calendarId, $event->id, $event);
106
    }
107
108
    /**
109
     * @param string|\Spatie\GoogleCalendar\Event $eventId
110
     * 
111
     * @return mixed
112
     */
113 View Code Duplication
    public function deleteEvent($eventId)
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...
114
    {
115
        if ($eventId instanceof Event) {
116
            $eventId = $eventId->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Spatie\GoogleCalendar\Event>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
        }
118
        
119
        dd($eventId);
120
121
        return $this->calendarService->events->delete($this->calendarId, $eventId);
122
    }
123
124
    public function getService() : Google_Service_Calendar
125
    {
126
        return $this->calendarService;
127
    }
128
}
129