Completed
Push — master ( f07892...67a41e )
by Ariel
12:10
created

Duration::getMinutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Timegridio\Concierge;
4
5
/**
6
 * This class is an adapted version of DateInterval, original work by Mochamad Gufron.
7
 * 
8
 * A simple class to convert time duration to a human readable string
9
 *
10
 * @class Duration
11
 *
12
 * @author Mochamad Gufron
13
 *
14
 * @link mgufron.com
15
 * @contact [email protected]
16
 */
17
class Duration
18
{
19
    /**
20
     * @var int timestamp interval
21
     */
22
    public $interval = 0;
23
24
    /**
25
     * @var int save interval when using format mode
26
     */
27
    private $tempInterval = 0;
28
29
    /**
30
     * @var string used format for forming the output
31
     */
32
    public $format = '';
33
34
    /**
35
     * @param int $interval timestamp interval
36
     * @param DateTime $interval the first date
37
     * @param DateTime $last_date the last date so we can find the interval of 
38
     * the first and the last date
39
     *
40
     * @return self object
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
41
     */
42 10
    public function __construct($interval = 0, \DateTime $last_date = null)
0 ignored issues
show
Unused Code introduced by
The parameter $last_date is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44 10
        if ($interval instanceof \DateTime) {
45 1
            $first_interval = strtotime($interval->format('Y-m-d H:i:s'));
46 1
            $last_interval = strtotime($interval->format('Y-m-d H:i:s'));
47 1
            $interval = $last_interval - $first_interval;
48 1
        }
49 10
        $this->interval = $interval;
50
51 10
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
52
    }
53
54
    /**
55
     * @param int $interval set current time interval
56
     *
57
     * @return self object
58
     */
59
    public function setInterval($interval)
60
    {
61
        $this->interval = $interval;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $format set current format
68
     *
69
     * @return self object
70
     */
71
    public function setFormat($format)
72
    {
73
        $this->format = $format;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param int $round_method rounding method after we reveal the readable 
80
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
81
     *
82
     * @return float in seconds
83
     */
84 6
    public function getSeconds($round_method = null)
85
    {
86 6
        $result = $this->interval / 1000;
87
88 6
        return $this->round($result, $round_method);
89
    }
90
91
    /**
92
     * @param int $round_method rounding method after we reveal the readable 
93
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
94
     *
95
     * @return float in minutes
96
     */
97 6
    public function getMinutes($round_method = null)
98
    {
99 6
        $result = $this->interval / (1000 * 60);
100
101 6
        return $this->round($result, $round_method);
102
    }
103
104
    /**
105
     * @param int $round_method rounding method after we reveal the readable 
106
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
107
     *
108
     * @return float in hours
109
     */
110 6
    public function getHours($round_method = null)
111
    {
112 6
        $result = $this->interval / (1000 * 60 * 60);
113
114 6
        return $this->round($result, $round_method);
115
    }
116
117
    /**
118
     * @param string $format rounding method after we reveal the readable 
119
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
120
     *
121
     * @return well formatted output
122
     */
123 4
    public function format($format = [])
124
    {
125 4
        $this->tempInterval = $this->interval;
126 4
        $hours = $this->getHours(PHP_ROUND_HALF_DOWN);
127 4
        $this->interval = $this->tempInterval % (1000 * 60 * 60);
128 4
        $minutes = $this->getMinutes(PHP_ROUND_HALF_DOWN);
129 4
        $this->interval = $this->tempInterval % (1000 * 60);
130 4
        $seconds = $this->getSeconds(PHP_ROUND_HALF_DOWN);
131 4
        $this->interval = $this->tempInterval;
132 4
        if (is_string($format)) {
133 2
            $result = strtr($format, [
134 2
            '{hours}'  => $hours,
135 2
            '{minutes}' => $minutes,
136 2
            '{seconds}' => $seconds,
137 2
        ]);
138 2
        } else {
139 2
            $template = $format['template'];
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140 2
            if ($seconds <= 0) {
141 2
                $format['{seconds}'] = '';
142 2
            }
143 2
            if ($minutes <= 0) {
144 2
                $format['{minutes}'] = '';
145 2
            }
146 2
            if ($hours <= 0) {
147 1
                $format['{hours}'] = '';
148 1
            }
149 2
            $format['{seconds}'] = strtr($format['{seconds}'], ['{seconds}' => $seconds]);
150 2
            $format['{minutes}'] = strtr($format['{minutes}'], ['{minutes}' => $minutes]);
151 2
            $format['{hours}'] = strtr($format['{hours}'], ['{hours}' => $hours]);
152 2
            $result = trim(strtr($format['template'], $format));
153
        }
154
155 4
        $result = $this->fixSingulars($result);
156
157 4
        return $result;
158
    }
159
160
    /**
161
     * @param int $round_method rounding method after we reveal the readable 
162
     * interval. You can use PHP_ROUND_HALF_UP and PHP_ROUND_HALF_DOWN
163
     *
164
     * @return float current number of the result. if it is using $round_method,
165
     * it will rounded up or down
166
     */
167 10
    private function round($result, $round_method = null)
168
    {
169 10
        if ($round_method === PHP_ROUND_HALF_UP) {
170 3
            $result = ceil($result);
171 10
        } elseif ($round_method === PHP_ROUND_HALF_DOWN) {
172 7
            $result = floor($result);
173 7
        }
174
175 10
        return $result;
176
    }
177
178
    /**
179
     * @return well formatted output
180
     */
181
    public function __toString()
182
    {
183
        return $this->format($this->format);
184
    }
185
186
    /**
187
     * Fix singulars of time units
188
     * 
189
     * @param  string $string
190
     * @return string  Formatted string with replaced plurals to singular
191
     */
192 4
    protected function fixSingulars($string)
193
    {
194 4
        $plurals = ['/^1 hours/', '/^1 minutes/', '/^1 seconds/'];
195 4
        $singulars = ['1 hour', '1 minute', '1 second'];
196
197 4
        return preg_replace($plurals, $singulars, $string);
198
    }
199
}
200