StopWatch::getStartTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Comodojo\Extender\Utils;
2
3
use \DateTime;
4
use \DateInterval;
5
6
/**
7
* @package     Comodojo Extender
8
* @author      Marco Giovinazzi <[email protected]>
9
* @license     MIT
10
*
11
* LICENSE:
12
*
13
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
* THE SOFTWARE.
20
 */
21
22
class StopWatch {
23
24
    /**
25
     * @var DateTime
26
     */
27
    protected $start_time;
28
29
    /**
30
     * @var DateTime
31
     */
32
    protected $stop_time;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $active = false;
38
39
    /**
40
     * @return StopWatch
41
     */
42
    public function start() {
43
44
        $this->start_time = self::capture();
45
        $this->active = true;
46
47
        return $this;
48
49
    }
50
51
    /**
52
     * @return StopWatch
53
     */
54
    public function stop() {
55
56
        $this->stop_time = self::capture();
57
        $this->active = false;
58
59
        return $this;
60
61
    }
62
63
    /**
64
     * @return StopWatch
65
     */
66
    public function resume() {
67
68
        $this->stop_time = null;
69
        $this->active = true;
70
71
        return $this;
72
73
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function isActive() {
80
        return $this->active;
81
    }
82
83
    /**
84
     * @return DateTime
85
     */
86
    public function getStartTime() {
87
88
        return $this->start_time;
89
90
    }
91
92
    /**
93
     * @return DateTime
94
     */
95
    public function getStopTime() {
96
97
        return $this->stop_time;
98
99
    }
100
101
    /**
102
     * @return DateInterval
103
     */
104
    public function getDrift() {
105
106
        return is_null($this->start_time) || is_null($this->stop_time) ? null : $this->stop_time->diff($this->start_time);
107
108
    }
109
110
    /**
111
     * @return StopWatch
112
     */
113
    public function clear() {
114
115
        $this->start_time = null;
116
        $this->stop_time = null;
117
        $this->active = false;
118
119
        return $this;
120
121
    }
122
123
    /**
124
     * @return DateTime
125
     */
126
    final public static function capture() {
127
128
        $t = microtime(true);
129
130
        $micro = sprintf("%06d",($t - floor($t)) * 1000000);
131
132
        return new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );
133
134
    }
135
136
}
137