Timer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 86
wmc 10
lcom 1
cbo 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 3 1
A stop() 0 3 1
A getStartTime() 0 3 1
A getStopTime() 0 3 1
A getDuration() 0 14 3
A getCheckpoint() 0 3 1
A createCheckpoint() 0 6 1
A getCheckpoints() 0 3 1
1
<?php
2
3
namespace Weew\Timer;
4
5
class Timer implements ITimer {
6
    const START = 'start';
7
    const STOP = 'stop';
8
9
    /**
10
     * @var array
11
     */
12
    protected $checkpoints = [];
13
14
    /**
15
     * Start timer.
16
     */
17
    public function start() {
18
        $this->createCheckpoint(Timer::START);
19
    }
20
21
    /**
22
     * Stop timer.
23
     */
24
    public function stop() {
25
        $this->createCheckpoint(Timer::STOP);
26
    }
27
28
    /**
29
     * @return float|null
30
     */
31
    public function getStartTime() {
32
        return $this->getCheckpoint(Timer::START);
33
    }
34
35
    /**
36
     * @return float|null
37
     */
38
    public function getStopTime() {
39
        return $this->getCheckpoint(Timer::STOP);
40
    }
41
42
    /**
43
     * @param string $from
44
     * @param string $to
45
     *
46
     * @return float
47
     */
48
    public function getDuration($from = Timer::START, $to = Timer::STOP) {
49
        $from = $this->getCheckpoint($from);
50
        $to = $this->getCheckpoint($to);
51
52
        if ($from === null) {
53
            return 0;
54
        }
55
56
        if ($to === null) {
57
            $to = microtime(true);
58
        }
59
60
        return abs($to - $from);
61
    }
62
63
    /**
64
     * @param $name
65
     *
66
     * @return float|null
67
     */
68
    public function getCheckpoint($name) {
69
        return array_get($this->checkpoints, $name);
70
    }
71
72
    /**
73
     * @param $name
74
     *
75
     * @return float
76
     */
77
    public function createCheckpoint($name) {
78
        $checkpoint = microtime(true);
79
        array_set($this->checkpoints, $name, $checkpoint);
80
81
        return $checkpoint;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getCheckpoints() {
88
        return $this->checkpoints;
89
    }
90
}
91