SimpleInterestCalculator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 2
cbo 5
dl 0
loc 138
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setPrincipal() 0 4 1
A setAnnualInterestRate() 0 4 1
A setTime() 0 4 1
A getPrincipal() 0 4 1
A getAnnualInterestRate() 0 4 1
A getTime() 0 4 1
A getTimeInYears() 0 4 1
A getTimeInMonths() 0 4 1
A getTimeInDays() 0 4 1
A getInterestNumber() 0 10 1
A getInterestDivisor() 0 10 1
A getInterestAmount() 0 8 1
1
<?php
2
3
namespace FinanCalc\Calculators {
4
5
    use Exception;
6
    use FinanCalc\Interfaces\Calculator\CalculatorAbstract;
7
    use FinanCalc\Utils\Lambdas;
8
    use FinanCalc\Utils\MathFuncs;
9
    use FinanCalc\Utils\Time\TimeSpan;
10
    use FinanCalc\Utils\Time\TimeUtils;
11
12
    /**
13
     * Class SimpleInterestCalculator
14
     * @package FinanCalc\Calculators
15
     */
16
    class SimpleInterestCalculator extends CalculatorAbstract
17
    {
18
        // amount of principal = 'P'
19
        protected $principal;
20
        // annual interest rate = 'i'
21
        protected $annualInterestRate;
22
        // the time, which converted to years = 't'
23
        /** @var  TimeSpan */
24
        protected $time;
25
26
        /**
27
         * @param $principal
28
         * @param $annualInterestRate
29
         * @param TimeSpan $time
30
         */
31
        public function __construct(
32
            $principal,
33
            $annualInterestRate,
34
            TimeSpan $time
35
        ) {
36
            $this->setPrincipal($principal);
37
            $this->setAnnualInterestRate($annualInterestRate);
38
            $this->setTime($time);
39
        }
40
41
        /**
42
         * @param $principal
43
         */
44
        public function setPrincipal($principal)
45
        {
46
            $this->setProperty("principal", $principal, Lambdas::checkIfPositive());
47
        }
48
49
        /**
50
         * @param $annualInterestRate
51
         */
52
        public function setAnnualInterestRate($annualInterestRate)
53
        {
54
            $this->setProperty("annualInterestRate", $annualInterestRate, Lambdas::checkIfPositive());
55
        }
56
57
        /**
58
         * @param TimeSpan $time
59
         */
60
        public function setTime(TimeSpan $time)
61
        {
62
            $this->setProperty("time", $time, Lambdas::checkIfPositive());
63
        }
64
65
        /**
66
         * @return string
67
         */
68
        public function getPrincipal()
69
        {
70
            return $this->principal;
71
        }
72
73
        /**
74
         * @return string
75
         */
76
        public function getAnnualInterestRate()
77
        {
78
            return $this->annualInterestRate;
79
        }
80
81
        /**
82
         * @return TimeSpan
83
         */
84
        public function getTime()
85
        {
86
            return $this->time;
87
        }
88
89
        /**
90
         * @return string
91
         */
92
        public function getTimeInYears()
93
        {
94
            return $this->time->toYears();
95
        }
96
97
        /**
98
         * @return string
99
         */
100
        public function getTimeInMonths()
101
        {
102
            return $this->time->toMonths();
103
        }
104
105
        /**
106
         * @return string
107
         */
108
        public function getTimeInDays()
109
        {
110
            return $this->time->toDays();
111
        }
112
113
        /**
114
         * @return string
115
         */
116
        public function getInterestNumber()
117
        {
118
            return MathFuncs::div(
119
                MathFuncs::mul(
120
                    $this->principal,
121
                    $this->getTimeInDays()
122
                ),
123
                100
124
            );
125
        }
126
127
        /**
128
         * @return string
129
         * @throws Exception
130
         */
131
        public function getInterestDivisor()
132
        {
133
            return MathFuncs::div(
134
                TimeUtils::getCurrentDayCountConvention()['days_in_a_year'],
135
                MathFuncs::mul(
136
                    $this->annualInterestRate,
137
                    100
138
                )
139
            );
140
        }
141
142
        /**
143
         * @return string
144
         */
145
        public function getInterestAmount()
146
        {
147
            // n = P*i*t = IN/ID
148
            return MathFuncs::div(
149
                $this->getInterestNumber(),
150
                $this->getInterestDivisor()
151
            );
152
        }
153
    }
154
}
155