ExcludeWeekendConstraint   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
dl 0
loc 16
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isWeekend() 0 3 1
A apply() 0 9 2
1
<?php
2
3
namespace Recurrence\Constraint\DatetimeConstraint;
4
5
use Recurrence\Constraint\RecurrenceConstraintInterface;
6
use Recurrence\Model\Recurrence;
7
8
class ExcludeWeekendConstraint implements DatetimeConstraintInterface, RecurrenceConstraintInterface
9
{
10
    public function apply(Recurrence $recurrence, \Datetime $datetime): \Datetime
11
    {
12 1
        if ($this->isWeekend($datetime)) {
13
            // Add 1 or 2 days to skip weekend (we didn't use `next monday` pattern of \Datetime::format cause it remove time)
14 1
            $days = (7 - (int) $datetime->format('N') + 1);
15 1
            $datetime->modify(sprintf('+%d days', $days));
16
        }
17
18 1
        return $datetime;
19
    }
20
21
    private function isWeekend(\Datetime$datetime): bool
22
    {
23 1
        return ((int) $datetime->format('N') >= 6);
24
    }
25
}
26