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