Test Failed
Push — master ( 2fcf63...a2b1bc )
by Alec
02:17
created

AbstractCounterReport::setFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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\Core\Traits\SimpleCounterFields;
7
use AlecRabbit\Counters\SimpleCounter;
8
use AlecRabbit\Formatters\Contracts\CounterReportFormatterInterface;
9
use AlecRabbit\Reports\Contracts\ReportableInterface;
10
use AlecRabbit\Reports\Contracts\ReportInterface;
11
12
abstract class AbstractCounterReport extends AbstractReport
13
{
14
    use SimpleCounterFields;
15
16
    /** @var CounterReportFormatterInterface */
17
    protected static $reportFormatter;
18
19
    public static function setFormatter(CounterReportFormatterInterface $formatter): void
20
    {
21
        static::$reportFormatter = $formatter;
22
    }
23
24
    /**
25 2
     * @param ReportableInterface $counter
26
     * @return ReportInterface
27 2
     * @throws \RuntimeException
28 1
     * @throws \Exception
29
     */
30 2
    public function buildOn(ReportableInterface $counter): ReportInterface
31
    {
32
        if ($counter instanceof SimpleCounter) {
33
            $this->name = $counter->getName();
34
            $this->value = $counter->getValue();
35
            $this->step = $counter->getStep();
36
            $this->started = $counter->isStarted();
37
            $this->initialValue = $counter->getInitialValue();
38
            $this->bumped = $counter->getBumped();
39 15
            return $this;
40
        }
41 15
        throw new \InvalidArgumentException(
42 14
            AbstractCounter::class . ' expected, ' . get_class($counter) . ' given.'
43 14
        );
44 14
    }
45 14
46 14
    /**
47 14
     * @return string
48 14
     */
49
    public function __toString(): string
50 1
    {
51 1
        return static::getFormatter()->format($this);
52
    }
53
54
    public static function getFormatter(): CounterReportFormatterInterface
55
    {
56
        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...
57
            static::$reportFormatter = self::createFormatter();
58
        }
59
        return static::$reportFormatter;
60
    }
61
62
    abstract protected static function createFormatter(): CounterReportFormatterInterface;
63
}
64