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
|
|
|
|