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

getPaymentAmounts()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 4
nop 4
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan\PaymentAmountCalculator;
6
7
use Kauri\Loan\PaymentAmountCalculatorInterface;
8
9
10
class AnnuityPaymentAmountCalculator implements PaymentAmountCalculatorInterface
11
{
12
    /**
13
     * @see http://www.financeformulas.net/Annuity_Payment_Formula.html
14
     * @see http://www.tvmcalcs.com/index.php/calculators/apps/lease_payments
15
     * @see http://www.ms.uky.edu/~rkremer/files/Finance04.pdf
16
     * @param array $periods
17
     * @param float $presentValue
18
     * @param float $interestRate
19
     * @param float $futureValue
20
     * @return array
21
     */
22 6
    public function getPaymentAmounts(
23
        array $periods,
24
        float $presentValue,
25
        float $interestRate,
26
        float $futureValue = 0.0
27
    ): array {
28 6
        $paymentAmounts = array();
29
30 6
        $discountFactor = 0;
31 6
        $futureValueDenominator = 1;
32
33 6
        foreach ($periods as $sequenceNo => $periodsLength) {
34 6
            $partialDiscountFactor = pow(1 + ($interestRate / 360 / 100 * $periodsLength),
35 6
                -$sequenceNo);
36
37 6
            $discountFactor = $discountFactor + $partialDiscountFactor;
38
39 6
            $partialFutureValueDenominator = 1 + ($interestRate / 360 / 100 * $periodsLength);
40 6
            $futureValueDenominator = $futureValueDenominator * $partialFutureValueDenominator;
41
        }
42
43 6
        $paymentAmount = ($presentValue - ($futureValue / $futureValueDenominator)) / $discountFactor;
44
45 6
        foreach ($periods as $sequenceNo => $periodsLength) {
46 6
            $paymentAmounts[$sequenceNo] = $paymentAmount;
47
        }
48
49 6
        return $paymentAmounts;
50
    }
51
52
}
53