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

AbstractCounterReport::buildOn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
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