Completed
Pull Request — master (#18)
by Samuel
02:00
created

ExcludeWeekendConstraint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
wmc 3
lcom 0
cbo 0
ccs 5
cts 5
cp 1
rs 10

2 Methods

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