Completed
Push — master ( 0accbd...f9f913 )
by Alec
03:04
created

TimerReportFormatter::refineName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 const AlecRabbit\Traits\Constants\DEFAULT_NAME;
10
11
class TimerReportFormatter extends ReportFormatter
12
{
13
    protected const MILLISECONDS_THRESHOLD = 10000;
14
15
    /** {@inheritdoc} */
16 10
    public function process(ReportInterface $report): string
17
    {
18 10
        if ($report instanceof TimerReport) {
19 9
            if (0 === $report->getCount() && DEFAULT_NAME === $report->getName()) {
20 5
                return $this->simple($report);
21
            }
22 4
            return $this->full($report);
23
        }
24 1
        $this->wrongReportType(TimerReport::class, $report);
25
        // @codeCoverageIgnoreStart
26
        return '';
27
        // @codeCoverageIgnoreEnd
28
    }
29
30
    /**
31
     * @param TimerReport $report
32
     * @param bool $eol
33
     * @return string
34
     */
35 5
    protected function simple(TimerReport $report, bool $eol = true): string
36
    {
37
        return
38 5
            sprintf(
39 5
                self::ELAPSED . ': %s %s',
40 5
                $this->refineElapsed($report->getElapsed()),
41 5
                $eol ? PHP_EOL : ''
42
            );
43
    }
44
45
    /**
46
     * @param \DateInterval $elapsed
47
     * @return string
48
     */
49 9
    protected function refineElapsed(\DateInterval $elapsed): string
50
    {
51 9
        return static::formatElapsed($elapsed);
52
    }
53
54 16
    public static function formatElapsed(\DateInterval $elapsed): string
55
    {
56 16
        $c = CarbonInterval::instance($elapsed);
57 16
        if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) {
58
            return
59 16
                Pretty::milliseconds($c->totalMilliseconds);
60
        }
61
        // @codeCoverageIgnoreStart
62
        return (string)$c;
63
        // @codeCoverageIgnoreEnd
64
    }
65
66
    /**
67
     * @param TimerReport $report
68
     * @param bool $eol
69
     * @return string
70
     */
71 4
    protected function full(TimerReport $report, bool $eol = true): string
72
    {
73 4
        $r = $report;
74 4
        $count = $r->getCount();
75
        $values =
76 4
            0 < $count ?
77 2
                sprintf(
78 2
                    self::AVERAGE . ': %s, ' .
79 2
                    self::LAST . ': %s, ' .
80 2
                    self::MIN . '(%s): %s, ' .
81 2
                    self::MAX . '(%s): %s, ' .
82 2
                    self::MARKS . ': %s, ',
83 2
                    $this->refineSeconds($r->getAverageValue()),
84 2
                    $this->refineSeconds($r->getLastValue()),
85 2
                    $r->getMinValueIteration(),
86 2
                    $this->refineSeconds($r->getMinValue()),
87 2
                    $r->getMaxValueIteration(),
88 2
                    $this->refineSeconds($r->getMaxValue()),
89
                    $count
90
                ) :
91 4
                '';
92
93
        return
94 4
            sprintf(
95 4
                self::TIMER . '%s: %s' .
96 4
                self::ELAPSED . ': %s%s',
97 4
                $this->refineName($r->getName()),
98
                $values,
99 4
                $this->refineElapsed($r->getElapsed()),
100 4
                $eol ? PHP_EOL : ''
101
            );
102
    }
103
104 2
    protected function refineSeconds(?float $seconds): string
105
    {
106
        return
107 2
            $seconds ? Pretty::seconds($seconds) : 'NULL';
108
    }
109
110 4
    protected function refineName(string $name): string
111
    {
112 4
        if (DEFAULT_NAME === $name) {
113 1
            return '';
114
        }
115 3
        return '[' . $name . ']';
116
    }
117
}
118