Passed
Pull Request — master (#4)
by
unknown
01:56
created

Holidays::to()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Google;
4
5
class Holidays
6
{
7
    /**
8
     * Google API Key.
9
     *
10
     * @var string
11
     */
12
    private $api_key;
13
14
    /**
15
     * Country Code.
16
     *
17
     * @var string
18
     */
19
    private $country_code;
20
21
    /**
22
     * Start Date.
23
     *
24
     * @var string
25
     */
26
    private $start_date;
27
28
    /**
29
     * End Date.
30
     *
31
     * @var string
32
     */
33
    private $end_date;
34
35
    /**
36
     * Minimal output boolean.
37
     *
38
     * @var bool
39
     */
40
    private $minimal = false;
41
42
    /**
43
     * Dates only output boolean.
44
     *
45
     * @var bool
46
     */
47
    private $dates_only = false;
48
49
    /**
50
     * Construct!
51
     *
52
     * @return void
53
     */
54
    public function __construct()
55
    {
56
        $this->start_date = date('Y-m-d').'T00:00:00-00:00';
57
        $this->end_date = (date('Y') + 1).'-01-01T00:00:00-00:00';
58
    }
59
 
60
    /**
61
     * Setting starting date.
62
     *
63
     * @param string Date in any format
0 ignored issues
show
Bug introduced by
The type Google\Date was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
     *
65
     * @return self
66
     */
67
    public function from($str)
68
    {
69
        $this->start_date = date('Y-m-d', strtotime($str)).'T00:00:00-00:00';
70
        return $this;
71
    }
72
    /**
73
     * Setting end date.
74
     *
75
     * @param string Date in any format
76
     *
77
     * @return self
78
     */
79
    public function to($str)
80
    {
81
        $this->end_date = date('Y-m-d', strtotime($str)).'T00:00:00-00:00';
82
        return $this;
83
    }
84
    /**
85
     * Setter of API key.
86
     *
87
     * @return void
88
     */
89
    public function withApiKey($api_key)
90
    {
91
        $this->api_key = $api_key;
92
93
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Google\Holidays which is incompatible with the documented return type void.
Loading history...
94
    }
95
96
    /**
97
     * Define the country code to retrieve holidays for.
98
     *
99
     * @return void
100
     */
101
    public function inCountry($country_code)
102
    {
103
        $this->country_code = strtolower($country_code);
104
105
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Google\Holidays which is incompatible with the documented return type void.
Loading history...
106
    }
107
108
    /**
109
     * Define the output result as minimal.
110
     *
111
     * @return void
112
     */
113
    public function withMinimalOutput()
114
    {
115
        $this->minimal = true;
116
117
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Google\Holidays which is incompatible with the documented return type void.
Loading history...
118
    }
119
120
    /**
121
     * Define the output as dates only.
122
     *
123
     * @return void
124
     */
125
    public function withDatesOnly()
126
    {
127
        $this->dates_only = true;
128
129
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Google\Holidays which is incompatible with the documented return type void.
Loading history...
130
    }
131
132
    /**
133
     * Get the list of holidays.
134
     *
135
     * @return mixed
136
     */
137
    public function list()
138
    {
139
        if (!$this->api_key) {
140
            throw new \Exception('Providing an API key might be a better start. RTFM.');
141
        }
142
143
        if (!$this->country_code) {
144
            throw new \Exception('Providing a Country Code is a good idea. RTFM.');
145
        }
146
147
        $result = [];
148
149
        $api_url = "https://content.googleapis.com/calendar/v3/calendars/en.{$this->country_code}%23holiday%40group.v.calendar.google.com/events".
150
               '?singleEvents=false'.
151
               "&timeMax={$this->end_date}".
152
               "&timeMin={$this->start_date}".
153
               "&key={$this->api_key}";
154
155
        $response = json_decode(file_get_contents($api_url), true);
156
157
        if (isset($response['items'])) {
158
            if ($this->dates_only === true) {
159
                foreach ($response['items'] as $holiday) {
160
                    $result[] = $holiday['start']['date'];
161
                }
162
163
                sort($result);
164
            } elseif ($this->minimal === true) {
165
                foreach ($response['items'] as $holiday) {
166
                    $result[] = [
167
                      'name' => $holiday['summary'],
168
                      'date' => $holiday['start']['date'],
169
                    ];
170
                }
171
172
                usort($result, function ($a, $b) {
173
                    if ($a['date'] == $b['date']) {
174
                        return 0;
175
                    }
176
177
                    return ($a['date'] < $b['date']) ? -1 : 1;
178
                });
179
            } else {
180
                $result = $response['items'];
181
182
                usort($result, function ($a, $b) {
183
                    if ($a['start']['date'] == $b['start']['date']) {
184
                        return 0;
185
                    }
186
187
                    return ($a['start']['date'] < $b['start']['date']) ? -1 : 1;
188
                });
189
            }
190
        }
191
192
        return $result;
193
    }
194
}
195