Passed
Pull Request — master (#1)
by Kauri
03:27
created

PaymentScheduleConfig   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A extractIntervalLength() 0 17 3
A getNoOfPayments() 0 4 1
A getStartDate() 0 4 1
A getAverageIntervalLength() 0 4 1
A getDateInterval() 0 4 1
A getFirstPaymentDate() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan;
6
7
8
class PaymentScheduleConfig implements PaymentScheduleConfigInterface
9
{
10
    private $noOfPayments;
11
    private $startDate;
12
    private $averageIntervalLength = 0;
13
    private $dateInterval;
14
    private $firstPaymentDate;
15
16
    /**
17
     * PaymentScheduleConfig constructor.
18
     * @param int $noOfPayments
19
     * @param \DateTimeInterface $startDate
20
     * @param string $dateIntervalPattern
21
     * @param \DateTimeInterface|null $firstPaymentDate
22
     */
23
    public function __construct(
24
        int $noOfPayments,
25
        \DateTimeInterface $startDate,
26
        string $dateIntervalPattern,
27
        \DateTimeInterface $firstPaymentDate = null
28
    ) {
29
        $this->noOfPayments = $noOfPayments;
30
        $this->startDate = new \DateTime($startDate->format('Y-m-d'), new \DateTimeZone('UTC'));
31
32
        if (!is_null($firstPaymentDate)) {
33
            $this->firstPaymentDate = new \DateTime($firstPaymentDate->format('Y-m-d'), new \DateTimeZone('UTC'));
34
        }
35
36
        $this->dateInterval = new \DateInterval($dateIntervalPattern);
37
38
        $this->averageIntervalLength = $this->extractIntervalLength($this->dateInterval);
39
    }
40
41
    /**
42
     * @param \DateInterval $dateInterval
43
     * @return int
44
     */
45
    private function extractIntervalLength(\DateInterval $dateInterval): int
46
    {
47
        $intervalLength = 0;
48
        $intervalMultiplier = array(
49
            'd' => 1,
50
            'm' => 30,
51
            'y' => 30 * 12
52
        );
53
54
        foreach ($intervalMultiplier as $pattern => $multiplier) {
55
            if ($dateInterval->format('%' . $pattern) > 0) {
56
                $intervalLength = $intervalLength + (int) $dateInterval->format('%' . $pattern) * $multiplier;
57
            }
58
        }
59
60
        return (int) $intervalLength;
61
    }
62
63
    /**
64
     * @return int
65
     */
66
    public function getNoOfPayments(): int
67
    {
68
        return $this->noOfPayments;
69
    }
70
71
    /**
72
     * @return \DateTimeInterface
73
     */
74
    public function getStartDate(): \DateTimeInterface
75
    {
76
        return $this->startDate;
77
    }
78
79
    /**
80
     * @return int
81
     */
82
    public function getAverageIntervalLength(): int
83
    {
84
        return $this->averageIntervalLength;
85
    }
86
87
    /**
88
     * @return \DateInterval
89
     */
90
    public function getDateInterval(): \DateInterval
91
    {
92
        return $this->dateInterval;
93
    }
94
95
    /**
96
     * @return \DateTimeInterface|null
97
     */
98
    public function getFirstPaymentDate(): ?\DateTimeInterface
99
    {
100
        return $this->firstPaymentDate;
101
    }
102
}