Period::length()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Helpers\Time;
4
5
use Carbon\Carbon;
6
use Spatie\Analytics\Exceptions\InvalidPeriod;
7
use Spatie\Analytics\Period as SpatiePeriod;
8
9
class Period extends SpatiePeriod
10
{
11
    /**
12
     * @var Carbon
13
     */
14
    public $startDate;
15
16
    /**
17
     * @var Carbon
18
     */
19
    public $endDate;
20
21
    /**
22
     * PeriodService constructor.
23
     *
24
     * @param Carbon $startDate
25
     * @param Carbon $endDate
26
     * @throws InvalidPeriod
27
     */
28
    public function __construct(Carbon $startDate, Carbon $endDate)
29
    {
30
        parent::__construct($startDate, $endDate);
31
    }
32
33
    /**
34
     * Retrieve the Period's length in DateInterval.
35
     *
36
     * @return int
37
     */
38
    public function length(): int
39
    {
40
        return $this->startDate->diff($this->endDate)->days ?? 0;
41
    }
42
43
    /**
44
     * Offset the Period by its length in days.
45
     *
46
     * @return SpatiePeriod
47
     */
48
    public function offsetPeriod(): SpatiePeriod
49
    {
50
        // Get the Period's length (in days)
51
        $length = $this->length();
52
53
        // Offset the period by the length
54
        return $this->create($this->startDate->subDays($length), $this->endDate->subDays($length));
55
    }
56
}
57