Completed
Push — master ( 77f282...4c64a1 )
by Freek
02:44
created

GoogleCalendar::updateEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %
Metric Value
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
        $parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339);
50
51
        if (is_null($endDateTime)) {
52
            $endDateTime = Carbon::now()->addYear()->endOfDay();
53
        }
54
        $parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339);
55
56
        $parameters = array_merge($parameters, $queryParameters);
57
58
        $googleEvents = $this
59
            ->calendarService
60
            ->events
61
            ->listEvents($this->calendarId, $parameters)
62
            ->getItems();
63
64
        $events = collect($googleEvents)
65
            ->map(function (Google_Service_Calendar_Event $event) {
66
                return Event::createFromGoogleCalendarEvent($event, $this->calendarId);
67
            })
68
       // ->sortBy(function (Event $event) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
       //    return $event->startDateTime->format(DATE_ISO8601);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
//        })
71
;
72
73
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
        $debug = $events->map(function (Event $event) {
75
            return $event->startDateTime->format('Y-m-d H:i:s') . ' - ' . $event->startDateTime->format('Y-m-d H:i:s') . $event->name . PHP_EOL;
76
        });
77
        */
78
79
        return $events;
80
    }
81
82
    /**
83
     * @param string $eventId
84
     *
85
     * @return static
86
     */
87
    public function getEvent($eventId)
88
    {
89
        $googleEvent = $this->calendarService->events->get($this->calendarId, $eventId);
90
91
        return Event::createFromGoogleCalendarEvent($googleEvent, $this->calendarId);
0 ignored issues
show
Bug introduced by
It seems like $googleEvent defined by $this->calendarService->...->calendarId, $eventId) on line 89 can also be of type object<Google_Http_Request>; however, Spatie\GoogleCalendar\Ev...omGoogleCalendarEvent() does only seem to accept object<Google_Service_Calendar_Event>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
92
    }
93
94
    /**
95
     * Insert an event.
96
     *
97
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
98
     *
99
     * @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert
100
     *
101
     * @return mixed
102
     */
103 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...
104
    {
105
        if ($event instanceof Event) {
106
            $event = $event->googleEvent;
107
        }
108
109
        return $this->calendarService->events->insert($this->calendarId, $event);
110
    }
111
112
    /**
113
     * @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event
114
     *
115
     * @return \Google_Service_Calendar_Event
116
     */
117 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...
118
    {
119
        if ($event instanceof Event) {
120
            $event = $event->googleEvent;
121
        }
122
123
        return $this->calendarService->events->update($this->calendarId, $event->id, $event);
124
    }
125
126
    /**
127
     * @param string|\Spatie\GoogleCalendar\Event $eventId
128
     * 
129
     * @return mixed
130
     */
131
    public function deleteEvent($eventId)
132
    {
133
        if ($eventId instanceof Event) {
134
            $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...
135
        }
136
137
        return $this->calendarService->events->delete($this->calendarId, $eventId);
138
    }
139
140
    public function getService() : Google_Service_Calendar
141
    {
142
        return $this->calendarService;
143
    }
144
}
145