1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scheduler\Job; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use Recurr\Rule as RecurrRule; |
7
|
|
|
use Recurr\Recurrence; |
8
|
|
|
use Recurr\Transformer\ArrayTransformer; |
9
|
|
|
use Recurr\Transformer\Constraint\AfterConstraint; |
10
|
|
|
use Recurr\Transformer\Constraint\BetweenConstraint; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RRule |
14
|
|
|
* @package Scheduler\Job |
15
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class RRule extends AbstractRule |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param DateTimeInterface $from |
22
|
|
|
* @param DateTimeInterface $to |
23
|
|
|
* @param boolean $inc |
24
|
|
|
* @throws |
25
|
|
|
* @return DateTimeInterface[] |
26
|
|
|
*/ |
27
|
13 |
View Code Duplication |
public function getRecurrences(DateTimeInterface $from, DateTimeInterface $to, $inc = true) |
|
|
|
|
28
|
|
|
{ |
29
|
13 |
|
$rRule = new RecurrRule($this->getRrule(), $this->getStartDate()); |
30
|
13 |
|
$rRuleTransformer = new ArrayTransformer(); |
31
|
13 |
|
$constraint = new BetweenConstraint($from, $to, $inc); |
32
|
13 |
|
$recurrenceCollection = $rRuleTransformer->transform($rRule, $constraint); |
33
|
13 |
|
$result = []; |
34
|
|
|
/** @var Recurrence $recurrence */ |
35
|
13 |
|
foreach ($recurrenceCollection as $recurrence) { |
36
|
12 |
|
$result[] = $recurrence->getStart(); |
37
|
|
|
} |
38
|
13 |
|
return $result; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param DateTimeInterface $from |
43
|
|
|
* @param boolean $inc including $from and $to dates |
44
|
|
|
* @return DateTimeInterface|null date of the next recurrence or null of no more recurrences scheduled. |
45
|
|
|
* @throws |
46
|
|
|
*/ |
47
|
1 |
View Code Duplication |
public function getNextRecurrence(DateTimeInterface $from, $inc = true) |
|
|
|
|
48
|
|
|
{ |
49
|
1 |
|
$rRule = new RecurrRule($this->getRrule(), $this->getStartDate()); |
50
|
1 |
|
$rRuleTransformer = new ArrayTransformer(); |
51
|
1 |
|
$constraint = new AfterConstraint($from, $inc); |
52
|
1 |
|
$recurrenceCollection = $rRuleTransformer->transform($rRule, $constraint); |
53
|
1 |
|
$result = null; |
54
|
|
|
/** @var Recurrence $recurrence */ |
55
|
1 |
|
foreach ($recurrenceCollection as $recurrence) { |
56
|
1 |
|
$result = $recurrence->getStart(); |
57
|
1 |
|
break; |
58
|
|
|
} |
59
|
1 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.