Passed
Push — master ( 30e4d8...a336a1 )
by Alec
01:58
created

SimpleCounterReportFormatter::computeBumped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Formatters;
4
5
use AlecRabbit\Counters\Contracts\CounterValuesInterface;
6
use AlecRabbit\Formatters\Contracts\CounterReportFormatterInterface;
7
use AlecRabbit\Formatters\Contracts\CounterStrings;
8
use AlecRabbit\Formatters\Core\ReportFormatter;
9
use AlecRabbit\Formatters\Core\Formattable;
10
use AlecRabbit\Reports\SimpleCounterReport;
11
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
12
use function AlecRabbit\typeOf;
13
14
class SimpleCounterReportFormatter extends ReportFormatter implements CounterReportFormatterInterface, CounterStrings
15
{
16
    /** {@inheritDoc} */
17
    public function format(Formattable $formattable): string
18
    {
19
        if ($formattable instanceof SimpleCounterReport) {
20
            if (DEFAULT_NAME === $formattable->getName()) {
21
                return $this->simple($formattable);
22
            }
23
            return $this->full($formattable);
24
        }
25
        throw new \RuntimeException(
26
            'Instance of [' . SimpleCounterReport::class . '] expected, [' . typeOf($formattable) . '] given.'
27
        );
28
    }
29
30
    /**
31
     * @param SimpleCounterReport $report
32
     * @param bool $eol
33
     * @return string
34
     */
35
    protected function simple(SimpleCounterReport $report, bool $eol = true): string
36
    {
37
        /** @var CounterValuesInterface $report */
38
        return
39
            sprintf(
40
                self::COUNTER . ': %s%s',
41
                (string)$report->getValue(),
42
                $eol ? PHP_EOL : ''
43
            );
44
    }
45
46
    /**
47
     * @param SimpleCounterReport $report
48
     * @param bool $eol
49
     * @return string
50
     */
51
    protected function full(SimpleCounterReport $report, bool $eol = true): string
52
    {
53
        return
54
            sprintf(
55
                self::COUNTER . '[%s]: ' .
56
                self::VALUE . ': %s, ' .
57
                self::STEP . ': %s, ' .
58
                self::BUMPED . ': %s%s',
59
                $report->getName(),
60
                (string)$report->getValue(),
61
                (string)$report->getStep(),
62
                $this->computeBumped($report),
63
                $eol ? PHP_EOL : ''
64
            );
65
    }
66
67
    /**
68
     * @param SimpleCounterReport $report
69
     * @return string
70
     */
71
    private function computeBumped(SimpleCounterReport $report): string
72
    {
73
        return
74
            sprintf(
75
                self::FORWARD . '%s ',
76
                $report->getBumped()
77
            );
78
    }
79
80
}
81