Passed
Push — master ( a2b1bc...228ac7 )
by Alec
03:07
created

SimpleCounterReportFormatter::format()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
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\Formatters\Core\AbstractFormatter;
7
use AlecRabbit\Reports\Core\Formattable;
8
use AlecRabbit\Reports\SimpleCounterReport;
9
use const AlecRabbit\Traits\Constants\DEFAULT_NAME;
10
11
class SimpleCounterReportFormatter extends AbstractFormatter
12
{
13 5
    public function format(Formattable $formattable): string
14
    {
15 5
        if ($formattable instanceof SimpleCounterReport) {
16 5
            $data = $formattable->getData();
17 5
            if (DEFAULT_NAME === $data['name']) {
18 4
                return $this->simple($data);
19
            }
20 1
            return $this->full($data);
21
        }
22
        return
23
            $this->errorMessage($formattable, SimpleCounterReport::class);
24
    }
25
26
    /**
27
     * @param array $data
28
     * @return string
29
     */
30 4
    protected function simple(array $data): string
31
    {
32
        return
33 4
            sprintf(
34 4
                CounterStrings::COUNTER . ': %s',
35 4
                (string)$data['value']
36
            );
37
    }
38
39
    /**
40
     * @param array $data
41
     * @return string
42
     */
43 1
    protected function full(array $data): string
44
    {
45
        return
46 1
            sprintf(
47 1
                CounterStrings::COUNTER . '[%s]: ' .
48 1
                CounterStrings::VALUE . ': %s, ' .
49 1
                CounterStrings::STEP . ': %s, ' .
50 1
                CounterStrings::BUMPED . ': %s',
51 1
                $data['name'],
52 1
                (string)$data['value'],
53 1
                (string)$data['step'],
54 1
                $this->computeBumped($data)
55
            );
56
    }
57
58
    /**
59
     * @param array $data
60
     * @return string
61
     */
62 1
    protected function computeBumped(array $data): string
63
    {
64
        return
65 1
            sprintf(
66 1
                CounterStrings::FORWARD . '%s ',
67 1
                $data['bumped']
68
            );
69
    }
70
}
71