Completed
Push — develop ( 04a6e9...eb616e )
by Alec
03:07
created

TimerReportFormatter::full()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 26
nc 2
nop 2
dl 0
loc 30
ccs 23
cts 23
cp 1
crap 3
rs 9.504
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 15
    public static function formatElapsed(\DateInterval $elapsed): string
55
    {
56 15
        $c = CarbonInterval::instance($elapsed);
57 15
        if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) {
58
            return
59 15
                Pretty::milliseconds($c->totalMilliseconds);
60
        }
61
        return (string)$c;
62
    }
63
64
    /**
65
     * @param TimerReport $report
66
     * @param bool $eol
67
     * @return string
68
     */
69 4
    protected function full(TimerReport $report, bool $eol = true): string
70
    {
71 4
        $r = $report;
72 4
        $count = $r->getCount();
73
        $values =
74 4
            0 < $count ?
75 2
                sprintf(
76 2
                    self::AVERAGE . ': %s, ' .
77 2
                    self::LAST . ': %s, ' .
78 2
                    self::MIN . '(%s): %s, ' .
79 2
                    self::MAX . '(%s): %s, ' .
80 2
                    self::MARKS . ': %s, ',
81 2
                    $this->refineSeconds($r->getAverageValue()),
82 2
                    $this->refineSeconds($r->getLastValue()),
83 2
                    $r->getMinValueIteration(),
84 2
                    $this->refineSeconds($r->getMinValue()),
85 2
                    $r->getMaxValueIteration(),
86 2
                    $this->refineSeconds($r->getMaxValue()),
87
                    $count
88
                ) :
89 4
                '';
90
91
        return
92 4
            sprintf(
93 4
                self::TIMER . '%s: %s' .
94 4
                self::ELAPSED . ': %s%s',
95 4
                $this->refineName($r->getName()),
96
                $values,
97 4
                $this->refineElapsed($r->getElapsed()),
98 4
                $eol ? PHP_EOL : ''
99
            );
100
    }
101
102 2
    protected function refineSeconds(?float $seconds): string
103
    {
104
        return
105 2
            $seconds ? Pretty::seconds($seconds) : 'NULL';
106
    }
107
108 4
    protected function refineName(string $name): string
109
    {
110 4
        if (DEFAULT_NAME === $name) {
111 1
            return '';
112
        }
113 3
        return '[' . $name . ']';
114
    }
115
}
116