Schedule::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 19
rs 9.7998

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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, ?string $calendaId = 'primary', ?int $period = 1): 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)){
23
                $startDate = new DateTime('now');
24
                $interval = new DateInterval("P{$period}D");
25
                $startDate->sub($interval);
26
            }
27
            $url_parameters['timeMin'] = $startDate->format(DATE_W3C);
28
        }
29
30
        $this->request(
31
            "GET",
32
            "v3/calendars/{$calendaId}/events?". http_build_query($url_parameters),
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 delete(string $eventId, ?string $calendaId = 'primary'): Schedule
81
    {   
82
        $this->request(
83
            "DELETE",
84
            "v3/calendars/{$calendaId}/events/{$eventId}",
85
            null,
86
            ["Content-Type" => "application/json"]
87
        );
88
        return $this;
89
    }
90
}
91