Passed
Push — master ( 54698d...36eab2 )
by Alec
02:14
created

TimerReport::setFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Reports;
4
5
use AlecRabbit\Formatters\TimerReportFormatter;
6
use AlecRabbit\Formatters\TimerReportFormatterInterface;
7
use AlecRabbit\Reports\Contracts\ReportableInterface;
8
use AlecRabbit\Reports\Contracts\ReportInterface;
9
use AlecRabbit\Reports\Contracts\TimerReportInterface;
10
use AlecRabbit\Reports\Core\AbstractReport;
11
use AlecRabbit\Timers\Core\AbstractTimer;
12
use AlecRabbit\Timers\Core\Traits\TimerFields;
13
14
/**
15
 * Class TimerReport
16
 * @psalm-suppress MissingConstructor
17
 */
18
class TimerReport extends AbstractReport implements TimerReportInterface
19
{
20
    use TimerFields;
21
22
    /** @var TimerReportFormatterInterface */
23
    protected static $reportFormatter;
24
25
//    /**
26
//     * TimerReport constructor.
27
//     * @throws \Exception
28
//     */
29
//    public function __construct()
30
//    {
31
//        $this->creationTime = new \DateTimeImmutable();
32
//        $this->elapsed = (new \DateTimeImmutable())->diff($this->creationTime);
33
//    }
34
35 1
    public static function setFormatter(TimerReportFormatterInterface $formatter): void
36
    {
37 1
        static::$reportFormatter = $formatter;
38 1
    }
39
40
    /**
41
     * @param ReportableInterface $reportable
42
     * @return ReportInterface
43
     * @throws \RuntimeException
44
     * @throws \Exception
45
     */
46 41
    public function buildOn(ReportableInterface $reportable): ReportInterface
47
    {
48 41
        if ($reportable instanceof AbstractTimer) {
49 40
            $this->name = $reportable->getName();
50 40
            $this->creationTime = $reportable->getCreation();
51 40
            $this->count = $count = $reportable->getCount();
52 40
            $this->minValue = ($count === 1) ? $reportable->getLastValue() : $reportable->getMinValue();
53 40
            $this->maxValue = $reportable->getMaxValue();
54 40
            $this->maxValueIteration = $reportable->getMaxValueIteration();
55 40
            $this->minValueIteration = $reportable->getMinValueIteration();
56 40
            $this->started = $reportable->isStarted();
57 40
            $this->stopped = $reportable->isStopped();
58 40
            $this->avgValue = $reportable->getAverageValue();
59 40
            $this->currentValue = $reportable->getLastValue();
60 40
            $this->elapsed = $reportable->getElapsed();
61 40
            return $this;
62
        }
63 1
        throw new \InvalidArgumentException(
64 1
            AbstractTimer::class . ' expected, ' . get_class($reportable) . ' given.'
65
        );
66
    }
67
68
    /**
69
     * @return string
70
     */
71 5
    public function __toString(): string
72
    {
73 5
        return static::getFormatter()->format($this);
74
    }
75
76 8
    public static function getFormatter(): TimerReportFormatterInterface
77
    {
78 8
        if (!static::$reportFormatter instanceof TimerReportFormatterInterface) {
0 ignored issues
show
introduced by
static::reportFormatter is always a sub-type of AlecRabbit\Formatters\Ti...eportFormatterInterface.
Loading history...
79 1
            static::$reportFormatter = new TimerReportFormatter();
80
        }
81 8
        return static::$reportFormatter;
82
    }
83
}
84