Passed
Push — master ( ed2711...5bfb66 )
by Samuel
02:22
created

Recurrence::getFrequency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Recurrence\Model;
4
5
use Recurrence\Constraint\DatetimeConstraint\DatetimeConstraintInterface;
6
use Recurrence\Constraint\ProviderConstraint\ProviderConstraintInterface;
7
use Recurrence\Constraint\RecurrenceConstraintInterface;
8
9
/**
10
 * Class Recurrence
11
 * @package Recurrence\Model
12
 */
13
class Recurrence
14
{
15
    /**
16
     * @var Frequency
17
     */
18
    private $frequency;
19
20
    /**
21
     * @var \Datetime
22
     */
23
    private $periodStartAt;
24
25
    /**
26
     * @var \Datetime
27
     */
28
    private $periodEndAt = null;
29
30
    /**
31
     * @var integer
32
     */
33
    private $interval = 1;
34
35
    /**
36
     * @var integer
37
     */
38
    private $count = null;
39
40
    /**
41
     * @var array
42
     */
43
    private $constraints = [];
44
45
    /**
46
     * Recurrence constructor.
47
     */
48
    public function __construct()
49
    {
50 1
        $this->setPeriodStartAt(new \DateTime());
51 1
    }
52
53
    /**
54
     * @param \Datetime $periodStartAt
55
     * @return $this
56
     */
57
    public function setPeriodStartAt(\Datetime $periodStartAt)
58
    {
59 1
        $this->periodStartAt = $periodStartAt;
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * @return \Datetime
66
     */
67
    public function getPeriodStartAt()
68
    {
69 1
        return $this->periodStartAt;
70
    }
71
72
    /**
73
     * @param \Datetime $periodEndAt
74
     * @return $this
75
     */
76
    public function setPeriodEndAt(\Datetime $periodEndAt)
77
    {
78
        $this->periodEndAt = $periodEndAt;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return \Datetime
85
     */
86
    public function getPeriodEndAt()
87
    {
88
        return $this->periodEndAt;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function hasPeriodEndAt()
95
    {
96
        return $this->periodEndAt !== null;
97
    }
98
99
    /**
100
     * @param Frequency $frequency
101
     * @return $this
102
     */
103
    public function setFrequency(Frequency $frequency)
104
    {
105
        $this->frequency = $frequency;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return Frequency
112
     */
113
    public function getFrequency()
114
    {
115
        return $this->frequency;
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    public function getInterval()
122
    {
123
        return $this->interval;
124
    }
125
126
    /**
127
     * @param int $interval
128
     * @return $this
129
     */
130
    public function setInterval($interval)
131
    {
132
        $this->interval = $interval;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return int
139
     */
140
    public function getCount()
141
    {
142
        return $this->count;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function hasCount()
149
    {
150
        return $this->count !== null;
151
    }
152
153
    /**
154
     * @param int $count
155
     * @return $this
156
     */
157
    public function setCount($count)
158
    {
159
        $this->count = $count;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function getConstraints()
168
    {
169 1
        return $this->constraints;
170
    }
171
172
    /**
173
     * @param array $constraints
174
     * @return $this
175
     */
176
    public function setConstraints(array $constraints)
177
    {
178 1
        $this->constraints = [];
179
180 1
        foreach ($constraints as $constraint) {
181 1
            $this->addConstraint($constraint);
182
        }
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public function hasConstraints()
191
    {
192 1
        return !empty($this->constraints);
193
    }
194
195
    /**
196
     * @param RecurrenceConstraintInterface $constraint
197
     * @return $this
198
     */
199
    public function addConstraint(RecurrenceConstraintInterface $constraint)
200
    {
201 1
        if ($this->hasConstraint(get_class($constraint))) {
202 1
            throw new \InvalidArgumentException(sprintf('Duplicate constraint [%s]', get_class($constraint)));
203
        }
204
205 1
        $this->constraints[] = $constraint;
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * @param string $constraintClassName
212
     * @return $this
213
     */
214
    public function removeConstraint($constraintClassName)
215
    {
216 1
        foreach ($this->constraints as $key => $constraint) {
217 1
            if (get_class($constraint) == $constraintClassName) {
218 1
                unset($this->constraints[$key]);
219
220 1
                break;
221
            }
222
        }
223
224 1
        $this->constraints = array_values($this->constraints);
225
226 1
        return $this;
227
    }
228
229
    /**
230
     * @param string $constraintClassName
231
     * @return bool
232
     */
233
    public function hasConstraint($constraintClassName)
234
    {
235 1
        foreach ($this->constraints as $key => $constraint) {
236 1
            if (get_class($constraint) == $constraintClassName) {
237 1
                return true;
238
            }
239
        }
240
241 1
        return false;
242
    }
243
244
    /**
245
     * @return bool
246
     */
247
    public function hasProviderConstraint()
248
    {
249 1
        foreach ($this->constraints as $key => $constraint) {
250 1
            if ($constraint instanceof ProviderConstraintInterface) {
251 1
                return true;
252
            }
253
        }
254
255 1
        return false;
256
    }
257
258
    /**
259
     * @return bool
260
     */
261
    public function hasDatetimeConstraint()
262
    {
263 1
        foreach ($this->constraints as $key => $constraint) {
264 1
            if ($constraint instanceof DatetimeConstraintInterface) {
265 1
                return true;
266
            }
267
        }
268
269 1
        return false;
270
    }
271
}
272