Passed
Branch main (2e4b54)
by Andreas
10:30
created

ObservanceDecorator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 15
c 1
b 0
f 0
dl 0
loc 44
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isWithinObservance() 0 7 4
A __construct() 0 5 1
A getName() 0 3 1
A isHoliday() 0 3 1
A dateMatches() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright Andreas Heigl <[email protected]>
7
 *
8
 * Licenses under the MIT-license. For details see the included file LICENSE.md
9
 */
10
11
namespace Org_Heigl\Holidaychecker;
12
13
use DateTimeInterface;
14
15
final class ObservanceDecorator implements HolidayIteratorItemInterface
16
{
17
    /** @var int|null */
18
    private $firstObservance;
19
20
    /** @var int|null */
21
    private $lastObservance;
22
23
	/** @var HolidayIteratorItemInterface */
24
	private $wrapped;
25
26
	public function __construct(HolidayIteratorItemInterface $wrapped, ?int $firstObservance, ?int $lastObservance)
27
    {
28
		$this->wrapped = $wrapped;
29
        $this->firstObservance = $firstObservance;
30
        $this->lastObservance = $lastObservance;
31
    }
32
33
    private function isWithinObservance(int $gregorianYear): bool
34
    {
35
        if (null !== $this->firstObservance && $this->firstObservance > $gregorianYear) {
36
            return false;
37
        }
38
39
        return null === $this->lastObservance || $this->lastObservance >= $gregorianYear;
40
    }
41
42
	public function dateMatches(DateTimeInterface $date): bool
43
	{
44
		if (! $this->isWithinObservance((int) $date->format('Y'))) {
45
			return false;
46
		}
47
48
		return $this->wrapped->dateMatches($date);
49
	}
50
51
	public function getName(): string
52
	{
53
		return $this->wrapped->getName();
54
	}
55
56
	public function isHoliday(): bool
57
	{
58
		return $this->wrapped->isHoliday();
59
	}
60
}
61