Passed
Push — master ( 110e22...e67765 )
by Alec
02:51
created

SimpleCounterReportFormatter::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 3.0416
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\Contracts\CounterValuesInterface;
6
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
7
use AlecRabbit\Tools\Reports\SimpleCounterReport;
8
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
9
10
class SimpleCounterReportFormatter extends ReportFormatter
11
{
12
    /** {@inheritdoc} */
13 3
    public function process(ReportInterface $report): string
14
    {
15 3
        if ($report instanceof SimpleCounterReport) {
16 2
            if (DEFAULT_NAME === $report->getName()) {
17 2
                return $this->simple($report);
18
            }
19
            return $this->full($report);
20
        }
21 1
        $this->wrongReportType(SimpleCounterReport::class, $report);
22
        // @codeCoverageIgnoreStart
23
        return '';
24
        // @codeCoverageIgnoreEnd
25
    }
26
27
    /**
28
     * @param SimpleCounterReport $report
29
     * @param bool $eol
30
     * @return string
31
     */
32 2
    protected function simple(SimpleCounterReport $report, bool $eol = true): string
33
    {
34
        /** @var CounterValuesInterface $report */
35
        return
36 2
            sprintf(
37 2
                self::COUNTER . ': %s%s',
38 2
                (string)$report->getValue(),
39 2
                $eol ? PHP_EOL : ''
40
            );
41
    }
42
43
    /**
44
     * @param SimpleCounterReport $report
45
     * @param bool $eol
46
     * @return string
47
     */
48
    protected function full(SimpleCounterReport $report, bool $eol = true): string
49
    {
50
        return
51
            sprintf(
52
                self::COUNTER . '[%s]: ' .
53
                self::VALUE . ': %s, ' .
54
                self::STEP . ': %s, ' .
55
                self::BUMPED . ': %s, ' .
56
                $report->getName(),
57
                (string)$report->getValue(),
58
                (string)$report->getStep(),
59
                $this->computeBumped($report),
60
                $eol ? PHP_EOL : ''
61
            );
62
    }
63
64
    /**
65
     * @param SimpleCounterReport $report
66
     * @return string
67
     */
68
    private function computeBumped(SimpleCounterReport $report): string
69
    {
70
        return
71
            sprintf(
72
                self::FORWARD . '%s ',
73
                $report->getBumped()
74
            );
75
    }
76
}
77