Completed
Push — develop ( f3d51f...285f08 )
by Abdelrahman
01:22
created

Period::__construct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 6
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 = '')
0 ignored issues
show
Unused Code introduced by
The parameter $interval is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

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