Passed
Push — master ( 54698d...36eab2 )
by Alec
02:14
created

TimerReportFormatter::formatElapsed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Formatters;
4
5
use AlecRabbit\Accessories\Pretty;
6
use AlecRabbit\Formatters\Core\Formattable;
7
use AlecRabbit\Formatters\Core\ReportFormatter;
8
use AlecRabbit\Reports\TimerReport;
9
use AlecRabbit\Timers\Contracts\TimerStrings;
10
use function AlecRabbit\typeOf;
11
use Carbon\CarbonInterval;
12
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
13
14
class TimerReportFormatter extends ReportFormatter implements TimerReportFormatterInterface, TimerStrings
15
{
16
    protected const MILLISECONDS_THRESHOLD = 10000;
17
18
    /** {@inheritdoc} */
19 8
    public function format(Formattable $formattable): string
20
    {
21 8
        if ($formattable instanceof TimerReport) {
22 7
            if (0 === $formattable->getCount() && DEFAULT_NAME === $formattable->getName()) {
23 3
                return $this->simple($formattable);
24
            }
25 4
            return $this->full($formattable);
26
        }
27 1
        throw new \RuntimeException(
28 1
            'Instance of [' . TimerReport::class . '] expected, [' . typeOf($formattable) . '] given.'
29
        );
30
    }
31
32
    /**
33
     * @param TimerReport $report
34
     * @param bool $eol
35
     * @return string
36
     */
37 3
    protected function simple(TimerReport $report, bool $eol = true): string
38
    {
39
        return
40 3
            sprintf(
41 3
                self::ELAPSED . ': %s %s',
42 3
                $this->refineElapsed($report->getElapsed()),
43 3
                $eol ? PHP_EOL : ''
44
            );
45
    }
46
47
    /**
48
     * @param \DateInterval $elapsed
49
     * @return string
50
     */
51 7
    protected function refineElapsed(\DateInterval $elapsed): string
52
    {
53 7
        return static::formatElapsed($elapsed);
54
    }
55
56
    /** {@inheritdoc} */
57 8
    public static function formatElapsed(\DateInterval $elapsed): string
58
    {
59 8
        $c = CarbonInterval::instance($elapsed);
60 8
        if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) {
61
            return
62 8
                Pretty::milliseconds($c->totalMilliseconds);
63
        }
64
        // @codeCoverageIgnoreStart
65
        return (string)$c;
66
        // @codeCoverageIgnoreEnd
67
    }
68
69
    /**
70
     * @param TimerReport $report
71
     * @param bool $eol
72
     * @return string
73
     */
74 4
    protected function full(TimerReport $report, bool $eol = true): string
75
    {
76 4
        $r = $report;
77 4
        $count = $r->getCount();
78
        $values =
79 4
            0 < $count ?
80 2
                sprintf(
81 2
                    self::AVERAGE . ': %s, ' .
82 2
                    self::LAST . ': %s, ' .
83 2
                    self::MIN . '(%s): %s, ' .
84 2
                    self::MAX . '(%s): %s, ' .
85 2
                    self::MARKS . ': %s, ',
86 2
                    $this->refineSeconds($r->getAverageValue()),
87 2
                    $this->refineSeconds($r->getLastValue()),
88 2
                    $r->getMinValueIteration(),
89 2
                    $this->refineSeconds($r->getMinValue()),
90 2
                    $r->getMaxValueIteration(),
91 2
                    $this->refineSeconds($r->getMaxValue()),
92
                    $count
93
                ) :
94 4
                '';
95
96
        return
97 4
            sprintf(
98 4
                self::TIMER . '%s: %s' .
99 4
                self::ELAPSED . ': %s%s',
100 4
                $this->refineName($r->getName()),
101
                $values,
102 4
                $this->refineElapsed($r->getElapsed()),
103 4
                $eol ? PHP_EOL : ''
104
            );
105
    }
106
107 2
    protected function refineSeconds(?float $seconds): string
108
    {
109
        return
110 2
            $seconds ? Pretty::seconds($seconds) : 'NULL';
111
    }
112
113 4
    protected function refineName(string $name): string
114
    {
115 4
        if (DEFAULT_NAME === $name) {
116 1
            return '';
117
        }
118 3
        return '[' . $name . ']';
119
    }
120
}
121