Passed
Push — main ( 101378...cb4542 )
by Leandro
01:40
created

Schedule::cancel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
namespace LeandroFerreiraMa\GoogleCalendar;
4
5
use DateInterval;
6
use DateTime;
7
8
class Schedule extends GoogleCalendar
9
{
10
    public function __construct(string $token)
11
    {
12
        parent::__construct($token);
13
    }
14
15
    public function read(?array $headers, ?string $syncToken, ?DateTime $startDate = null): Schedule
16
    {
17
        $url_parameters = array();
18
19
        if(!empty($syncToken)){
20
            $url_parameters['syncToken'] = strip_tags($syncToken);
21
        } else {
22
            if(is_null($startDate)){
0 ignored issues
show
Bug introduced by
The function is_null was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
            if(/** @scrutinizer ignore-call */ is_null($startDate)){
Loading history...
23
                $startDate = new DateTime('now');
24
                $interval = new DateInterval("P1D");
25
                $startDate->sub($interval);
26
            }
27
            $url_parameters['timeMin'] = $startDate->format(DATE_W3C);
28
        }
29
        
30
        $this->request(
31
            "GET",
32
            "v3/calendars/primary/events?". http_build_query($url_parameters),
0 ignored issues
show
Bug introduced by
The function http_build_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            "v3/calendars/primary/events?". /** @scrutinizer ignore-call */ http_build_query($url_parameters),
Loading history...
33
            null,
34
            $headers
35
        );
36
        return $this;
37
    }
38
39
    public function create(string $summary, DateTime $start, DateTime $end,?string $description = null, ?string $location = null, ?string $calendaId = 'primary', ?array $attendees = []): Schedule
40
    {
41
        $schedule = [
42
            "summary" => $summary,
43
            "description" => $description,
44
            "location" => $location,
45
            "start" => array('dateTime' => $start->format(DATE_RFC3339)),
46
            "end" => array('dateTime' => $end->format(DATE_RFC3339)),
47
            "attendees" => $attendees
48
        ];
49
        
50
        $this->request(
51
            "POST",
52
            "v3/calendars/{$calendaId}/events",
53
            $schedule,
54
            ["Content-Type" => "application/json"]
55
        );
56
        return $this;
57
    }
58
59
    public function update(string $eventId, string $summary, DateTime $start, DateTime $end,?string $description = null, ?string $location = null, ?string $calendaId = 'primary', string $status = 'confirmed', ?array $attendees = []): Schedule
60
    {
61
        $schedule = [
62
            "summary" => $summary,
63
            "description" => $description,
64
            "location" => $location,
65
            "status" => $status,
66
            "start" => array('dateTime' => $start->format(DATE_RFC3339)),
67
            "end" => array('dateTime' => $end->format(DATE_RFC3339)),
68
            "attendees" => $attendees
69
        ];
70
        
71
        $this->request(
72
            "PUT",
73
            "v3/calendars/{$calendaId}/events/{$eventId}",
74
            $schedule,
75
            ["Content-Type" => "application/json"]
76
        );
77
        return $this;
78
    }
79
80
    public function cancel(string $eventId, ?string $calendaId = 'primary'): Schedule
81
    {
82
        $schedule = [
83
            "status" => 'canceled'
84
        ];
85
        
86
        $this->request(
87
            "PUT",
88
            "v3/calendars/{$calendaId}/events/{$eventId}",
89
            $schedule,
90
            ["Content-Type" => "application/json"]
91
        );
92
        return $this;
93
    }
94
}
95