Completed
Push — master ( 927e77...af986a )
by Freek
01:11
created

src/GoogleCalendar.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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