Passed
Branch factorize (4cfa74)
by Samuel
01:51
created

DatetimeProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 86.67%

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 13
cts 15
cp 0.8667
rs 10

1 Method

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