Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Counter / Timer.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Counter;
12
13 use Behat\Testwork\Counter\Exception\TimerException;
14
15 /**
16  * Provides time counting functionality.
17  *
18  * @author Konstantin Kudryashov <ever.zet@gmail.com>
19  */
20 final class Timer
21 {
22     /**
23      * @var null|float
24      */
25     private $starTime;
26     /**
27      * @var null|float
28      */
29     private $stopTime;
30
31     /**
32      * Starts timer.
33      */
34     public function start()
35     {
36         $this->starTime = microtime(true);
37     }
38
39     /**
40      * Stops timer.
41      *
42      * @throws TimerException If timer has not been started
43      */
44     public function stop()
45     {
46         if (!$this->starTime) {
47             throw new TimerException('You can not stop timer that has not been started.');
48         }
49
50         $this->stopTime = microtime(true);
51     }
52
53     /**
54      * @return null|float
55      *
56      * @throws TimerException If timer has not been started
57      */
58     public function getTime()
59     {
60         if (!$this->starTime) {
61             throw new TimerException('You can not get time from timer that never been started.');
62         }
63
64         $stopTime = $this->stopTime;
65         if (!$this->stopTime) {
66             $stopTime = microtime(true);
67         }
68
69         return $stopTime - $this->starTime;
70     }
71
72     /**
73      * Returns number of minutes passed.
74      *
75      * @return integer
76      */
77     public function getMinutes()
78     {
79         return intval(floor($this->getTime() / 60));
80     }
81
82     /**
83      * Returns number of seconds passed.
84      *
85      * @return float
86      */
87     public function getSeconds()
88     {
89         return round($this->getTime() - ($this->getMinutes() * 60), 3);
90     }
91
92     /**
93      * Returns string representation of time passed.
94      *
95      * @return string
96      */
97     public function __toString()
98     {
99         if (!$this->starTime || !$this->stopTime) {
100             return '0m0s';
101         }
102
103         return sprintf('%dm%.2fs', $this->getMinutes(), $this->getSeconds());
104     }
105 }