Passed
Push — develop ( efad38...adafda )
by Alec
02:51
created

TimerReport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 45
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFormatter() 0 3 1
A buildOn() 0 19 3
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 34
    public function __construct()
22
    {
23
        // This lines here keep vimeo/psalm quiet
24 34
        $this->creationTime = new \DateTimeImmutable();
25 34
        $this->elapsed = (new \DateTimeImmutable())->diff($this->creationTime);
26 34
    }
27
28
    protected static function getFormatter(): FormatterInterface
29
    {
30
        return Factory::getTimerReportFormatter();
31
    }
32
33
    /**
34
     * @param ReportableInterface $reportable
35
     * @return Contracts\ReportInterface
36
     * @throws \RuntimeException
37
     * @throws \Exception
38
     */
39 34
    public function buildOn(ReportableInterface $reportable): ReportInterface
40
    {
41 34
        if ($reportable instanceof AbstractTimer) {
42 34
            $this->name = $reportable->getName();
43 34
            $this->creationTime = $reportable->getCreation();
44 34
            $this->count = $count = $reportable->getCount();
45 34
            $this->minValue = ($count === 1) ? $reportable->getLastValue() : $reportable->getMinValue();
46 34
            $this->maxValue = $reportable->getMaxValue();
47 34
            $this->maxValueIteration = $reportable->getMaxValueIteration();
48 34
            $this->minValueIteration = $reportable->getMinValueIteration();
49 34
            $this->started = $reportable->isStarted();
50 34
            $this->stopped = $reportable->isStopped();
51 34
            $this->avgValue = $reportable->getAverageValue();
52 34
            $this->currentValue = $reportable->getLastValue();
53 34
            $this->elapsed = $reportable->getElapsed();
54
        } else {
55
            $this->wrongReportable('Instance of ' . AbstractTimer::class, $reportable);
56
        }
57 34
        return $this;
58
    }
59
}
60