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

DatetimeProvider::provide()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 13
cts 15
cp 0.8667
rs 8.5806
cc 4
eloc 16
nc 8
nop 1
crap 4.0378
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