Completed
Push — develop ( eb616e...328010 )
by Alec
03:31
created

ProfilerReportFormatter::timersStrings()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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