Completed
Push — master ( 3bb3f6...b96e37 )
by Vladimir
02:38
created

EqualPrincipalPaymentScheduleCalculator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.29%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 97
ccs 25
cts 28
cp 0.8929
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A calculatePaymentInterest() 0 4 1
A __construct() 0 10 1
A getTotalInterest() 0 4 1
A setTotalInterest() 0 4 1
B calculateSchedule() 0 27 2
1
<?php
2
/**
3
 * @author: Vova Lando <[email protected]>
4
 * @package: LoanPaymentsCalculator
5
 * @subpackage:
6
 * @created: 05/09/2017 16:56
7
 */
8
9
namespace cog\LoanPaymentsCalculator\PaymentSchedule;
10
11
use cog\LoanPaymentsCalculator\Payment\Payment;
12
use cog\LoanPaymentsCalculator\Period\Period;
13
use cog\LoanPaymentsCalculator\Schedule\Schedule;
14
15
/**
16
 * Class EqualPrincipalPaymentScheduleCalculator
17
 * @package cog\LoanPaymentsCalculator\PaymentSchedule
18
 */
19
class EqualPrincipalPaymentScheduleCalculator implements PaymentScheduleCalculator
20
{
21
    /**
22
     * @var Period[]
23
     */
24
    private $schedulePeriods;
25
26
    /**
27
     * @var float
28
     */
29
    private $principalAmount;
30
31
    /**
32
     * @var float
33
     */
34
    private $dailyInterestRate;
35
36
    /**
37
     * @var float
38
     */
39
    private $totalInterest;
40
41
    /**
42
     * PaymentSchedule constructor.
43
     * @param Period[] $schedulePeriods
44
     * @param float    $principalAmount
45
     * @param float    $dailyInterestRate
46
     */
47 2
    public function __construct(
48
        $schedulePeriods,
49
        $principalAmount,
50
        $dailyInterestRate
51
    ){
0 ignored issues
show
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found 0 spaces
Loading history...
52 2
        $this->schedulePeriods = $schedulePeriods;
53 2
        $this->principalAmount = $principalAmount;
54 2
        $this->dailyInterestRate = $dailyInterestRate;
55 2
        $this->totalInterest = 0.0;
56 2
    }
57
58
    /**
59
     * @return float
60
     */
61 2
    public function getTotalInterest()
62
    {
63 2
        return $this->totalInterest;
64
    }
65
66
    /**
67
     * @param float $totalInterest
68
     */
69
    public function setTotalInterest($totalInterest)
70
    {
71
        $this->totalInterest = $totalInterest;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 2
    public function calculateSchedule()
78
    {
79
        /**
80
         * @var Payment[] $payments
81
         */
82 2
        $payments = [];
83 2
        $numberOfPeriods = count($this->schedulePeriods);
84 2
        $paymentPrincipal = $this->principalAmount/$numberOfPeriods;
85 2
        $totalPrincipalToPay = $this->principalAmount;
86
87 2
        for ($i=0; $i<$numberOfPeriods ; $i++) {
0 ignored issues
show
Coding Style introduced by
Space found before second semicolon of FOR loop
Loading history...
88 2
            $payment = new Payment($this->schedulePeriods[$i]);
89
            // Payment principal
90 2
            $payment->setPrincipal($paymentPrincipal);
91
            // Payment interest
92 2
            $paymentInterest = $this->calculatePaymentInterest($totalPrincipalToPay, $this->dailyInterestRate, $payment->getPeriod()->daysLength);
93 2
            $payment->setInterest($paymentInterest);
94
            // Payment totals
95 2
            $totalPrincipalToPay-=$paymentPrincipal;
96 2
            $payment->setPrincipalBalanceLeft($totalPrincipalToPay);
97
98 2
            $payments[] = $payment;
99 2
            $this->totalInterest += $paymentInterest;
100
        }
101
102 2
        return $payments;
103
    }
104
105
    /**
106
     * @param float   $remainingPrincipalAmount
107
     * @param float   $dailyInterestRate
108
     * @param integer $periodInDays
109
     * @return float
110
     */
111 2
    private function calculatePaymentInterest($remainingPrincipalAmount, $dailyInterestRate, $periodInDays)
112
    {
113 2
        return $remainingPrincipalAmount*$dailyInterestRate*$periodInDays;
114
    }
115
}