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