Period   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A offsetPeriod() 0 7 1
A length() 0 3 1
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