Passed
Push — master ( b66789...fff5ae )
by Kauri
54s
created

PaymentAmountCalculator::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;
6
7
8
class PaymentAmountCalculator implements PaymentAmountCalculatorInterface
9
{
10
    /**
11
     * @see http://www.financeformulas.net/Annuity_Payment_Formula.html
12
     * @param float $presentValue
13
     * @param float $ratePerPeriod
14
     * @param float $numberOfPeriods
15
     * @return float
16
     */
17 6
    public function getPaymentAmount(float $presentValue, float $ratePerPeriod, float $numberOfPeriods): float
18
    {
19 6
        if ($ratePerPeriod > 0) {
20 3
            $payment = (($ratePerPeriod / 100) * $presentValue) / (1 - pow(1 + ($ratePerPeriod / 100),
21 3
                        -$numberOfPeriods));
22
        } else {
23 3
            $payment = $presentValue / $numberOfPeriods;
24
        }
25
26 6
        return $payment;
27
    }
28
}
29