Completed
Push — develop ( d2e190...d276d7 )
by Alec
10:43 queued 07:55
created

TimerReportFormatter::refineElapsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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