Countdown::execute()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
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