Fallback::holidays()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 12
c 1
b 1
f 0
nc 5
nop 1
dl 0
loc 24
ccs 10
cts 12
cp 0.8333
crap 4.074
rs 9.8666
1
<?php
2
3
namespace Andreshg112\HolidaysPhp;
4
5
use HaydenPierce\ClassFinder\ClassFinder;
6
7
class Fallback
8
{
9
    protected $country;
10
11
    protected $language;
12
13 1
    public function __construct(string $country, string $language)
14
    {
15 1
        $country = trim($country);
16
17 1
        if (!is_dir(__DIR__ . "/Providers/{$country}")) {
18
            throw HolidaysPhpException::unsupportedCountry();
19
        }
20
21 1
        $this->country = $country;
22
23 1
        $this->language = trim($language);
24 1
    }
25
26 1
    public function holidays(int $year = null): array
27
    {
28 1
        $namespace = __NAMESPACE__ . "\\Providers\\{$this->country}";
29
30 1
        $classes = ClassFinder::getClassesInNamespace($namespace);
31
32 1
        $holidays = [];
33
34 1
        foreach ($classes as $class) {
35
            try {
36
                /** @var \Andreshg112\HolidaysPhp\Providers\BaseProvider */
37 1
                $provider = new $class($this->language);
38
39 1
                $holidays = $provider->holidays($year);
40
41 1
                if (!empty($holidays)) {
42 1
                    break;
43
                }
44
            } catch (\Throwable $th) {
45
                continue;
46
            }
47
        }
48
49 1
        return $holidays;
50
    }
51
}
52