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

src/Event.php (1 issue)

mismatching argument types.

Documentation Minor

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 Google_Service_Calendar_Event*/
13
    public $googleEvent;
14
15
    /** @var int */
16
    protected $calendarId;
17
18
    public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId)
19
    {
20
        $event = new static();
21
22
        $event->googleEvent = $googleEvent;
23
24
        $event->calendarId = $calendarId;
25
26
        return $event;
27
    }
28
29
    public static function create(array $properties, $calendarId = null)
30
    {
31
        $event = new static;
32
33
        $event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId();
34
35
        foreach($properties as $name => $value) {
36
            $event->$name = $value;
37
        }
38
        
39
        return $event->save();
40
    }
41
42
    public function __construct()
43
    {
44
        $this->googleEvent = new Google_Service_Calendar_Event();
45
    }
46
47
    public function __get($name)
48
    {
49
        $name = $this->translateFieldName($name);
50
51
        $value = array_get($this->googleEvent, $name);
52
53
        if (in_array($name, ['startDate', 'end.date']) && $value) {
54
            $value = Carbon::createFromFormat('Y-m-d', $value);
55
        }
56
57
        if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) {
58
            $value = Carbon::createFromFormat(DateTime::RFC3339, $value);
59
        }
60
61
        return $value;
62
    }
63
64
    public function __set($name, $value)
65
    {
66
        $name = $this->translateFieldName($name);
67
68
        if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) {
69
            $this->setDateProperty($name, $value);
70
71
            return;
72
        }
73
74
        array_set($this->googleEvent, $name, $value);
0 ignored issues
show
$this->googleEvent is of type object<Google_Service_Calendar_Event>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function exists()
81
    {
82
        return $this->id != '';
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isAllDayEvent()
89
    {
90
        return is_null($this->googleEvent['start']['dateTime']);
91
    }
92
93
    public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, $queryParameters = [], $calendarId = null)
94
    {
95
        $googleCalendar = self::getGoogleCalendar($calendarId);
96
97
        return $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters);
98
    }
99
100
    /**
101
     * @param string $id
102
     * @param string $calendarId
103
     *
104
     * @return mixed
105
     */
106
    public static function find($id, $calendarId = null)
107
    {
108
        $googleCalendar = self::getGoogleCalendar($calendarId);
109
110
        return $googleCalendar->getEvent($id);
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function save()
117
    {
118
        $method = $this->exists() ? 'updateEvent' : 'insertEvent';
119
120
        return $this->getGoogleCalendar()->$method($this);
121
    }
122
123
    /**
124
     * @param string $id
125
     *
126
     * @return mixed
127
     */
128
    public function delete($id = null)
129
    {
130
        return $this->getGoogleCalendar($this->calendarId)->deleteEvent($id ?? $this->id);
131
    }
132
133
    /**
134
     * @param string $calendarId
135
     *
136
     * @return \Spatie\GoogleCalendar\GoogleCalendar
137
     */
138
    protected static function getGoogleCalendar($calendarId = null) : GoogleCalendar
139
    {
140
        $calendarId = $calendarId ?? config('laravel-google-calendar.calendar_id');
141
142
        return GoogleCalendarFactory::createForCalendarId($calendarId);
143
    }
144
145
    /**
146
     * @param string         $name
147
     * @param \Carbon\Carbon $date
148
     */
149
    protected function setDateProperty($name, Carbon $date)
150
    {
151
        $eventDateTime = new Google_Service_Calendar_EventDateTime();
152
153
        if (in_array($name, ['start.date', 'end.date'])) {
154
            $eventDateTime->setDate($date->format('Y-m-d'));
155
        }
156
157
        if (in_array($name, ['start.dateTime', 'end.dateTime'])) {
158
            $eventDateTime->setDate($date->format(DateTime::RFC3339));
159
        }
160
161
        if (starts_with($name, 'start')) {
162
            $this->googleEvent->setStart($eventDateTime);
163
        }
164
165
        if (starts_with($name, 'end')) {
166
            $this->googleEvent->setEnd($eventDateTime);
167
        }
168
    }
169
170
    /**
171
     * @param $name
172
     *
173
     * @return string
174
     */
175
    protected function translateFieldName($name)
176
    {
177
        if ($name === 'name') {
178
            return 'summary';
179
        }
180
181
        if ($name === 'startDate') {
182
            return 'start.date';
183
        }
184
185
        if ($name === 'endDate') {
186
            return 'end.date';
187
        }
188
189
        if ($name === 'startDateTime') {
190
            return 'start.dateTime';
191
        }
192
193
        if ($name === 'endDateTime') {
194
            return  'end.dateTime';
195
        }
196
197
        return $name;
198
    }
199
}
200