Passed
Push — master ( a336a1...bef922 )
by Alec
02:02
created

SimpleCounterReportFormatter::computeBumped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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 4
    public function format(Formattable $formattable): string
18
    {
19 4
        if ($formattable instanceof SimpleCounterReport) {
20 3
            if (DEFAULT_NAME === $formattable->getName()) {
21 2
                return $this->simple($formattable);
22
            }
23 1
            return $this->full($formattable);
24
        }
25 1
        throw new \RuntimeException(
26 1
            '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 2
    protected function simple(SimpleCounterReport $report, bool $eol = true): string
36
    {
37
        /** @var CounterValuesInterface $report */
38
        return
39 2
            sprintf(
40 2
                self::COUNTER . ': %s%s',
41 2
                (string)$report->getValue(),
42 2
                $eol ? PHP_EOL : ''
43
            );
44
    }
45
46
    /**
47
     * @param SimpleCounterReport $report
48
     * @param bool $eol
49
     * @return string
50
     */
51 1
    protected function full(SimpleCounterReport $report, bool $eol = true): string
52
    {
53
        return
54 1
            sprintf(
55 1
                self::COUNTER . '[%s]: ' .
56 1
                self::VALUE . ': %s, ' .
57 1
                self::STEP . ': %s, ' .
58 1
                self::BUMPED . ': %s%s',
59 1
                $report->getName(),
60 1
                (string)$report->getValue(),
61 1
                (string)$report->getStep(),
62 1
                $this->computeBumped($report),
63 1
                $eol ? PHP_EOL : ''
64
            );
65
    }
66
67
    /**
68
     * @param SimpleCounterReport $report
69
     * @return string
70
     */
71 1
    private function computeBumped(SimpleCounterReport $report): string
72
    {
73
        return
74 1
            sprintf(
75 1
                self::FORWARD . '%s ',
76 1
                $report->getBumped()
77
            );
78
    }
79
}
80