TimeDifference::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright Andrea Heigl <[email protected]>
7
 *
8
 * Licenses under the MIT-license. For details see the included file LICENSE.md
9
 */
10
11
namespace Org_Heigl\CaptainHook\Hooks\AddTime\Formatter;
12
13
14
use DateInterval;
15
use Org_Heigl\CaptainHook\Hooks\AddTime\Fuzzier\Exact;
16
use Org_Heigl\CaptainHook\Hooks\AddTime\Fuzzier\Fuzzier;
17
18
class TimeDifference
19
{
20
    private $fuzzier;
21
22
    public function __construct(Fuzzier $fuzzier = null)
23
    {
24
        if (null === $fuzzier) {
25
            $fuzzier = new Exact();
26
        }
27
        $this->fuzzier = $fuzzier;
28
    }
29
30
    public function format(DateInterval $interval)
31
    {
32
        $interval = $this->fuzzier->fuzzy($interval);
33
34
        $string = '';
35
        if ($interval->h > 0) {
36
            $string .= $interval->h . 'h';
37
        }
38
39
        if ($interval->i > 0) {
40
            $string .= $interval->i . 'm';
41
        }
42
43
        if ($interval->s > 0) {
44
            $string .= $interval->s . 's';
45
        }
46
47
        return $string;
48
    }
49
}