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

ExtendedCounterReportFormatter::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
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\ExtendedCounterReport;
9
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
10
11
class ExtendedCounterReportFormatter extends ReportFormatter
12
{
13
    /** {@inheritdoc} */
14
    public function process(Formattable $formattable): string
15
    {
16
        if ($formattable instanceof ExtendedCounterReport) {
17
            if (DEFAULT_NAME === $formattable->getName()) {
18
                return $this->simple($formattable);
19
            }
20
            return $this->full($formattable);
21
        }
22
        $this->wrongFormattableType(ExtendedCounterReport::class, $formattable);
23
        // @codeCoverageIgnoreStart
24
        return ''; // never executes
25
        // @codeCoverageIgnoreEnd
26
    }
27
28
    /**
29
     * @param ExtendedCounterReport $report
30
     * @param bool $eol
31
     * @return string
32
     */
33
    protected function simple(ExtendedCounterReport $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 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