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
|
8 |
|
public function process(ReportInterface $report): string |
18
|
|
|
{ |
19
|
8 |
|
if ($report instanceof TimerReport) { |
20
|
7 |
|
if (0 === $report->getCount() && DEFAULT_NAME === $report->getName()) { |
21
|
5 |
|
return $this->simple($report); |
22
|
|
|
} |
23
|
2 |
|
return $this->full($report); |
24
|
|
|
} |
25
|
1 |
|
$this->wrongReportType(TimerReport::class, $report); |
26
|
|
|
// @codeCoverageIgnoreStart |
27
|
|
|
return ''; |
28
|
|
|
// @codeCoverageIgnoreEnd |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param TimerReport $report |
33
|
|
|
* @param bool $eol |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
5 |
|
protected function simple(TimerReport $report, bool $eol = true): string |
37
|
|
|
{ |
38
|
|
|
return |
39
|
5 |
|
sprintf( |
40
|
5 |
|
self::ELAPSED . ': %s %s', |
41
|
5 |
|
$this->refineElapsed($report->getElapsed()), |
42
|
5 |
|
$eol ? PHP_EOL : '' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \DateInterval $elapsed |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
7 |
|
protected function refineElapsed(\DateInterval $elapsed): string |
51
|
|
|
{ |
52
|
7 |
|
return static::formatElapsed($elapsed); |
53
|
|
|
} |
54
|
|
|
|
55
|
13 |
|
public static function formatElapsed(\DateInterval $elapsed): string |
56
|
|
|
{ |
57
|
13 |
|
$c = CarbonInterval::instance($elapsed); |
58
|
13 |
|
if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) { |
59
|
|
|
return |
60
|
13 |
|
Pretty::milliseconds($c->totalMilliseconds); |
61
|
|
|
} |
62
|
|
|
return (string)$c; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param TimerReport $report |
67
|
|
|
* @param bool $eol |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
2 |
|
protected function full(TimerReport $report, bool $eol = true): string |
71
|
|
|
{ |
72
|
2 |
|
$r = $report; |
73
|
2 |
|
return sprintf( |
74
|
2 |
|
self::TIMER . '%s: ' . |
75
|
2 |
|
self::AVERAGE . ': %s, ' . |
76
|
2 |
|
self::LAST . ': %s, ' . |
77
|
2 |
|
self::MIN . '(%s): %s, ' . |
78
|
2 |
|
self::MAX . '(%s): %s, ' . |
79
|
2 |
|
self::COUNT . ': %s, ' . |
80
|
2 |
|
self::ELAPSED . ': %s%s', |
81
|
2 |
|
$this->refineName($r->getName()), |
82
|
2 |
|
$this->refineSeconds($r->getAverageValue()), |
83
|
2 |
|
$this->refineSeconds($r->getLastValue()), |
84
|
2 |
|
$r->getMinValueIteration(), |
85
|
2 |
|
$this->refineSeconds($r->getMinValue()), |
86
|
2 |
|
$r->getMaxValueIteration(), |
87
|
2 |
|
$this->refineSeconds($r->getMaxValue()), |
88
|
2 |
|
$r->getCount(), |
89
|
2 |
|
$this->refineElapsed($r->getElapsed()), |
90
|
2 |
|
$eol ? PHP_EOL : '' |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
protected function refineName(string $name): string |
95
|
|
|
{ |
96
|
2 |
|
if (DEFAULT_NAME === $name) { |
97
|
|
|
return ''; |
98
|
|
|
} |
99
|
2 |
|
return '[' . $name . ']'; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
protected function refineSeconds(?float $seconds): string |
103
|
|
|
{ |
104
|
|
|
return |
105
|
2 |
|
$seconds ? Pretty::seconds($seconds) : 'NULL'; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|