Passed
Push — master ( 1fece0...af95a9 )
by Hector Luis
03:59
created

WeekDays   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 10 1
A __construct() 0 8 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Domain Policy Class
5
 * @package Ticaje_BookingApi
6
 * @author  Hector Luis Barrientos <[email protected]>
7
 */
8
9
namespace Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS\Constraint;
10
11
use DateInterval;
12
use DatePeriod;
13
use DateTime;
14
use Ticaje\BookingApi\Domain\Signatures\PeriodSignature;
15
use Ticaje\BookingApi\Domain\Signatures\WeekDaysSignature;
16
17
/**
18
 * Class WeekDays
19
 * @package Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS\Constraint
20
 */
21
class WeekDays implements PeriodSignature, WeekDaysSignature
22
{
23
    /** @var int|string */
24
    private $numberOfYearsOn;
25
26
    /** @var array */
27
    private $monthToCover;
28
29
    /**
30
     * WeekDays constructor.
31
     *
32
     * @param string $numberOfYearsOn
33
     * @param string $monthToCover
34
     */
35
    public function __construct(
36
        string $numberOfYearsOn,
37
        string $monthToCover
38
    ) {
39
        $this->numberOfYearsOn = $numberOfYearsOn ?? 1;
40
        $this->monthToCover = $monthToCover ? explode(',', $monthToCover) : [
41
            1,
42
            12,
43
        ];
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function extract(array $data): DatePeriod
50
    {
51
        $year = $data['year'];
52
        $yearTo = $year + $this->numberOfYearsOn;
53
        $weekDay = $data['weekDay'];
54
55
        return new DatePeriod(
56
            new DateTime("first $weekDay of $year-{$this->monthToCover[0]}"),
57
            DateInterval::createFromDateString("next $weekDay"),
58
            new DateTime("last day of $yearTo-{$this->monthToCover[1]}")
59
        );
60
    }
61
}
62