PaymentPeriodsFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 51
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 19 2
A calculatePeriodStart() 0 8 1
A calculatePeriodEnd() 0 7 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
21 3
        $periods = new PaymentPeriods($paymentSchedule->getConfig()->getAverageIntervalLength());
22
23 3
        $periodStart = clone $paymentSchedule->getConfig()->getStartDate();
24
25 3
        foreach ($paymentSchedule->getPaymentDates() as $paymentNo => $paymentDate) {
26 3
            $periodStart = self::calculatePeriodStart($periodStart);
27 3
            $periodEnd = self::calculatePeriodEnd($paymentDate);
28
29 3
            $period = new Period($periodStart, $periodEnd, $periods->getAvgPeriodLength());
30 3
            $periods->add($period, $paymentNo);
31
32 3
            $periodStart = clone $paymentDate;
33
        }
34
35 3
        return $periods;
36
    }
37
38
    /**
39
     * @param \DateTimeInterface $periodStart
40
     * @return \DateTimeInterface
41
     */
42 3
    private static function calculatePeriodStart(\DateTimeInterface $periodStart): \DateTimeInterface
43
    {
44 3
        $periodStart = clone $periodStart;
45
        // Move to next day
46 3
        $periodStart->add(new \DateInterval('P1D'));
47
48 3
        return $periodStart;
49
    }
50
51
    /**
52
     * @param \DateTimeInterface $paymentDate
53
     * @return \DateTimeInterface
54
     */
55 3
    private static function calculatePeriodEnd(\DateTimeInterface $paymentDate): \DateTimeInterface
56
    {
57 3
        $periodEnd = clone $paymentDate;
58
        // Move to the end of the day
59 3
        $periodEnd->add(new \DateInterval('P1D'))->sub(new \DateInterval('PT1S'));
60 3
        return $periodEnd;
61
    }
62
}
63