Completed
Push — master ( dd5309...9fb870 )
by Siad
20:07
created

SleepTask::getSeconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
21
/**
22
 * A phing sleep task.
23
 *
24
 * <p>A task for sleeping a short period of time, useful when a
25
 * build or deployment process requires an interval between tasks.</p>
26
 *
27
 * <p>A negative value can be supplied to any of attributes provided the total sleep time
28
 * is positive, pending fundamental changes in physics and PHP execution times.</p>
29
 *
30
 * <p>Note that sleep times are always hints to be interpreted by the OS how it feels
31
 * small times may either be ignored or rounded up to a minimum timeslice. Note
32
 * also that the system clocks often have a fairly low granularity too, which complicates
33
 * measuring how long a sleep actually took.</p>
34
 *
35
 * @author  Daniel Kutik, [email protected]
36
 * @package phing.tasks.system
37
 */
38
class SleepTask extends Task
39
{
40
    /**
41
     * failure flag
42
     *
43
     * @var bool
44
     */
45
    private $failOnError = true;
46
47
    /**
48
     * sleep seconds
49
     */
50
    private $seconds = 0;
51
52
    /**
53
     * sleep hours
54
     */
55
    private $hours = 0;
56
    /**
57
     * sleep minutes
58
     */
59
    private $minutes = 0;
60
61
    /**
62
     * sleep milliseconds
63
     */
64
    private $milliseconds = 0;
65
66
    /**
67
     * @param bool $var
68
     */
69 1
    public function setFailOnError(bool $var)
70
    {
71 1
        $this->failOnError = $var;
72 1
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function getFailOnError()
78
    {
79
        return $this->failOnError;
80
    }
81
82
    /**
83
     * @param mixed $hours
84
     */
85 1
    public function setHours($hours)
86
    {
87 1
        $this->hours = $hours;
88 1
    }
89
90
    /**
91
     * @return mixed
92
     */
93
    public function getHours()
94
    {
95
        return $this->hours;
96
    }
97
98
    /**
99
     * @param mixed $milliseconds
100
     */
101 1
    public function setMilliseconds($milliseconds)
102
    {
103 1
        $this->milliseconds = $milliseconds;
104 1
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getMilliseconds()
110
    {
111
        return $this->milliseconds;
112
    }
113
114
    /**
115
     * @param mixed $minutes
116
     */
117 3
    public function setMinutes($minutes)
118
    {
119 3
        $this->minutes = $minutes;
120 3
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function getMinutes()
126
    {
127
        return $this->minutes;
128
    }
129
130
    /**
131
     * @param mixed $seconds
132
     */
133 4
    public function setSeconds($seconds)
134
    {
135 4
        $this->seconds = $seconds;
136 4
    }
137
138
    /**
139
     * @return mixed
140
     */
141
    public function getSeconds()
142
    {
143
        return $this->seconds;
144
    }
145
146
    /**
147
     * return time to sleep
148
     *
149
     * @return int time. if below 0 then there is an error
150
     */
151 6
    private function getSleepTime()
152
    {
153 6
        return ((($this->hours * 60) + $this->minutes) * 60 + $this->seconds) * 1000 + $this->milliseconds;
154
    }
155
156
    /**
157
     * verify parameters
158
     *
159
     * @throws BuildException if something is invalid
160
     */
161 6
    private function validateAttributes()
162
    {
163 6
        if ($this->getSleepTime() < 0) {
164 2
            throw new BuildException('Negative sleep periods are not supported');
165
        }
166 4
    }
167
168 6
    public function main()
169
    {
170
        try {
171 6
            $this->validateAttributes();
172 4
            $sleepTime = $this->getSleepTime();
173 4
            usleep($sleepTime * 1000);
174 2
        } catch (Exception $e) {
175 2
            if ($this->failOnError) {
176 1
                throw new BuildException($e);
177
            }
178
        }
179 5
    }
180
}
181