GenericTimer::start()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2014 Gamer Network Ltd.
6
 *
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-profiler
10
 */
11
12
namespace yolk\profiler;
13
14
use yolk\contracts\profiler\Timer;
15
16
/**
17
 * Simple timer implementation.
18
 */
19
class GenericTimer implements Timer {
20
21
	/**
22
	 * Time last started.
23
	 * @var float
24
	 */
25
	protected $started;
26
27
	/**
28
	 * Total elapsed time
29
	 * @var float
30
	 */
31
	protected $elapsed;
32
33
	public function __construct() {
34
		$this->started = (float) 0;
35
		$this->elapsed = (float) 0;
36
	}
37
38
	/**
39
	 * Start the timer.
40
	 * @param  float $time optional starting point (microtime)
41
	 * @return self
42
	 */
43
	public function start( $time = null ) {
44
45
		if( !$this->started )
46
			$this->started = $time ?: (float) microtime(true);
47
48
		return $this;
49
50
	}
51
52
	/**
53
	 * Stop the timer and add the duration to the total elapsed time.
54
	 * @return self
55
	 */
56
	public function stop() {
57
58
		if( $this->started ) {
59
			$this->elapsed += microtime(true) - $this->started;
60
			$this->started = (float) 0;
61
		}
62
63
		return $this;
64
65
	}
66
67
	/**
68
	 * Determine if the timer is currently running.
69
	 * @return boolean
70
	 */
71
	public function isRunning() {
72
		return (bool) $this->started;
73
	}
74
75
	/**
76
	 * Return the elapsed time from last start.
77
	 * @return float
78
	 */
79
	public function getElapsed( $total = false ) {
80
		return $this->started ? (microtime(true) - $this->started) : (float) 0;
81
	}
82
83
	/**
84
	 * Return the total elapsed time.
85
	 * @return float
86
	 */
87
	public function getTotalElapsed() {
88
89
		$elapsed = $this->elapsed;
90
91
		if( $this->started )
92
			$elapsed += (microtime(true) - $this->started);
93
94
		return $elapsed;
95
96
	}
97
98
}
99
100
// EOF