Completed
Push — master ( e67765...0accbd )
by Alec
02:55
created

ProfilerReportFormatter::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace AlecRabbit\Tools\Reports\Formatters;
5
6
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
7
use AlecRabbit\Tools\Reports\SimpleCounterReport;
8
use AlecRabbit\Tools\Reports\ProfilerReport;
9
use AlecRabbit\Tools\Reports\TimerReport;
10
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
11
12
class ProfilerReportFormatter extends ReportFormatter
13
{
14
    /** @var string */
15
    private $elapsed = '';
16
17
    /** {@inheritdoc} */
18 3
    public function process(ReportInterface $report): string
19
    {
20 3
        if ($report instanceof ProfilerReport) {
21
            return
22 2
                sprintf(
23 2
                    '%s %s %s',
24 2
                    $this->countersStrings($report),
25 2
                    $this->timersStrings($report),
26 2
                    $this->elapsed
27
                );
28
        }
29 1
        $this->wrongReportType(ProfilerReport::class, $report);
30
        // @codeCoverageIgnoreStart
31
        return '';
32
        // @codeCoverageIgnoreEnd
33
    }
34
35
    /**
36
     * @param ProfilerReport $report
37
     * @return string
38
     */
39 2
    protected function countersStrings(ProfilerReport $report): string
40
    {
41 2
        $r = '';
42 2
        foreach ($report->getCountersReports() as $countersReport) {
43 2
            if ($countersReport instanceof SimpleCounterReport && DEFAULT_NAME === $countersReport->getName()) {
44
                $r .= $countersReport->isStarted() ? $countersReport : '';
45
            } else {
46 2
                $r .= $countersReport;
47
            }
48
        }
49 2
        return $r;
50
    }
51
52
    /**
53
     * @param ProfilerReport $report
54
     * @return string
55
     */
56 2
    protected function timersStrings(ProfilerReport $report): string
57
    {
58 2
        $r = '';
59 2
        foreach ($report->getTimersReports() as $timerReport) {
60 2
            if ($timerReport instanceof TimerReport && DEFAULT_NAME === $timerReport->getName()) {
61
//                $this->elapsed = (string)$timerReport;
62 2
                $r .= $timerReport;
63
            } else {
64
                $r .= $timerReport;
65
            }
66
        }
67 2
        return $r;
68
    }
69
}
70