Failed Conditions
Pull Request — master (#8)
by Kauri
02:29
created

PaymentsCalculator::getPeriodLengths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan;
6
7
8
class PaymentsCalculator implements PaymentsCalculatorInterface
9
{
10
    /**
11
     * @var PaymentAmountCalculatorInterface
12
     */
13
    private $paymentAmountCalculator;
14
    /**
15
     * @var InterestAmountCalculatorInterface
16
     */
17
    private $interestAmountCalculator;
18
19
    /**
20
     * PaymentsCalculator constructor.
21
     * @param PaymentAmountCalculatorInterface $paymentAmountCalculator
22
     * @param InterestAmountCalculatorInterface $interestAmountCalculator
23
     */
24 4
    public function __construct(
25
        PaymentAmountCalculatorInterface $paymentAmountCalculator,
26
        InterestAmountCalculatorInterface $interestAmountCalculator
27
    ) {
28 4
        $this->paymentAmountCalculator = $paymentAmountCalculator;
29 4
        $this->interestAmountCalculator = $interestAmountCalculator;
30 4
    }
31
32
    /**
33
     * @param PaymentPeriodsInterface $paymentPeriods
34
     * @param float $amountOfPrincipal
35
     * @param float $yearlyInterestRate
36
     * @param int $calculationMode
37
     * @param float $futureValue
38
     * @return array
39
     */
40 4
    public function calculatePayments(
41
        PaymentPeriodsInterface $paymentPeriods,
42
        float $amountOfPrincipal,
43
        float $yearlyInterestRate,
44
        int $calculationMode,
45
        float $futureValue
46
    ): array {
47 4
        $payments = array();
48
49 4
        $periodLengths = $this->getPeriodLengths($paymentPeriods, $calculationMode);
50 4
        $paymentAmounts = $this->paymentAmountCalculator->getPaymentAmounts($periodLengths, $amountOfPrincipal,
51 4
            $yearlyInterestRate, 0);
52
53 4
        $principalLeft = $amountOfPrincipal;
54
55 4
        foreach ($paymentPeriods->getPeriods() as $period) {
56
            /**
57
             * Get payment amount
58
             */
59 4
            $paymentAmount = round($paymentAmounts[$period->getSequenceNo()], 2);
60
61
            /**
62
             * Get interest rate for period
63
             */
64 4
            $ratePerPeriod = $this->interestAmountCalculator->getPeriodInterestRate($yearlyInterestRate,
1 ignored issue
show
Bug introduced by
The method getPeriodInterestRate() does not seem to exist on object<Kauri\Loan\Intere...untCalculatorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65 4
                $period->getLength($calculationMode));
66
67
            /**
68
             * Calculate interest part
69
             */
70 4
            $interest = round($this->interestAmountCalculator->getInterestAmount($principalLeft, $ratePerPeriod), 2);
71
72
            /**
73
             * Calculate principal part
74
             */
75 4
            if ($period->getSequenceNo() < $paymentPeriods->getNoOfPeriods()) {
76 4
                $principal = $paymentAmount - $interest;
77
            } else {
78 4
                $principal = $principalLeft;
79
            }
80
81
            /**
82
             * Calculate balance left
83
             */
84 4
            $principalLeft = $principalLeft - $principal;
85
86
            /**
87
             * Compose payment data
88
             */
89
            $paymentData = array(
90 4
                'sequence_no' => $period->getSequenceNo(),
91 4
                'payment' => $interest + $principal,
92 4
                'principal' => $principal,
93 4
                'interest' => $interest,
94 4
                'principal_left' => $principalLeft,
95 4
                'period' => $period
96
            );
97
98 4
            $payments[$period->getSequenceNo()] = $paymentData;
99
        }
100
101 4
        return $payments;
102
    }
103
104
    /**
105
     * @param PaymentPeriodsInterface $paymentPeriods
106
     * @param int $lengthMode
107
     * @return array
108
     */
109 4
    private function getPeriodLengths(PaymentPeriodsInterface $paymentPeriods, int $lengthMode): array
110
    {
111 4
        $lengths = array();
112
113
        /** @var PeriodInterface $period */
114 4
        foreach ($paymentPeriods->getPeriods() as $period) {
115 4
            $lengths[$period->getSequenceNo()] = $period->getLength($lengthMode);
116
        }
117
118 4
        return $lengths;
119
    }
120
}
121