Passed
Push — master ( 7b8b0a...7a00fa )
by Alec
02:15
created

TimerReportFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 64
ccs 23
cts 29
cp 0.7931
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getString() 0 6 2
A setStyles() 0 2 1
A elapsed() 0 7 1
A full() 0 24 2
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