Completed
Push — develop ( b733eb...69cb61 )
by Alec
06:31
created

TimerReportFormatter::setStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\Traits\DEFAULT_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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