WeekDays   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A extract() 0 10 1
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\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\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 ?? self::NUMBER_OF_YEARS_TO_COME;
40
        $this->monthToCover = $monthToCover ? explode(',', $monthToCover) : self::MONTHS_RANGE;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function extract(array $data): DatePeriod
47
    {
48
        $year = $data['year'];
49
        $yearTo = $year + $this->numberOfYearsOn;
50
        $weekDay = $data['weekDay'];
51
52
        return new DatePeriod(
53
            new DateTime("first $weekDay of $year-{$this->monthToCover[0]}"),
54
            DateInterval::createFromDateString("next $weekDay"),
55
            new DateTime("last day of $yearTo-{$this->monthToCover[1]}")
56
        );
57
    }
58
}
59