Passed
Push — develop ( 8e5858...26158a )
by Alec
02:57
created

ExtendedCounterReportFormatter::simple()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
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 2
                return $this->simple($report);
19
            }
20 1
            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 2
    protected function simple(ExtendedCounterReport $report, bool $eol = true): string
34
    {
35
        /** @var CounterValuesInterface $report */
36
        return
37 2
            sprintf(
38 2
                self::COUNTER . ': %s%s',
39 2
                (string)$report->getValue(),
40 2
                $eol ? PHP_EOL : ''
41
            );
42
    }
43
44
    /**
45
     * @param ExtendedCounterReport $report
46
     * @param bool $eol
47
     * @return string
48
     */
49 1
    protected function full(ExtendedCounterReport $report, bool $eol = true): string
50
    {
51
        return
52 1
            sprintf(
53 1
                self::COUNTER . '[%s]: ' .
54 1
                self::VALUE . ': %s, ' .
55 1
                self::STEP . ': %s, ' .
56 1
                self::BUMPED . ': %s, ' .
57 1
                self::PATH . ': %s, ' .
58 1
                self::LENGTH . ': %s, ' .
59 1
                self::MAX . ': %s, ' .
60 1
                self::MIN . ': %s, ' .
61 1
                self::DIFF . ': %s %s',
62 1
                $report->getName(),
63 1
                (string)$report->getValue(),
64 1
                (string)$report->getStep(),
65 1
                $this->computeBumped($report),
66 1
                (string)$report->getPath(),
67 1
                (string)$report->getLength(),
68 1
                (string)$report->getMax(),
69 1
                (string)$report->getMin(),
70 1
                (string)$report->getDiff(),
71 1
                $eol ? PHP_EOL : ''
72
            );
73
    }
74
75
    /**
76
     * @param ExtendedCounterReport $report
77
     * @return string
78
     */
79 1
    private function computeBumped(ExtendedCounterReport $report): string
80
    {
81
        return
82 1
            sprintf(
83 1
                self::FORWARD . '%s ' . self::BACKWARD . '%s',
84 1
                $report->getBumped(),
85 1
                $report->getBumpedBack()
86
            );
87
    }
88
}
89