Completed
Pull Request — master (#18)
by Samuel
03:55
created

Recurrence::getFrequency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Recurrence\Model;
4
use Recurrence\Constraint\RecurrenceConstraintInterface;
5
6
7
/**
8
 * Class Recurrence
9
 * @package Recurrence\Model
10
 */
11
class Recurrence
12
{
13
    /**
14
     * @var Frequency
15
     */
16
    private $frequency;
17
18
    /**
19
     * @var \Datetime
20
     */
21
    private $periodStartAt;
22
23
    /**
24
     * @var \Datetime
25
     */
26
    private $periodEndAt;
27
28
    /**
29
     * @var integer
30
     */
31
    private $interval = 1;
32
33
    /**
34
     * @var integer
35
     */
36
    private $count;
37
38
    /**
39
     * @var array
40
     */
41
    private $constraints = [];
42
43
    /**
44
     * Recurrence constructor.
45
     */
46
    public function __construct()
47
    {
48
        $this->setPeriodStartAt(new \DateTime());
49
    }
50
51
    /**
52
     * @param \Datetime $periodStartAt
53
     * @return $this
54
     */
55
    public function setPeriodStartAt(\Datetime $periodStartAt)
56
    {
57
        $this->periodStartAt = $periodStartAt;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return \Datetime
64
     */
65
    public function getPeriodStartAt()
66
    {
67
        return $this->periodStartAt;
68
    }
69
70
    /**
71
     * @param \Datetime $periodEndAt
72
     * @return $this
73
     */
74
    public function setPeriodEndAt(\Datetime $periodEndAt)
75
    {
76
        $this->periodEndAt = $periodEndAt;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return \Datetime
83
     */
84
    public function getPeriodEndAt()
85
    {
86
        return $this->periodEndAt;
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    public function hasPeriodEndAt()
93
    {
94
        return $this->periodEndAt !== null;
95
    }
96
97
    /**
98
     * @param Frequency $frequency
99
     * @return $this
100
     */
101
    public function setFrequency(Frequency $frequency)
102
    {
103
        $this->frequency = $frequency;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return Frequency
110
     */
111
    public function getFrequency()
112
    {
113
        return $this->frequency;
114
    }
115
116
    /**
117
     * @return int
118
     */
119
    public function getInterval()
120
    {
121
        return $this->interval;
122
    }
123
124
    /**
125
     * @param int $interval
126
     * @return $this
127
     */
128
    public function setInterval($interval)
129
    {
130
        $this->interval = $interval;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return int
137
     */
138
    public function getCount()
139
    {
140
        return $this->count;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146
    public function hasCount()
147
    {
148
        return $this->count !== null;
149
    }
150
151
    /**
152
     * @param int $count
153
     * @return $this
154
     */
155
    public function setCount($count)
156
    {
157
        $this->count = $count;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function getConstraints()
166
    {
167
        return $this->constraints;
168
    }
169
170
    /**
171
     * @param array $constraints
172
     * @return $this
173
     */
174
    public function setConstraints(array $constraints)
175
    {
176
        $this->constraints = [];
177
178
        foreach ($constraints as $constraint) {
179
            $this->addConstraint($constraint);
180
        }
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function hasConstraints()
189
    {
190
        return !empty($this->constraints);
191
    }
192
193
    /**
194
     * @param RecurrenceConstraintInterface $constraint
195
     * @return $this
196
     */
197
    public function addConstraint(RecurrenceConstraintInterface $constraint)
198
    {
199
        if ($this->hasConstraint($constraint)) {
0 ignored issues
show
Documentation introduced by
$constraint is of type object<Recurrence\Constr...nceConstraintInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
200
            throw new \InvalidArgumentException(sprintf('Duplicate constraint : %', basename(get_class($constraint))));
201
        }
202
203
        $this->constraints[] = $constraint;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @param RecurrenceConstraintInterface $constraint
210
     * @return $this
211
     */
212
    public function removeConstraint(RecurrenceConstraintInterface $constraint)
213
    {
214
        if ($key = $this->hasConstraint($constraint)) {
0 ignored issues
show
Documentation introduced by
$constraint is of type object<Recurrence\Constr...nceConstraintInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
215
            unset($this->constraints[$key]);
216
        }
217
218
        return $this;
219
    }
220
221
    /**
222
     * @param string $constraintClassName
223
     * @return bool
224
     */
225
    public function hasConstraint($constraintClassName)
226
    {
227
        foreach ($this->constraints as $key => $constraint) {
228
            if (get_class($constraint) == $constraintClassName) {
229
                return true;
230
            }
231
        }
232
233
        return false;
234
    }
235
}
236