Passed
Push — develop ( 0a2c3f...8e5858 )
by Alec
03:05
created

TimerReport::buildOn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 19
ccs 16
cts 16
cp 1
crap 3
rs 9.7333
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Reports;
4
5
use AlecRabbit\Tools\AbstractTimer;
6
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
7
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
8
use AlecRabbit\Tools\Reports\Contracts\TimerReportInterface;
9
use AlecRabbit\Tools\Reports\Core\Report;
10
use AlecRabbit\Tools\Reports\Formatters\Contracts\FormatterInterface;
11
use AlecRabbit\Tools\Traits\TimerFields;
12
13
class TimerReport extends Report implements TimerReportInterface
14
{
15
    use TimerFields;
16
17
    /**
18
     * TimerReport constructor.
19
     * @throws \Exception
20
     */
21 63
    public function __construct()
22
    {
23
        // This lines here keep vimeo/psalm quiet
24 63
        $this->creationTime = new \DateTimeImmutable();
25 63
        $this->elapsed = (new \DateTimeImmutable())->diff($this->creationTime);
26 63
    }
27
28 4
    protected static function getFormatter(): FormatterInterface
29
    {
30 4
        return Factory::getTimerReportFormatter();
31
    }
32
33
    /**
34
     * @param ReportableInterface $reportable
35
     * @return Contracts\ReportInterface
36
     * @throws \RuntimeException
37
     * @throws \Exception
38
     */
39 63
    public function buildOn(ReportableInterface $reportable): ReportInterface
40
    {
41 63
        if ($reportable instanceof AbstractTimer) {
42 63
            $this->name = $reportable->getName();
43 63
            $this->creationTime = $reportable->getCreation();
44 63
            $this->count = $count = $reportable->getCount();
45 63
            $this->minValue = ($count === 1) ? $reportable->getLastValue() : $reportable->getMinValue();
46 63
            $this->maxValue = $reportable->getMaxValue();
47 63
            $this->maxValueIteration = $reportable->getMaxValueIteration();
48 63
            $this->minValueIteration = $reportable->getMinValueIteration();
49 63
            $this->started = $reportable->isStarted();
50 63
            $this->stopped = $reportable->isStopped();
51 63
            $this->avgValue = $reportable->getAverageValue();
52 63
            $this->currentValue = $reportable->getLastValue();
53 63
            $this->elapsed = $reportable->getElapsed();
54
        } else {
55 1
            $this->wrongReportable(AbstractTimer::class, $reportable);
56
        }
57 63
        return $this;
58
    }
59
}
60