Passed
Push — master ( ad9856...9ee920 )
by Kauri
52s
created

EqualPrincipalPaymentAmountCalculator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 84
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getPaymentAmounts() 0 26 2
A getPeriodInterestRate() 0 5 1
A getPrincipalPart() 0 6 1
A getPaymentAmount() 0 16 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan\PaymentAmountCalculator;
6
7
use Kauri\Loan\PaymentAmountCalculatorInterface;
8
9
class EqualPrincipalPaymentAmountCalculator implements PaymentAmountCalculatorInterface
10
{
11
    /**
12
     * @param array $periods
13
     * @param float $presentValue
14
     * @param float $interestRate
15
     * @param float $futureValue
16
     * @return array
17
     */
18 4
    public function getPaymentAmounts(
19
        array $periods,
20
        float $presentValue,
21
        float $interestRate,
22
        float $futureValue
23
    ): array {
24 4
        $paymentAmounts = array();
25
26 4
        $principalLeft = $presentValue;
27 4
        $noOfPayments = count($periods);
28 4
        $principal = $this->getPrincipalPart($presentValue, $futureValue, $noOfPayments);
29
30 4
        foreach ($periods as $sequenceNo => $periodLength) {
31 4
            $ratePerPeriod = $this->getPeriodInterestRate($interestRate, $periodLength);
32 4
            $noOfRemainingPeriods = $noOfPayments - $sequenceNo + 1;
33
34 4
            $paymentAmount = $this->getPaymentAmount($principalLeft, $futureValue, $ratePerPeriod,
35
                $noOfRemainingPeriods);
36
37 4
            $principalLeft = $principalLeft - $principal;
38
39 4
            $paymentAmounts[$sequenceNo] = $paymentAmount;
40
        }
41
42 4
        return $paymentAmounts;
43
    }
44
45
    /**
46
     * @param $interestRate
47
     * @param $periodLength
48
     * @return float
49
     */
50 4
    private function getPeriodInterestRate(float $interestRate, float $periodLength): float
51
    {
52 4
        $periodRate = $interestRate / 360 * $periodLength;
53 4
        return (float) $periodRate;
54
    }
55
56
    /**
57
     * @param float $presentValue
58
     * @param float $futureValue
59
     * @param int $noOfPeriods
60
     * @return float
61
     */
62 4
    private function getPrincipalPart(float $presentValue, float $futureValue, int $noOfPeriods): float
63
    {
64 4
        $principal = ($presentValue - $futureValue) / $noOfPeriods;
65
66 4
        return (float) $principal;
67
    }
68
69
    /**
70
     * @param float $presentValue
71
     * @param float $futureValue
72
     * @param float $ratePerPeriod
73
     * @param int $numberOfPeriods
74
     * @return float
75
     */
76 4
    private function getPaymentAmount(
77
        float $presentValue,
78
        float $futureValue,
79
        float $ratePerPeriod,
80
        int $numberOfPeriods
81
    ): float {
82 4
        $principal = $this->getPrincipalPart($presentValue, $futureValue, $numberOfPeriods);
83
84 4
        if ($ratePerPeriod > 0) {
85 1
            $payment = $principal + $presentValue * ($ratePerPeriod / 100);
86
        } else {
87 3
            $payment = $principal;
88
        }
89
90 4
        return (float) $payment;
91
    }
92
}
93