Test Setup Failed
Push — develop ( 7cd2b2...750858 )
by Alec
03:42
created

TimerReport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 45
rs 10
c 0
b 0
f 0
ccs 18
cts 18
cp 1
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
use function AlecRabbit\typeOf;
13
14
class TimerReport extends Report implements TimerReportInterface
15
{
16
    use TimerFields;
17
18
    protected static function getFormatter(): FormatterInterface
19
    {
20
        return Factory::getTimerReportFormatter();
21
    }
22 12
23
    /**
24 12
     * TimerReport constructor.
25 12
     * @throws \Exception
26 12
     */
27 12
    public function __construct()
28 12
    {
29 12
        // This lines here keep vimeo/psalm quiet
30 12
        $this->creationTime = new \DateTimeImmutable();
31 12
        $this->elapsed = (new \DateTimeImmutable())->diff($this->creationTime);
32 12
    }
33 12
34 12
    /**
35 12
     * @param ReportableInterface $reportable
36 12
     * @return Contracts\ReportInterface
37 12
     * @throws \RuntimeException
38 12
     * @throws \Exception
39 12
     */
40 12
    public function buildOn(ReportableInterface $reportable): ReportInterface
41
    {
42
        if ($reportable instanceof AbstractTimer) {
43
            $this->name = $reportable->getName();
44
            $this->creationTime = $reportable->getCreation();
45
            $this->count = $count = $reportable->getCount();
46
            $this->minValue = ($count === 1) ? $reportable->getLastValue() : $reportable->getMinValue();
47
            $this->maxValue = $reportable->getMaxValue();
48
            $this->maxValueIteration = $reportable->getMaxValueIteration();
49
            $this->minValueIteration = $reportable->getMinValueIteration();
50
            $this->started = $reportable->isStarted();
51
            $this->stopped = $reportable->isStopped();
52
            $this->avgValue = $reportable->getAverageValue();
53
            $this->currentValue = $reportable->getLastValue();
54
            $this->elapsed = $reportable->getElapsed();
55
            return $this;
56
        }
57
        throw new \RuntimeException(
58
            'Instance of ' . AbstractTimer::class . ' expected ' . typeOf($reportable) . ' given.'
59
        );
60
    }
61
}
62