Passed
Push — main ( 4f2490...bd03c4 )
by De Cramer
03:58
created

TimeIntervalExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 4 1
A formatTime() 0 18 3
1
<?php
2
3
namespace Oliverde8\PhpEtlBundle\Twig;
4
5
use Twig\Extension\AbstractExtension;
6
use Twig\TwigFilter;
7
8
class TimeIntervalExtension extends AbstractExtension
9
{
10
    public function getFilters()
11
    {
12
        return [
13
            new TwigFilter('timeSpent', [$this, 'formatTime']),
14
        ];
15
    }
16
17
    public function formatTime($time)
18
    {
19
20
        $time = abs($time);
21
        $sec = str_pad($time % 60, 2, '0', STR_PAD_LEFT);
22
        $min = str_pad(floor($time / 60), 2, '0', STR_PAD_LEFT);
23
        $hour = str_pad(floor($time / 60 / 60), 1, '0');
24
25
        $formattedPieces = [];
26
        if ($time > (60*60)) {
27
            $formattedPieces[] = $hour . "h";
28
        }
29
        if ($time > (60)) {
30
            $formattedPieces[] = $min . "m";
31
        }
32
        $formattedPieces[] = $sec . "s";
33
34
        return implode(" ", $formattedPieces);
35
    }
36
}