Completed
Push — master ( c7a3c9...eed046 )
by Alec
16:25
created

SimpleCounterReportFormatter::computeBumped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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