Canada::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 18
rs 9.9332
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Yasumi package.
5
 *
6
 * Copyright (c) 2015 - 2020 AzuyaLabs
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Sacha Telgenhof <[email protected]>
12
 */
13
14
namespace Yasumi\Provider;
15
16
use DateTime;
17
use Yasumi\Exception\InvalidDateException;
18
use Yasumi\Exception\UnknownLocaleException;
19
use Yasumi\Holiday;
20
21
/**
22
 * Provider for all holidays in Canada.
23
 */
24
class Canada extends AbstractProvider
25
{
26
    use CommonHolidays, ChristianHolidays;
27
28
    /**
29
     * Code to identify this Holiday Provider. Typically this is the ISO3166 code corresponding to the respective
30
     * country or sub-region.
31
     */
32
    public const ID = 'CA';
33
34
    /**
35
     * Initialize holidays for Canada.
36
     *
37
     * @throws InvalidDateException
38
     * @throws \InvalidArgumentException
39
     * @throws UnknownLocaleException
40
     * @throws \Exception
41
     */
42
    public function initialize(): void
43
    {
44
        $this->timezone = 'America/Toronto';
45
46
        // Add common holidays
47
        $this->addHoliday($this->newYearsDay($this->year, $this->timezone, $this->locale));
48
49
        // Add Christian holidays
50
        $this->addHoliday($this->christmasDay($this->year, $this->timezone, $this->locale));
51
        $this->addHoliday($this->secondChristmasDay($this->year, $this->timezone, $this->locale));
52
        $this->addHoliday($this->goodFriday($this->year, $this->timezone, $this->locale));
53
        $this->addHoliday($this->easterMonday($this->year, $this->timezone, $this->locale));
54
55
        // Calculate other holidays
56
        $this->calculateCanadaDay();
57
        $this->calculateLabourDay();
58
        $this->calculateThanksgivingDay();
59
        $this->calculateRemembranceDay();
60
    }
61
62
    /**
63
     * Family Day.
64
     *
65
     * @link https://en.wikipedia.org/wiki/Family_Day_(Canada)
66
     *
67
     * @throws InvalidDateException
68
     * @throws \InvalidArgumentException
69
     * @throws UnknownLocaleException
70
     * @throws \Exception
71
     */
72
    protected function calculateFamilyDay(): void
73
    {
74
        if ($this->year < 2009) {
75
            return;
76
        }
77
78
        $this->addHoliday(new Holiday(
79
            'familyDay',
80
            [],
81
            new DateTime("third monday of february $this->year", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
82
            $this->locale
83
        ));
84
    }
85
86
    /**
87
     * Victoria Day.
88
     *
89
     * @link https://en.wikipedia.org/wiki/Victoria_Day
90
     *
91
     * @throws InvalidDateException
92
     * @throws \InvalidArgumentException
93
     * @throws UnknownLocaleException
94
     * @throws \Exception
95
     */
96
    protected function calculateVictoriaDay(): void
97
    {
98
        if ($this->year < 1845) {
99
            return;
100
        }
101
102
        $this->addHoliday(new Holiday(
103
            'victoriaDay',
104
            [],
105
            new DateTime("last monday front of $this->year-05-25", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
106
            $this->locale
107
        ));
108
    }
109
110
    /**
111
     * National Indigenous Peoples Day.
112
     *
113
     * @link https://www.rcaanc-cirnac.gc.ca/eng/1100100013248/1534872397533
114
     *
115
     * @throws InvalidDateException
116
     * @throws \InvalidArgumentException
117
     * @throws UnknownLocaleException
118
     * @throws \Exception
119
     */
120
    protected function calculateNationalIndigenousPeoplesDay(): void
121
    {
122
        if ($this->year < 1996) {
123
            return;
124
        }
125
126
        $this->addHoliday(new Holiday(
127
            'nationalIndigenousPeoplesDay',
128
            [],
129
            new DateTime("$this->year-06-21", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
130
            $this->locale
131
        ));
132
    }
133
134
    /**
135
     * Canada Day.
136
     *
137
     * @link https://en.wikipedia.org/wiki/Canada_Day
138
     *
139
     * @throws InvalidDateException
140
     * @throws \InvalidArgumentException
141
     * @throws UnknownLocaleException
142
     * @throws \Exception
143
     */
144
    protected function calculateCanadaDay(): void
145
    {
146
        if ($this->year < 1983) {
147
            return;
148
        }
149
150
        $this->addHoliday(new Holiday(
151
            'canadaDay',
152
            [],
153
            new DateTime($this->year . '-07-01', DateTimeZoneFactory::getDateTimeZone($this->timezone)),
154
            $this->locale
155
        ));
156
    }
157
158
    /**
159
     * Civic Holiday.
160
     *
161
     * @link https://en.wikipedia.org/wiki/Civic_Holiday
162
     *
163
     * @throws InvalidDateException
164
     * @throws \InvalidArgumentException
165
     * @throws UnknownLocaleException
166
     * @throws \Exception
167
     */
168
    protected function calculateCivicHoliday(): void
169
    {
170
        if ($this->year < 1879) {
171
            return;
172
        }
173
174
        $this->addHoliday(new Holiday(
175
            'civicHoliday',
176
            [],
177
            new DateTime("first monday of august $this->year", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
178
            $this->locale
179
        ));
180
    }
181
182
    /**
183
     * Labour Day.
184
     *
185
     * @link https://en.wikipedia.org/wiki/Labour_Day
186
     *
187
     * @throws InvalidDateException
188
     * @throws \InvalidArgumentException
189
     * @throws UnknownLocaleException
190
     * @throws \Exception
191
     */
192
    private function calculateLabourDay(): void
193
    {
194
        if ($this->year < 1894) {
195
            return;
196
        }
197
198
        $this->addHoliday(new Holiday(
199
            'labourDay',
200
            [],
201
            new DateTime("first monday of september $this->year", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
202
            $this->locale
203
        ));
204
    }
205
206
    /**
207
     * Thanksgiving.
208
     *
209
     * @link https://en.wikipedia.org/wiki/Thanksgiving_(Canada)
210
     *
211
     * @throws InvalidDateException
212
     * @throws \InvalidArgumentException
213
     * @throws UnknownLocaleException
214
     * @throws \Exception
215
     */
216
    protected function calculateThanksgivingDay(): void
217
    {
218
        if ($this->year < 1879) {
219
            return;
220
        }
221
222
        $this->addHoliday(new Holiday(
223
            'thanksgivingDay',
224
            [],
225
            new DateTime("second monday of october $this->year", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
226
            $this->locale
227
        ));
228
    }
229
230
    /**
231
     * Remembrance Day.
232
     *
233
     * @link https://en.wikipedia.org/wiki/Remembrance_Day_(Canada)
234
     *
235
     * @throws InvalidDateException
236
     * @throws \InvalidArgumentException
237
     * @throws UnknownLocaleException
238
     * @throws \Exception
239
     */
240
    protected function calculateRemembranceDay(): void
241
    {
242
        if ($this->year < 1919) {
243
            return;
244
        }
245
246
        $this->addHoliday(new Holiday(
247
            'remembranceDay',
248
            [],
249
            new DateTime("$this->year-11-11", DateTimeZoneFactory::getDateTimeZone($this->timezone)),
250
            $this->locale
251
        ));
252
    }
253
}
254