Passed
Pull Request — master (#7)
by Andrés
02:29
created

CalendarioDeColombiaCom::baseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
use Wa72\HtmlPageDom\HtmlPageCrawler;
11
12
class CalendarioDeColombiaCom extends BaseProvider
13
{
14 1
    protected function baseUrl(): string
15
    {
16 1
        return 'https://calendariodecolombia.com';
17
    }
18
19 1
    public function countryTranslations(): array
20
    {
21
        return [
22 1
            'es' => 'Colombia',
23
        ];
24
    }
25
26 1
    public function holidays(int $year = null): array
27
    {
28 1
        $year = $year ?? date('Y');
29
30 1
        $baseUrl = $this->baseUrl();
31
32 1
        $url = "{$baseUrl}/calendario-{$year}.html";
33
34
        try {
35 1
            $html = file_get_contents($url);
36
        } catch (\Throwable $th) {
37
            throw HolidaysPhpException::notFound();
38
        }
39
40 1
        $page = new HtmlPage($html);
41
42 1
        $rows = $page->filter(
43 1
            '#cuadro_festivos > div > .tabla_festivos1 > .formato_fechas, #cuadro_festivos > div > .tabla_festivos2 > .formato_fechas'
44
        );
45
46 1
        if ($rows->count() === 0) {
47
            throw HolidaysPhpException::unrecognizedStructure();
48
        }
49
50 1
        $holidays = [];
51
52 1
        $country = $this->country();
53
54 1
        foreach ($rows as $row) {
55 1
            $crawler = new HtmlPageCrawler($row);
56
57 1
            $dateContainer = $crawler->filter('time')->first();
58
59
            // This element must exist
60 1
            if ($dateContainer->count() === 0) {
61
                continue;
62
            }
63
64
            // The date is already in a field time
65 1
            $date = Date::parse($dateContainer->getAttribute('datetime'));
66
67 1
            $titleContainer = $crawler->filter('a')->first();
68
69 1
            $holidays[] = new Holiday(
70 1
                $country,
71 1
                $date->setTime(0, 0),
72 1
                $titleContainer->getAttribute('title'), // title
1 ignored issue
show
Bug introduced by
It seems like $titleContainer->getAttribute('title') can also be of type null; however, parameter $title of Andreshg112\HolidaysPhp\Holiday::__construct() does only seem to accept string, 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

72
                /** @scrutinizer ignore-type */ $titleContainer->getAttribute('title'), // title
Loading history...
73 1
                $this->getLanguage()
74
            );
75
        }
76
77 1
        return $holidays;
78
    }
79
}
80