Completed
Push — master ( 1b85db...753906 )
by Freek
02:16
created

src/Event.php (4 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 DateTime;
7
use Google_Service_Calendar_Event;
8
use Google_Service_Calendar_EventDateTime;
9
10
class Event
11
{
12
    /** @var string */
13
    public $id;
14
15
    /** @var string */
16
    public $name;
17
18
    /** @var Carbon */
19
    public $startDateTime;
20
21
    /** @var Carbon */
22
    public $endDateTime;
23
24
    /** @var bool */
25
    public $allDayEvent;
26
27
    /** @var Google_Service_Calendar_Event*/
28
    public $googleEvent;
29
    
30
    /** @var int */
31
    protected $calendarId;
32
33
    public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId)  {
34
35
        $event = new static;
36
37
        $event->name = $googleEvent->summary;
38
39 View Code Duplication
        if (! is_null($googleEvent['start']['date'])) {
0 ignored issues
show
This code seems to be duplicated across 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...
40
            $event->startDateTime = Carbon::createFromFormat('Y-m-d', $googleEvent['start']['date']);
41
        }
42
43 View Code Duplication
        if (! is_null($googleEvent['start']['dateTime'])) {
0 ignored issues
show
This code seems to be duplicated across 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...
44
            $event->startDateTime = Carbon::createFromFormat(DateTime::RFC3339, $googleEvent['start']['dateTime']);
45
        }
46
47 View Code Duplication
        if (! is_null($googleEvent['end']['date'])) {
0 ignored issues
show
This code seems to be duplicated across 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...
48
            $event->endDateTime = Carbon::createFromFormat('Y-m-d', $googleEvent['end']['date']);
49
        }
50
51 View Code Duplication
        if (! is_null($googleEvent['end']['dateTime'])) {
0 ignored issues
show
This code seems to be duplicated across 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...
52
            $event->endDateTime = Carbon::createFromFormat(DateTime::RFC3339, $googleEvent['end']['dateTime']);
53
        }
54
55
        $event->allDayEvent = is_null($googleEvent['start']['dateTime']);
56
57
        $event->id = $googleEvent->id;
58
59
        $event->googleEvent = $googleEvent;
60
        
61
        $event->calendarId = $calendarId;
62
63
        return $event;
64
    }
65
66
    public function convertToGoogleEvent() : Google_Service_Calendar_Event
67
    {
68
        $googleEvent = $this->googleEvent ?? new Google_Service_Calendar_Event();
69
70
        $googleEvent->summary = $this->name;
71
        
72
        $start = new Google_Service_Calendar_EventDateTime();
73
        $end = new Google_Service_Calendar_EventDateTime();
74
        
75
        if ($this->allDayEvent) {
76
            $start->setDate($this->startDateTime->format('Y-m-d'));
77
            $end->setDate($this->endDateTime->format('Y-m-d'));
78
        }
79
        else {
80
            $start->setDateTime($this->startDateTime->format(DateTime::RFC3339));
81
            $end->setDateTime($this->endDateTime->format(DateTime::RFC3339));
82
        }
83
        
84
        $googleEvent->setStart($start);
85
        $googleEvent->setEnd($end);
86
87
        return $googleEvent;
88
    }
89
90
    public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, $queryParameters = [], $calendarId = null)
91
    {
92
        $googleCalendar = self::getGoogleCalendar($calendarId);
93
94
        return $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
95
    }
96
    
97
    public static function find($id, $calendarId = null)
98
    {
99
        $googleCalendar = self::getGoogleCalendar($calendarId);
100
        
101
        return $googleCalendar->getEvent($id);
102
    }
103
104
    public function save()
105
    {
106
        $method = $this->googleEvent ? 'updateEvent' : 'insertEvent';
107
108
        return $this->getGoogleCalendar()->$method($this);
109
    }
110
111
    public function delete($id = null)
112
    {
113
        return $this->getGoogleCalendar($this->calendarId)->deleteEvent($id ?? $this->id);
114
    }
115
116
    protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar
117
    {
118
        $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
119
120
        return GoogleCalendarFactory::createForCalendarId($calendarId);
121
    }
122
}