Passed
Push — master ( a2b1bc...228ac7 )
by Alec
03:07
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\Formatters;
4
5
use AlecRabbit\Formatters\Contracts\CounterStrings;
6
use AlecRabbit\Reports\Core\Formattable;
7
use AlecRabbit\Reports\ExtendedCounterReport;
8
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
9
10
class ExtendedCounterReportFormatter extends SimpleCounterReportFormatter
11
{
12 2
    public function format(Formattable $formattable): string
13
    {
14 2
        if ($formattable instanceof ExtendedCounterReport) {
15 2
            $data = $formattable->getData();
16 2
            if (DEFAULT_NAME === $data['name']) {
17 2
                return $this->simple($data);
18
            }
19
            return $this->full($data);
20
        }
21
        return
22
            $this->errorMessage($formattable, ExtendedCounterReport::class);
23
    }
24
25
    protected function full(array $data): string
26
    {
27
        return
28
            sprintf(
29
                CounterStrings::COUNTER . '[%s]: ' .
30
                CounterStrings::VALUE . ': %s, ' .
31
                CounterStrings::STEP . ': %s, ' .
32
                CounterStrings::BUMPED . ': %s, ' .
33
                CounterStrings::PATH . ': %s, ' .
34
                CounterStrings::LENGTH . ': %s, ' .
35
                CounterStrings::MAX . ': %s, ' .
36
                CounterStrings::MIN . ': %s, ' .
37
                CounterStrings::DIFF . ': %s',
38
                $data['name'],
39
                (string)$data['value'],
40
                (string)$data['step'],
41
                $this->computeBumped($data),
42
                (string)$data['path'],
43
                (string)$data['length'],
44
                (string)$data['max'],
45
                (string)$data['min'],
46
                (string)$data['diff']
47
            );
48
    }
49
50
    /**
51
     * @param array $data
52
     * @return string
53
     */
54
    protected function computeBumped(array $data): string
55
    {
56
        return
57
            sprintf(
58
                CounterStrings::FORWARD . '%s ' . CounterStrings::BACKWARD . '%s ',
59
                $data['bumped'],
60
                $data['bumpedBack']
61
            );
62
    }
63
}
64