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