Period::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Subscriptions\Services;
6
7
use Carbon\Carbon;
8
9
class Period
10
{
11
    /**
12
     * Starting date of the period.
13
     *
14
     * @var string
15
     */
16
    protected $start;
17
18
    /**
19
     * Ending date of the period.
20
     *
21
     * @var string
22
     */
23
    protected $end;
24
25
    /**
26
     * Interval.
27
     *
28
     * @var string
29
     */
30
    protected $interval;
31
32
    /**
33
     * Interval count.
34
     *
35
     * @var int
36
     */
37
    protected $period = 1;
38
39
    /**
40
     * Create a new Period instance.
41
     *
42
     * @param string $interval
43
     * @param int    $count
44
     * @param string $start
45
     *
46
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
47
     */
48
    public function __construct($interval = 'month', $count = 1, $start = '')
49
    {
50
        $this->interval = $interval;
51
52
        if (empty($start)) {
53
            $this->start = Carbon::now();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Carbon\Carbon::now() of type object<Carbon\Carbon> is incompatible with the declared type string of property $start.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        } elseif (! $start instanceof Carbon) {
55
            $this->start = new Carbon($start);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Carbon\Carbon($start) of type object<Carbon\Carbon> is incompatible with the declared type string of property $start.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        } else {
57
            $this->start = $start;
58
        }
59
60
        $this->period = $count;
61
        $start = clone $this->start;
62
        $method = 'add'.ucfirst($this->interval).'s';
63
        $this->end = $start->{$method}($this->period);
64
    }
65
66
    /**
67
     * Get start date.
68
     *
69
     * @return \Carbon\Carbon
70
     */
71
    public function getStartDate(): Carbon
72
    {
73
        return $this->start;
74
    }
75
76
    /**
77
     * Get end date.
78
     *
79
     * @return \Carbon\Carbon
80
     */
81
    public function getEndDate(): Carbon
82
    {
83
        return $this->end;
84
    }
85
86
    /**
87
     * Get period interval.
88
     *
89
     * @return string
90
     */
91
    public function getInterval(): string
92
    {
93
        return $this->interval;
94
    }
95
96
    /**
97
     * Get period interval count.
98
     *
99
     * @return int
100
     */
101
    public function getIntervalCount(): int
102
    {
103
        return $this->period;
104
    }
105
}
106