Passed
Push — master ( 79b3fb...96316b )
by Andrés
02:29
created

CalendarioHispanohablanteCom::holidays()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 65
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6.0296

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 32
c 2
b 1
f 0
nc 6
nop 1
dl 0
loc 65
ccs 29
cts 32
cp 0.9063
crap 6.0296
rs 8.7857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 1
            if (!$date) {
74
                throw HolidaysPhpException::unrecognizedDate();
75
            }
76
77
            // Remove it so the text inside the $crawler is only the title of the holiday
78 1
            $dateContainer->remove();
79
80 1
            $titleContainer = $crawler->first();
81
82 1
            $holidays[] = new Holiday(
83 1
                $country,
84 1
                $date,
85 1
                $titleContainer->text(),
86 1
                $this->getLanguage()
87
            );
88
        }
89
90 1
        return $holidays;
91
    }
92
}
93