Completed
Push — master ( f9a123...73c14a )
by Alec
18:36
created

ProfilerReportFormatter::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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