Passed
Pull Request — master (#5)
by Kauri
02:36
created

getPrincipalPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
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 3
    public function getPaymentAmounts(
19
        array $periods,
20
        float $presentValue,
21
        float $interestRate,
22
        float $futureValue
23
    ): array {
24 3
        $paymentAmounts = array();
25
26 3
        $principalLeft = $presentValue;
27 3
        $noOfPayments = count($periods);
28 3
        $principal = $this->getPrincipalPart($presentValue, $futureValue, $noOfPayments);
29
30 3
        foreach ($periods as $sequenceNo => $periodLength) {
31 3
            $ratePerPeriod = $this->getPeriodInterestRate($interestRate, $periodLength);
32 3
            $noOfRemainingPeriods = $noOfPayments - $sequenceNo + 1;
33
34 3
            $paymentAmount = $this->getPaymentAmount($principalLeft, $futureValue, $ratePerPeriod,
35
                $noOfRemainingPeriods);
36
37 3
            $principalLeft = $this->decreasePrincipalLeft($principalLeft, $principal);
38
39 3
            $paymentAmounts[$sequenceNo] = $paymentAmount;
40
        }
41
42 3
        return $paymentAmounts;
43
    }
44
45
    /**
46
     * @param float $presentValue
47
     * @param float $discount
48
     * @return float
49
     */
50 3
    private function decreasePrincipalLeft(float $presentValue, float $discount)
51
    {
52 3
        return (float) ($presentValue - $discount);
53
    }
54
55
    /**
56
     * @param $interestRate
57
     * @param $periodLength
58
     * @return float
59
     */
60 3
    private function getPeriodInterestRate(float $interestRate, float $periodLength): float
61
    {
62 3
        $periodRate = $interestRate / 360 * $periodLength;
63 3
        return (float) $periodRate;
64
    }
65
66
    /**
67
     * @param float $presentValue
68
     * @param float $futureValue
69
     * @param int $noOfPeriods
70
     * @return float
71
     */
72 3
    private function getPrincipalPart(float $presentValue, float $futureValue, int $noOfPeriods): float
73
    {
74 3
        $principal = ($presentValue - $futureValue) / $noOfPeriods;
75
76 3
        return (float) $principal;
77
    }
78
79
    /**
80
     * @param float $presentValue
81
     * @param float $futureValue
82
     * @param float $ratePerPeriod
83
     * @param int $numberOfPeriods
84
     * @return float
85
     */
86 3
    private function getPaymentAmount(
87
        float $presentValue,
88
        float $futureValue,
89
        float $ratePerPeriod,
90
        int $numberOfPeriods
91
    ): float {
92 3
        $principal = $this->getPrincipalPart($presentValue, $futureValue, $numberOfPeriods);
93
94 3
        if ($ratePerPeriod > 0) {
95
            $payment = $principal + $presentValue * ($ratePerPeriod / 100);
96
        } else {
97 3
            $payment = $principal;
98
        }
99
100 3
        return (float) $payment;
101
    }
102
}
103