FormatService::formatSeconds()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
rs 8.439
cc 5
eloc 17
nc 7
nop 1
1
<?php
2
/**
3
 * Class description
4
 *
5
 * @package CacheCheck\Service
6
 * @author  Julian Seitz <[email protected]
7
 */
8
9
namespace HDNET\CacheCheck\Service;
10
11
/**
12
 * Class FormatService
13
 */
14
class FormatService extends AbstractService
15
{
16
17
    const MINUTE = 60;
18
19
    const HOUR = 3600;
20
21
    const DAY = 86400;
22
23
    const YEAR = 31536000;
24
25
    /**
26
     * Splits given time value(in seconds) into years, days, hours, minutes, seconds
27
     *
28
     * @param        $seconds
29
     *
30
     * @return string
31
     */
32
    public function formatSeconds($seconds)
33
    {
34
        if (!is_int($seconds)) {
35
            return 'NaN';
36
        }
37
        $return = [];
38
39
        $slots = [
40
            'year'   => self::YEAR,
41
            'day'    => self::DAY,
42
            'hour'   => self::HOUR,
43
            'minute' => self::MINUTE,
44
        ];
45
        foreach ($slots as $label => $secondSlot) {
46
            $fullInt = floor($seconds / $secondSlot);
47
            if ($fullInt > 0) {
48
                $return[] = $fullInt . ' ' . $label;
49
            }
50
            $seconds = $seconds % $secondSlot;
51
        }
52
53
        // seconds
54
        if ($seconds > 0) {
55
            $return[] = $seconds . ' seconds';
56
        }
57
58
        return implode(', ', $return);
59
    }
60
}
61