ExcludeDaysOfWeekConstraint   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 14
dl 0
loc 32
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0

2 Methods

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