Completed
Push — master ( 1758f1...601966 )
by Felix
08:03
created

Period::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace SchulzeFelix\SearchConsole;
4
5
use DateTime;
6
use Carbon\Carbon;
7
use SchulzeFelix\SearchConsole\Exceptions\InvalidPeriod;
8
9
10
class Period
11
{
12
    /** @var \DateTime */
13
    public $startDate;
14
15
    /** @var \DateTime */
16
    public $endDate;
17
18
    public static function create(DateTime $startDate, $endDate): Period
19
    {
20
        return new static($startDate, $endDate);
21
    }
22
23
    /**
24
     * @param int $numberOfDays
25
     * @return Period
26
     *
27
     * Modified to match Google Search Console delay in data (3 Days) and PDT timezone
28
     */
29
    public static function days(int $numberOfDays): Period
30
    {
31
        $endDate = Carbon::today('PDT')->subDays(3);
32
33
        $startDate = Carbon::today('PDT')->subDays($numberOfDays + 2)->startOfDay();
34
35
        return new static($startDate, $endDate);
36
    }
37
38
    public function __construct(DateTime $startDate, DateTime $endDate)
39
    {
40
        if ($startDate > $endDate) {
41
            throw InvalidPeriod::startDateCannotBeAfterEndDate($startDate, $endDate);
42
        }
43
44
        $this->startDate = $startDate;
45
46
        $this->endDate = $endDate;
47
    }
48
}