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

Period   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A days() 0 8 1
A __construct() 0 10 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
}