Completed
Push — master ( aeb2a1...0a8019 )
by Freek
02:31
created

Event::convertToGoogleEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 9.0856
cc 2
eloc 14
nc 2
nop 0
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
Duplication introduced by
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
Duplication introduced by
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
Duplication introduced by
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
Duplication introduced by
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 = 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->setDateTime($this->startDateTime->format(DateTime::RFC3339));
77
            $end->setDateTime($this->startDateTime->format(DateTime::RFC3339));
78
        }
79
        else {
80
            $start->setDate($this->startDateTime->format('Y-m-d'));
81
            $end->setDate($this->endDateTime->format('Y-m-d'));
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
107
    }
108
109
    public function delete($id = null)
110
    {
111
        return $this->getGoogleCalendar($this->calendarId)->deleteEvent($id ?? $this->id);
112
    }
113
114
    protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar
115
    {
116
        $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
117
118
        return GoogleCalendarFactory::createForCalendarId($calendarId);
119
    }
120
}