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

TimerReport::getFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
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