Passed
Pull Request — master (#4)
by Kauri
02:33
created

AnnuityPaymentAmountCalculator::getPaymentAmount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 2
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
     * @param float $presentValue
15
     * @param float $ratePerPeriod
16
     * @param float $numberOfPeriods
17
     * @return float
18
     */
19 9
    public function getPaymentAmount(float $presentValue, float $ratePerPeriod, float $numberOfPeriods): float
20
    {
21 9
        if ($ratePerPeriod > 0) {
22 5
            $payment = (($ratePerPeriod / 100) * $presentValue) / (1 - pow(1 + ($ratePerPeriod / 100),
23 5
                        -$numberOfPeriods));
24
        } else {
25 4
            $payment = $presentValue / $numberOfPeriods;
26
        }
27
28 9
        return $payment;
29
    }
30
}
31