Countdown   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateDaysInHours() 0 7 2
A execute() 0 16 2
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper\Operation\DateTimeOperation;
14
15
class Countdown extends AbstractOperation implements CountdownInterface
16
{
17 7
    public function execute($dateFrom = 'now', $dateTo, string $format = '%s%02d:%02d:%02d'): string
18
    {
19 7
        $dateFrom = $this->createDateObject($dateFrom);
20 7
        $dateTo = $this->createDateObject($dateTo);
21
22 7
        $dateDifference = $dateFrom->diff($dateTo);
23
24 7
        $dateFromHigherThanDateTo = $dateFrom > $dateTo;
25
26 7
        $hours = $dateDifference->h;
27 7
        $minutes = $dateDifference->i;
28 7
        $seconds = $dateDifference->s;
29
30 7
        $hours += $this->calculateDaysInHours($dateDifference->days);
31
32 7
        return \sprintf($format, $dateFromHigherThanDateTo ? '-' : '', $hours, $minutes, $seconds);
33
    }
34
35 7
    private function calculateDaysInHours(int $days): int
36
    {
37 7
        if ($days > 0) {
38 5
            return $days * self::DAY_IN_HOURS;
39
        }
40
41 2
        return 0;
42
    }
43
}
44