TimeUtils   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 17
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 156
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getYearsFromDays() 0 7 1
A getYearsFromMonths() 0 7 1
A getYearsFromYears() 0 7 1
A getMonthsFromDays() 0 7 1
A getMonthsFromMonths() 0 7 1
A getMonthsFromYears() 0 7 1
A getDaysFromDays() 0 7 1
A getDaysFromMonths() 0 7 1
A getDaysFromYears() 0 7 1
A getCurrentDayCountConvention() 0 18 4
A isDayCountConventionValid() 0 13 4
1
<?php
2
3
4
namespace FinanCalc\Utils\Time {
5
6
    use Exception;
7
    use FinanCalc\Constants\ErrorMessages;
8
    use FinanCalc\Utils\Config;
9
    use FinanCalc\Utils\MathFuncs;
10
11
    /**
12
     * Class TimeUtils
13
     * @package FinanCalc\Utils
14
     */
15
    class TimeUtils
16
    {
17
18
        /**
19
         * @param $numberOfDays
20
         * @return string
21
         * @throws Exception
22
         */
23
        public static function getYearsFromDays($numberOfDays)
24
        {
25
            return MathFuncs::div(
26
                $numberOfDays,
27
                self::getCurrentDayCountConvention()['days_in_a_year']
28
            );
29
        }
30
31
        /**
32
         * @param $numberOfMonths
33
         * @return string
34
         */
35
        public static function getYearsFromMonths($numberOfMonths)
36
        {
37
            return MathFuncs::div(
38
                $numberOfMonths,
39
                12
40
            );
41
        }
42
43
        /**
44
         * @param $numberOfYears
45
         * @return string
46
         */
47
        public static function getYearsFromYears($numberOfYears)
48
        {
49
            return MathFuncs::div(
50
                $numberOfYears,
51
                1
52
            );
53
        }
54
55
        /**
56
         * @param $numberOfDays
57
         * @return string
58
         * @throws Exception
59
         */
60
        public static function getMonthsFromDays($numberOfDays)
61
        {
62
            return MathFuncs::div(
63
                $numberOfDays,
64
                self::getCurrentDayCountConvention()['days_in_a_month']
65
            );
66
        }
67
68
        /**
69
         * @param $numberOfMonths
70
         * @return string
71
         */
72
        public static function getMonthsFromMonths($numberOfMonths)
73
        {
74
            return MathFuncs::div(
75
                $numberOfMonths,
76
                1
77
            );
78
        }
79
80
        /**
81
         * @param $numberOfYears
82
         * @return string
83
         */
84
        public static function getMonthsFromYears($numberOfYears)
85
        {
86
            return MathFuncs::mul(
87
                $numberOfYears,
88
                12
89
            );
90
        }
91
92
        /**
93
         * @param $numberOfDays
94
         * @return string
95
         */
96
        public static function getDaysFromDays($numberOfDays)
97
        {
98
            return MathFuncs::div(
99
                $numberOfDays,
100
                1
101
            );
102
        }
103
104
        /**
105
         * @param $numberOfMonths
106
         * @return string
107
         * @throws Exception
108
         */
109
        public static function getDaysFromMonths($numberOfMonths)
110
        {
111
            return MathFuncs::mul(
112
                $numberOfMonths,
113
                self::getCurrentDayCountConvention()['days_in_a_month']
114
            );
115
        }
116
117
        /**
118
         * @param $numberOfYears
119
         * @return string
120
         * @throws Exception
121
         */
122
        public static function getDaysFromYears($numberOfYears)
123
        {
124
            return MathFuncs::mul(
125
                $numberOfYears,
126
                self::getCurrentDayCountConvention()['days_in_a_year']
127
            );
128
        }
129
130
        /**
131
         * @return mixed
132
         * @throws Exception
133
         */
134
        public static function getCurrentDayCountConvention()
135
        {
136
            $dayCountConventionIdentifier = Config::getConfigField('day_count_convention');
137
            $availableDayCountConventions = Config::getConfigField('available_day_count_conventions');
138
139
            if (is_array($availableDayCountConventions) &&
140
                array_key_exists($dayCountConventionIdentifier, $availableDayCountConventions)
141
            ) {
142
                $dayCountConvention = $availableDayCountConventions[$dayCountConventionIdentifier];
143
144
                if (self::isDayCountConventionValid($dayCountConvention)) {
145
                    $dayCountConvention['days_in_a_month'] = MathFuncs::div($dayCountConvention['days_in_a_year'], 12);
146
                    return $dayCountConvention;
147
                }
148
            }
149
150
            throw new Exception(ErrorMessages::getDayCountConventionNotDefinedMessage());
151
        }
152
153
        /**
154
         * @param $dayCountConvention
155
         * @return bool
156
         */
157
        public static function isDayCountConventionValid($dayCountConvention)
158
        {
159
            if (is_array($dayCountConvention)) {
160
                $dayCountConventionField = $dayCountConvention['days_in_a_year'];
161
                if (!is_string($dayCountConventionField) || !ctype_digit($dayCountConventionField)) {
162
                    return false;
163
                }
164
165
                return true;
166
            }
167
168
            return false;
169
        }
170
    }
171
}
172