Completed
Push — develop ( f10e50...4f888d )
by Alec
03:20
created

TimerReport::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 0
dl 0
loc 21
ccs 16
cts 16
cp 1
crap 2
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 29.11.18
5
 * Time: 21:02
6
 */
7
8
namespace AlecRabbit\Tools\Reports;
9
10
use AlecRabbit\Tools\Reports\Base\Report;
11
use AlecRabbit\Tools\Timer;
12
use AlecRabbit\Tools\Traits\TimerFields;
13
use function AlecRabbit\format_time;
14
use const AlecRabbit\Constants\Accessories\DEFAULT_NAME;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\Accessories\DEFAULT_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
16
class TimerReport extends Report
17
{
18
    use TimerFields;
19
20
    /**
21
     * TimerReport constructor.
22
     * @param Timer $timer
23
     */
24 7
    public function __construct(Timer $timer)
25
    {
26 7
        if (0 === $count = $timer->getCount()) {
27
            throw new \RuntimeException('Timer "' . $timer->getName() . '" has not been started.');
28
        }
29 7
        $this->name = $timer->getName();
30 7
        $this->previous = $timer->getPrevious();
31 7
        $this->creation = $timer->getCreation();
32 7
        $this->start = $timer->getStart();
33 7
        $this->elapsed = $timer->getElapsed();
34 7
        $this->stopped = $timer->isStopped();
35 7
        $this->currentValue = $timer->getLastValue();
36 7
        $this->minValueIteration = $timer->getMinValueIteration();
37 7
        $this->maxValueIteration = $timer->getMaxValueIteration();
38 7
        $this->avgValue = $timer->getAverageValue();
39 7
        $this->minValue = ($count === 1) ? $timer->getLastValue() : $timer->getMinValue();
40 7
        $this->maxValue = $timer->getMaxValue();
41 7
        $this->count = $count;
42 7
    }
43
44
    /**
45
     * @return string
46
     */
47 1
    public function __toString(): string
48
    {
49 1
        if (DEFAULT_NAME === $name = $this->getName()) {
50
            return
51 1
                sprintf(
52 1
                    'Timer:[%s] Elapsed: %s' . PHP_EOL,
53 1
                    $name,
54 1
                    format_time($this->getElapsed())
55
                );
56
        }
57
        return
58 1
            sprintf(
59 1
                'Timer:[%s] Average: %s, Last: %s, Min(%s): %s, Max(%s): %s, Count: %s' . PHP_EOL,
60 1
                $name,
61 1
                format_time($this->getAverageValue()),
62 1
                format_time($this->getLastValue()),
63 1
                $this->getMinValueIteration(),
64 1
                format_time($this->getMinValue()),
65 1
                $this->getMaxValueIteration(),
66 1
                format_time($this->getMaxValue()),
67 1
                $this->getCount()
68
            );
69
    }
70
}
71