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

ExcludeDaysOfWeekConstraint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A apply() 0 8 2
1
<?php
2
3
namespace Recurrence\Constraint;
4
5
use Recurrence\Model\Recurrence;
6
7
/**
8
 * Class ExcludeDaysOfWeekConstraint
9
 * @package Recurrence\Constraint
10
 */
11
class ExcludeDaysOfWeekConstraint implements RecurrenceConstraintInterface
12
{
13
14
    /**
15
     * @var array
16
     */
17
    private $excludedDays = [];
18
19
    /**
20
     * @param array $excludedDays
21
     */
22
    public function __construct(array $excludedDays = [])
23
    {
24
        $this->excludedDays = array_unique($excludedDays);
25
26
        sort($this->excludedDays);
27
28
        if (count($this->excludedDays) >= 7) {
29
            throw new \InvalidArgumentException('At least one day of the week must be allowed');
30
        }
31
    }
32
33
    /**
34
     * @param Recurrence $recurrence
35
     * @param \Datetime $datetime
36
     * @return \Datetime
37
     */
38
    public function apply(Recurrence $recurrence, \Datetime $datetime)
39
    {
40
        while (in_array((int) $datetime->format('N'), $this->excludedDays)) {
41
            $datetime->modify('+1 day');
42
        }
43
44
        return $datetime;
45
    }
46
}
47