Passed
Push — master ( 602023...30e4d8 )
by Alec
11:05
created

AbstractCounterReport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 39
ccs 11
cts 18
cp 0.6111
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormatter() 0 3 1
A getFormatter() 0 6 2
A buildOn() 0 13 2
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Reports\Core;
4
5
use AlecRabbit\Counters\Core\AbstractCounter;
6
use AlecRabbit\Counters\SimpleCounter;
7
use AlecRabbit\Counters\Core\Traits\SimpleCounterFields;
8
use AlecRabbit\Formatters\Contracts\CounterReportFormatterInterface;
9
use AlecRabbit\Formatters\SimpleCounterReportFormatter;
10
use AlecRabbit\Reports\Contracts\ReportableInterface;
11
use AlecRabbit\Reports\Contracts\ReportInterface;
12
13
abstract class AbstractCounterReport extends AbstractReport
14
{
15
    use SimpleCounterFields;
16
17
    /** @var CounterReportFormatterInterface */
18
    protected static $reportFormatter;
19
20
    public static function setFormatter(CounterReportFormatterInterface $formatter): void
21
    {
22
        static::$reportFormatter = $formatter;
23
    }
24
25
    protected static function getFormatter(): CounterReportFormatterInterface
26
    {
27
        if (!static::$reportFormatter instanceof CounterReportFormatterInterface) {
0 ignored issues
show
introduced by
static::reportFormatter is always a sub-type of AlecRabbit\Formatters\Co...eportFormatterInterface.
Loading history...
28
            static::$reportFormatter = new SimpleCounterReportFormatter();
29
        }
30
        return static::$reportFormatter;
31
    }
32
33
    /**
34
     * @param ReportableInterface $counter
35
     * @return ReportInterface
36
     * @throws \RuntimeException
37
     * @throws \Exception
38
     */
39 12
    public function buildOn(ReportableInterface $counter): ReportInterface
40
    {
41 12
        if ($counter instanceof SimpleCounter) {
42 11
            $this->name = $counter->getName();
43 11
            $this->value = $counter->getValue();
44 11
            $this->step = $counter->getStep();
45 11
            $this->started = $counter->isStarted();
46 11
            $this->initialValue = $counter->getInitialValue();
47 11
            $this->bumped = $counter->getBumped();
48 11
            return $this;
49
        }
50 1
        throw new \InvalidArgumentException(
51 1
            AbstractCounter::class . ' expected, ' . get_class($counter) . ' given.'
52
        );
53
    }
54
}
55