Passed
Push — master ( e92e62...a2a1ac )
by Alec
09:33
created

SimpleCounterReportFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 57
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A simple() 0 6 1
A computeBumped() 0 6 1
A full() 0 12 1
A format() 0 11 3
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 4
            $data = $formattable->getData();
17 4
            if (DEFAULT_NAME === $data['name']) {
18 3
                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 6
    protected function simple(array $data): string
31
    {
32
        return
33 6
            sprintf(
34 6
                CounterStrings::COUNTER . ': %s',
35 6
                (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