1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: alec |
4
|
|
|
* Date: 10.12.18 |
5
|
|
|
* Time: 14:22 |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
10
|
|
|
|
11
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
12
|
|
|
use function AlecRabbit\format_time_auto; |
13
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
14
|
|
|
|
15
|
|
|
class TimerReportFormatter extends Formatter |
16
|
|
|
{ |
17
|
|
|
/** @var TimerReport */ |
18
|
|
|
protected $report; |
19
|
|
|
|
20
|
|
|
/** {@inheritdoc} */ |
21
|
11 |
|
public function setStyles(): void |
22
|
|
|
{ |
23
|
11 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
* @throws \Throwable |
28
|
|
|
*/ |
29
|
4 |
|
public function getString(): string |
30
|
|
|
{ |
31
|
4 |
|
if (DEFAULT_NAME === $this->report->getName()) { |
32
|
3 |
|
return $this->elapsed(); |
33
|
|
|
} |
34
|
1 |
|
return $this->full(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
* @throws \Throwable |
40
|
|
|
*/ |
41
|
3 |
|
public function elapsed(): string |
42
|
|
|
{ |
43
|
|
|
return |
44
|
3 |
|
sprintf( |
45
|
3 |
|
'Elapsed: %s %s', |
46
|
3 |
|
$this->themed->comment(format_time_auto($this->report->getElapsed())), |
47
|
3 |
|
PHP_EOL |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
* @throws \Throwable |
54
|
|
|
*/ |
55
|
1 |
|
public function full(): string |
56
|
|
|
{ |
57
|
1 |
|
$name = $this->report->getName(); |
58
|
|
|
try { |
59
|
1 |
|
$str = sprintf( |
60
|
1 |
|
'Timer[%s]: Average: %s, Last: %s, Min(%s): %s, Max(%s): %s, Count: %s' . PHP_EOL, |
61
|
1 |
|
$this->themed->info($name), |
62
|
1 |
|
$this->themed->comment(format_time_auto($this->report->getAverageValue())), |
63
|
|
|
format_time_auto($this->report->getLastValue()), |
64
|
|
|
$this->report->getMinValueIteration(), |
65
|
|
|
format_time_auto($this->report->getMinValue()), |
66
|
|
|
$this->report->getMaxValueIteration(), |
67
|
|
|
format_time_auto($this->report->getMaxValue()), |
68
|
|
|
$this->report->getCount() |
69
|
|
|
); |
70
|
1 |
|
} catch (\Throwable $e) { |
71
|
|
|
$str = |
72
|
1 |
|
sprintf( |
73
|
1 |
|
'Timer[%s]: %s' . PHP_EOL, |
74
|
1 |
|
$this->themed->red($name), |
75
|
1 |
|
$this->themed->comment('Exception encountered') |
76
|
|
|
); |
77
|
|
|
} |
78
|
1 |
|
return $str; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|