Passed
Push — develop ( 043862...ee5a55 )
by nguereza
03:18
created

Watch::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 * Copyright (c) 2015 PHP Reboot
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file Watch.php
35
 *
36
 *  The Stop Watch class
37
 *
38
 *  @package    Platine\Framework\Helper\Timer
39
 *  @author Platine Developers team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   https://www.platine-php.com
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Framework\Helper\Timer;
50
51
use InvalidArgumentException;
52
53
/**
54
 * @class Watch
55
 * @package Platine\Framework\Helper\Timer
56
 */
57
class Watch
58
{
59
    /**
60
     * The default name to use if none is provided
61
     */
62
    public const WATCH_DEFAULT_NAME = '__default__';
63
64
    /**
65
     * The timer list
66
     * @var array<string, Timer>
67
     */
68
    protected array $timers = [];
69
70
    /**
71
     * Start the timer
72
     * @param string $name
73
     * @return bool
74
     */
75
    public function start(string $name = self::WATCH_DEFAULT_NAME): bool
76
    {
77
        $this->addWatch($name);
78
79
        return $this->getWatch($name)->start();
80
    }
81
82
    /**
83
     * Pause the timer
84
     * @param string $name
85
     * @return bool
86
     */
87
    public function pause(string $name = self::WATCH_DEFAULT_NAME): bool
88
    {
89
        if ($this->exists($name) === false) {
90
            return false;
91
        }
92
93
        return $this->getWatch($name)->pause();
94
    }
95
96
    /**
97
     * Stop the timer
98
     * @param string $name
99
     * @return bool
100
     */
101
    public function stop(string $name = self::WATCH_DEFAULT_NAME): bool
102
    {
103
        if ($this->exists($name) === false) {
104
            return false;
105
        }
106
107
        return $this->getWatch($name)->stop();
108
    }
109
110
    /**
111
     * Get the timer time
112
     * @param string $name
113
     * @return float
114
     */
115
    public function getTime(string $name = self::WATCH_DEFAULT_NAME): float
116
    {
117
        if ($this->exists($name) === false) {
118
            return -1;
119
        }
120
121
        return $this->getWatch($name)->getTime();
122
    }
123
124
    /**
125
     * Add a new time to the stop watch.
126
     * @param string $name
127
     * @return $this
128
     */
129
    public function addWatch(string $name): self
130
    {
131
        if (array_key_exists($name, $this->timers)) {
132
            throw new InvalidArgumentException(sprintf(
133
                'Watch with the name [%s] already exist',
134
                $name
135
            ));
136
        }
137
138
        $timer = new Timer($name);
139
        $this->timers[$name] = $timer;
140
141
        return $this;
142
    }
143
144
    /**
145
    * Get watch
146
    * @param string $name
147
    * @return Timer
148
    */
149
    public function getWatch(string $name): Timer
150
    {
151
        if (array_key_exists($name, $this->timers) === false) {
152
            throw new InvalidArgumentException(sprintf(
153
                'Watch with the name [%s] does not exist',
154
                $name
155
            ));
156
        }
157
158
        return $this->timers[$name];
159
    }
160
161
    /**
162
     * Whether the timer with given name exists
163
     * @param string $name
164
     * @return bool
165
     */
166
    public function exists(string $name): bool
167
    {
168
        return array_key_exists($name, $this->timers);
169
    }
170
171
    /**
172
     * Return the total timers
173
     * @return int
174
     */
175
    public function count(): int
176
    {
177
        return count($this->timers);
178
    }
179
}
180