Test Setup Failed
Push — develop ( 7cd2b2...750858 )
by Alec
03:42
created

ExtendedCounterReportFormatter::simple()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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