Passed
Pull Request — master (#4)
by
unknown
03:43
created

Holidays   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 185
rs 10
c 0
b 0
f 0
wmc 19

8 Methods

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