Timer::getExecutionTimeInSeconds()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 3
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File: Timer.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\ReactPhpPlayground\Model;
12
13
use MSlwk\ReactPhpPlayground\Api\TimerInterface;
14
use MSlwk\ReactPhpPlayground\Exception\TimerException;
15
16
/**
17
 * Class Timer
18
 * @package MSlwk\ReactPhpPlayground\Model
19
 */
20
class Timer implements TimerInterface
21
{
22
    /**
23
     * @var float
24
     */
25
    private $timeStart = 0.0;
26
27
    /**
28
     * @var float
29
     */
30
    private $timeStop = 0.0;
31
32
    /**
33
     * @return void
34
     */
35
    public function startTimer(): void
36
    {
37
        $this->timeStart = microtime(true);
38
    }
39
40
    /**
41
     * @return void
42
     * @throws TimerException
43
     */
44
    public function stopTimer(): void
45
    {
46
        if (!$this->timeStart) {
47
            throw new TimerException('Timer not started');
48
        }
49
        $this->timeStop = microtime(true);
50
    }
51
52
    /**
53
     * @return float
54
     * @throws TimerException
55
     */
56
    public function getExecutionTimeInSeconds(): float
57
    {
58
        if (!$this->timeStart || !$this->timeStop) {
59
            throw new TimerException('Execution time cannot be calculated');
60
        }
61
        $executionTime = $this->timeStop - $this->timeStart;
62
        $this->timeStart = 0.0;
63
        $this->timeStop = 0.0;
64
        return $executionTime;
65
    }
66
}
67