Test Failed
Push — master ( 7cb433...d93f46 )
by Andrés
03:27
created

CalendarioHispanohablanteCom   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 31
c 1
b 1
f 0
dl 0
loc 69
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A countryTranslations() 0 4 1
A baseUrl() 0 3 1
A holidays() 0 55 5
1
<?php
2
3
namespace Andreshg112\HolidaysPhp\Providers\Colombia;
4
5
use Andreshg112\HolidaysPhp\Holiday;
6
use Andreshg112\HolidaysPhp\HolidaysPhpException;
7
use Andreshg112\HolidaysPhp\Providers\BaseProvider;
8
use Jenssegers\Date\Date;
9
use Wa72\HtmlPageDom\HtmlPage;
10
11
class CalendarioHispanohablanteCom extends BaseProvider
12
{
13
    protected function baseUrl(): string
14
    {
15
        return 'https://calendariohispanohablante.com';
16
    }
17
18
    public function countryTranslations(): array
19
    {
20
        return [
21
            'es' => 'Colombia',
22
        ];
23
    }
24
25
    public function holidays(int $year = null): ?array
26
    {
27
        $year = $year ?? date('Y');
28
29
        $baseUrl = $this->baseUrl();
30
31
        $url = "{$baseUrl}/{$year}/calendario-colombia-{$year}.html";
32
33
        try {
34
            $html = file_get_contents($url);
35
        } catch (\Throwable $th) {
36
            throw HolidaysPhpException::notFound();
37
        }
38
39
        $page = new HtmlPage($html);
40
41
        $pList = $page->filter('.group-three > p');
42
43
        if ($pList->count() === 0) {
44
            throw HolidaysPhpException::unrecognizedStructure();
45
        }
46
47
        $holidays = [];
48
49
        $country = $this->country();
50
51
        /** @var \DOMElement */
52
        foreach ($pList as $pItem) {
53
            $pChildren = $pItem->childNodes;
54
55
            // It must contain 2 nodes, else, it must be something different than a date
56
            if ($pChildren->count() !== 2) {
57
                continue;
58
            }
59
60
            // The text is like "Viernes 1 de Enero", so it has to be separated
61
            [$day, $spanishDate] = explode(' ', trim($pChildren->item(0)->textContent), 2);
62
63
            // The ending : has to be removed
64
            $spanishDate = rtrim($spanishDate, ':');
65
66
            Date::setLocale($this->getLanguage());
67
68
            // The date looks this way "1 de Enero 2021"
69
            $date = Date::createFromFormat('j \d\e F Y', "{$spanishDate} {$year}");
70
71
            $holidays[] = new Holiday(
72
                $country,
73
                $date->setTime(0, 0),
0 ignored issues
show
Bug introduced by
It seems like $date->setTime(0, 0) can also be of type false; however, parameter $date of Andreshg112\HolidaysPhp\Holiday::__construct() does only seem to accept Jenssegers\Date\Date, maybe add an additional type check? ( Ignorable by Annotation )

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

73
                /** @scrutinizer ignore-type */ $date->setTime(0, 0),
Loading history...
74
                trim($pChildren->item(1)->textContent), // title
75
                $this->getLanguage()
76
            );
77
        }
78
79
        return $holidays;
80
    }
81
}
82