Completed
Push — master ( c4a284...c64a87 )
by Freek
07:27 queued 05:53
created

GoogleCalendar   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 18.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 16
loc 87
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCalendarId() 0 4 1
A __construct() 0 6 1
A listEvents() 0 23 3
A getEvent() 0 4 1
A insertEvent() 8 8 2
A updateEvent() 8 8 2
A deleteEvent() 0 8 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 DateTime;
6
use Carbon\Carbon;
7
use Google_Service_Calendar;
8
use Google_Service_Calendar_Event;
9
10
class GoogleCalendar
11
{
12
    /** @var \Google_Service_Calendar */
13
    protected $calendarService;
14
15
    /** @var string */
16
    protected $calendarId;
17
18
    public function __construct(Google_Service_Calendar $calendarService, string $calendarId)
19
    {
20
        $this->calendarService = $calendarService;
21
22
        $this->calendarId = $calendarId;
23
    }
24
25
    public function getCalendarId(): string
26
    {
27
        return $this->calendarId;
28
    }
29
30
    /*
31
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/list
32
     */
33
    public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = []): array
34
    {
35
        $parameters = ['singleEvents' => true];
36
37
        if (is_null($startDateTime)) {
38
            $startDateTime = Carbon::now()->startOfDay();
39
        }
40
41
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
42
43
        if (is_null($endDateTime)) {
44
            $endDateTime = Carbon::now()->addYear()->endOfDay();
45
        }
46
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
47
48
        $parameters = array_merge($parameters, $queryParameters);
49
50
        return $this
51
            ->calendarService
52
            ->events
53
            ->listEvents($this->calendarId, $parameters)
54
            ->getItems();
55
    }
56
57
    public function getEvent(string $eventId): Google_Service_Calendar_Event
58
    {
59
        return $this->calendarService->events->get($this->calendarId, $eventId);
60
    }
61
62
    /*
63
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
64
     */
65 View Code Duplication
    public function insertEvent($event): Google_Service_Calendar_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...
66
    {
67
        if ($event instanceof Event) {
68
            $event = $event->googleEvent;
69
        }
70
71
        return $this->calendarService->events->insert($this->calendarId, $event);
72
    }
73
74 View Code Duplication
    public function updateEvent($event): Google_Service_Calendar_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...
75
    {
76
        if ($event instanceof Event) {
77
            $event = $event->googleEvent;
78
        }
79
80
        return $this->calendarService->events->update($this->calendarId, $event->id, $event);
81
    }
82
83
    public function deleteEvent($eventId)
84
    {
85
        if ($eventId instanceof Event) {
86
            $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...
87
        }
88
89
        $this->calendarService->events->delete($this->calendarId, $eventId);
90
    }
91
92
    public function getService(): Google_Service_Calendar
93
    {
94
        return $this->calendarService;
95
    }
96
}
97