Completed
Push — develop ( d2e190...d276d7 )
by Alec
10:43 queued 07:55
created

ProfilerReportFormatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 52
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 2
A timersStrings() 0 11 4
A countersStrings() 0 11 5
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
    public function process(ReportInterface $report): string
19
    {
20
        if ($report instanceof ProfilerReport) {
21
            return
22
                sprintf(
23
                    '%s %s %s',
24
                    $this->countersStrings($report),
25
                    $this->timersStrings($report),
26
                    $this->elapsed
27
                );
28
        }
29
        return '';
30
    }
31
32
    /**
33
     * @param ProfilerReport $report
34
     * @return string
35
     */
36
    protected function countersStrings(ProfilerReport $report): string
37
    {
38
        $r = '';
39
        foreach ($report->getCountersReports() as $countersReport) {
40
            if ($countersReport instanceof SimpleCounterReport && DEFAULT_NAME === $countersReport->getName()) {
41
                $r .= $countersReport->isStarted() ? $countersReport : '';
42
            } else {
43
                $r .= $countersReport;
44
            }
45
        }
46
        return $r;
47
    }
48
49
    /**
50
     * @param ProfilerReport $report
51
     * @return string
52
     */
53
    protected function timersStrings(ProfilerReport $report): string
54
    {
55
        $r = '';
56
        foreach ($report->getTimersReports() as $timerReport) {
57
            if ($timerReport instanceof TimerReport && DEFAULT_NAME === $timerReport->getName()) {
58
                $this->elapsed = (string)$timerReport;
59
            } else {
60
                $r .= $timerReport;
61
            }
62
        }
63
        return $r;
64
    }
65
}
66