SimpleCounterReportFormatter::simple()   A
last analyzed

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\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 4
    public function format(Formattable $formattable): string
14
    {
15 4
        if ($formattable instanceof SimpleCounterReport) {
16 3
            $data = $formattable->getData();
17 3
            if (DEFAULT_NAME === $data['name']) {
18 2
                return $this->simple($data);
19
            }
20 1
            return $this->full($data);
21
        }
22
        return
23 1
            $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
                CounterStrings::COUNTER . '[%s]: ' .
48
                CounterStrings::VALUE . ': %s, ' .
49
                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