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

ExtendedCounterReportFormatter::computeBumped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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