Completed
Push — develop ( d2e190...d276d7 )
by Alec
10:43 queued 07:55
created

TimerReport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

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