Passed
Push — master ( 07d32f...817bfa )
by Kauri
03:00
created

PaymentPeriodsFactory::calculatePeriodStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan;
6
7
8
/**
9
 * Class PaymentPeriodsFactory
10
 * @package Kauri\Loan
11
 */
12
class PaymentPeriodsFactory implements PaymentPeriodsFactoryInterface
13
{
14
    /**
15
     * @param PaymentScheduleInterface $paymentSchedule
16
     * @return PaymentPeriodsInterface
17
     */
18 3
    public static function generate(PaymentScheduleInterface $paymentSchedule): PaymentPeriodsInterface
19
    {
20 3
        $periods = new PaymentPeriods($paymentSchedule->getConfig()->getAverageIntervalLength());
21
22 3
        $periodStart = clone $paymentSchedule->getConfig()->getStartDate();
23
24 3
        foreach ($paymentSchedule->getPaymentDates() as $paymentNo => $paymentDate) {
25 3
            $periodStart = self::calculatePeriodStart($periodStart);
26 3
            $periodEnd = self::calculatePeriodEnd($paymentDate);
27
28 3
            $period = new Period($periodStart, $periodEnd);
29 3
            $periods->add($period, $paymentNo);
30
31 3
            $periodStart = clone $paymentDate;
32
        }
33
34 3
        return $periods;
35
    }
36
37
    /**
38
     * @param \DateTimeInterface $periodStart
39
     * @return \DateTimeInterface
40
     */
41 3
    private static function calculatePeriodStart(\DateTimeInterface $periodStart): \DateTimeInterface
42
    {
43 3
        $periodStart = clone $periodStart;
44
        // Move to next day
45 3
        $periodStart->add(new \DateInterval('P1D'));
46
47 3
        return $periodStart;
48
    }
49
50
    /**
51
     * @param \DateTimeInterface $paymentDate
52
     * @return \DateTimeInterface
53
     */
54 3
    private static function calculatePeriodEnd(\DateTimeInterface $paymentDate): \DateTimeInterface
55
    {
56 3
        $periodEnd = clone $paymentDate;
57
        // Move to the end of the day
58 3
        $periodEnd->add(new \DateInterval('P1D'))->sub(new \DateInterval('PT1S'));
59 3
        return $periodEnd;
60
    }
61
}
62