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

CalendarioDeColombiaCom::holidays()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 55
rs 9.1768

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
11
class CalendarioDeColombiaCom extends BaseProvider
12
{
13
    protected function baseUrl(): string
14
    {
15
        return 'https://calendariodecolombia.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}/calendario-{$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
        $rows = $page->filter(
42
            '#cuadro_festivos > div > .tabla_festivos1 > .formato_fechas, #cuadro_festivos > div > .tabla_festivos2 > .formato_fechas'
43
        );
44
45
        if ($rows->count() === 0) {
46
            throw HolidaysPhpException::unrecognizedStructure();
47
        }
48
49
        $holidays = [];
50
51
        $country = $this->country();
52
53
        /** @var \DOMElement */
54
        foreach ($rows as $row) {
55
            $nodeList = $row->childNodes;
56
57
            // It must contain this amount, else, it must be something different than a holiday
58
            if ($nodeList->count() !== 7) {
59
                continue;
60
            }
61
62
            /** @var \DOMElement */
63
            $timeElement = $nodeList->item(3);
64
65
            // The date is already in a field time
66
            $date = Date::parse($timeElement->getAttribute('datetime'));
67
68
            /** @var \DOMElement */
69
            $aElement = $nodeList->item(2);
70
71
            $holidays[] = new Holiday(
72
                $country,
73
                $date->setTime(0, 0),
74
                trim($aElement->getAttribute('title')), // title
75
                $this->getLanguage()
76
            );
77
        }
78
79
        return $holidays;
80
    }
81
}
82