| Total Complexity | 9 |
| Total Lines | 78 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | final class Stopwatch |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var double|null |
||
| 13 | */ |
||
| 14 | private $startedOn; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var double|null |
||
| 18 | */ |
||
| 19 | private $stoppedOn; |
||
| 20 | |||
| 21 | 24 | public function __construct() |
|
| 22 | { |
||
| 23 | 24 | $this->startedOn = null; |
|
| 24 | 24 | $this->startedOn = null; |
|
| 25 | 24 | } |
|
| 26 | |||
| 27 | /** |
||
| 28 | * @param double|null $startedOn |
||
| 29 | * |
||
| 30 | * @return void |
||
| 31 | * |
||
| 32 | * @throws AlreadyRunningException |
||
| 33 | */ |
||
| 34 | 23 | public function start($startedOn = null) |
|
| 47 | 4 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return void |
||
| 51 | * |
||
| 52 | * @throws NotStartedException |
||
| 53 | */ |
||
| 54 | 10 | public function stop() |
|
| 55 | { |
||
| 56 | 10 | if (null === $this->startedOn) { |
|
| 57 | 1 | throw NotStartedException::create(); |
|
| 58 | } |
||
| 59 | |||
| 60 | 9 | $this->stoppedOn = $this->now(); |
|
| 61 | 9 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get current datetime in MicroSeconds |
||
| 65 | * |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | 12 | private function now() |
|
| 69 | { |
||
| 70 | 12 | return (int) (microtime(true) * 1000000); |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the Duration in MilliSeconds |
||
| 75 | * |
||
| 76 | * @return double |
||
| 77 | * |
||
| 78 | * @throws NotStoppedException |
||
| 79 | */ |
||
| 80 | 10 | public function getDuration() |
|
| 87 | } |
||
| 88 | } |
||
| 89 |