Passed
Push — master ( c2e69f...e5bfaf )
by Samuel
43s
created

DatetimeProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B provide() 0 31 4
1
<?php
2
3
namespace Recurrence;
4
5
/**
6
 * Class DatetimeProvider
7
 * @package Recurrence
8
 */
9
class DatetimeProvider
10
{
11
    /**
12
     * @param Recurrence $recurrence
13
     * @return array<\DateTime>
14
     */
15
    public function provide(Recurrence $recurrence)
16
    {
17 1
        $interval = $recurrence->getFrequency()->convertToDateIntervalFormat();
18
19
        // Transform interval in Datetime interval expression
20 1
        if ($recurrence->getInterval() !== 1) {
21 1
            $interval = str_replace('1', $recurrence->getInterval(), $interval);
22 1
        }
23
24 1
        $dateInterval = new \DateInterval($interval);
25
26
        // Estimate end date in case of count option
27 1
        $periodEndAt = $recurrence->getPeriodEndAt();
28 1
        if ($recurrence->hasCount()) {
29 1
            $periodEndAt   = clone $recurrence->getPeriodStartAt();
30 1
            $periodEndAt->modify(str_replace('1', ($recurrence->getCount() + 1), $recurrence->getFrequency()->convertToDateTimeFormat()));
31 1
        }
32
33 1
        $recurrences = iterator_to_array(new \DatePeriod(
34 1
            $recurrence->getPeriodStartAt(),
35 1
            $dateInterval,
36
            $periodEndAt
37 1
        ));
38
39
        // When having count option, return only amount of recurrences requested
40 1
        if ($recurrence->hasCount()) {
41 1
            return array_slice($recurrences, 0, $recurrence->getCount());
42
        }
43
44 1
        return $recurrences;
45
    }
46
}
47