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

CalendarioHispanohablanteCom   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 34
c 2
b 1
f 0
dl 0
loc 75
ccs 32
cts 34
cp 0.9412
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A countryTranslations() 0 4 1
A baseUrl() 0 3 1
A holidays() 0 61 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
use Wa72\HtmlPageDom\HtmlPageCrawler;
11
12
class CalendarioHispanohablanteCom extends BaseProvider
13
{
14 2
    protected function baseUrl(): string
15
    {
16 2
        return 'https://calendariohispanohablante.com';
17
    }
18
19 3
    public function countryTranslations(): array
20
    {
21
        return [
22 3
            'es' => 'Colombia',
23
        ];
24
    }
25
26 2
    public function holidays(int $year = null): array
27
    {
28 2
        $year = $year ?? date('Y');
29
30 2
        $baseUrl = $this->baseUrl();
31
32 2
        $url = "{$baseUrl}/{$year}/calendario-colombia-{$year}.html";
33
34
        try {
35 2
            $html = file_get_contents($url);
36 1
        } catch (\Throwable $th) {
37 1
            throw HolidaysPhpException::notFound();
38
        }
39
40 1
        $page = new HtmlPage($html);
41
42 1
        $rows = $page->filter('.group-three > p');
43
44 1
        if ($rows->count() === 0) {
45
            throw HolidaysPhpException::unrecognizedStructure();
46
        }
47
48 1
        $holidays = [];
49
50 1
        $country = $this->country();
51
52 1
        foreach ($rows as $row) {
53 1
            $crawler = new HtmlPageCrawler($row);
54
55 1
            $dateContainer = $crawler->filter('b')->first();
56
57
            // This element must exist
58 1
            if ($dateContainer->count() === 0) {
59
                continue;
60
            }
61
62
            // The text is like "Viernes 1 de Enero", so it has to be separated
63 1
            [, $spanishDate] = explode(' ', $dateContainer->text(), 2);
64
65
            // The ending : has to be removed
66 1
            $spanishDate = rtrim($spanishDate, ':');
67
68 1
            Date::setLocale($this->getLanguage());
69
70
            // The date looks this way "1 de Enero 2021"
71 1
            $date = Date::createFromFormat('j \d\e F Y', "{$spanishDate} {$year}");
72
73
            // Remove it so the text inside the $crawler is only the title of the holiday
74 1
            $dateContainer->remove();
75
76 1
            $titleContainer = $crawler->first();
77
78 1
            $holidays[] = new Holiday(
79 1
                $country,
80 1
                $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

80
                /** @scrutinizer ignore-type */ $date->setTime(0, 0),
Loading history...
81 1
                $titleContainer->text(),
82 1
                $this->getLanguage()
83
            );
84
        }
85
86 1
        return $holidays;
87
    }
88
}
89