Fallback::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
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