Passed
Push — master ( 91accf...e7194d )
by Siad
06:49
created

DateSelector::setCheckdirs()   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 1
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
 * Selector that chooses files based on their last modified date. Ant uses
22
 * millisecond precision (thanks to Java); PHP is forced to use only seconds
23
 * precision.
24
 *
25
 * @author  Hans Lellelid <[email protected]> (Phing)
26
 * @author  Bruce Atherton <[email protected]> (Ant)
27
 * @package phing.types.selectors
28
 */
29
class DateSelector extends BaseExtendSelector
30
{
31
    private $seconds = -1; // millis in Ant, but PHP doesn't support that level of precision
32
    private $dateTime = null;
33
    private $includeDirs = false;
34
    private $granularity = 0;
35
    private $cmp = 2;
36
    public const MILLIS_KEY = "millis";
37
    public const SECONDS_KEY = "seconds";
38
    public const DATETIME_KEY = "datetime";
39
    public const CHECKDIRS_KEY = "checkdirs";
40
    public const GRANULARITY_KEY = "granularity";
41
    public const WHEN_KEY = "when";
42
    private static $timeComparisons = ["before", "after", "equal"];
43
44
    /**
45
     *
46
     */
47 38
    public function __construct()
48
    {
49 38
        parent::__construct();
50
        //if (Os.isFamily("dos")) {
51
        //    granularity = 2000;
52
        //}
53 38
    }
54
55
    /**
56
     * @return string
57
     */
58 16
    public function __toString()
59
    {
60 16
        $buf = "{dateselector date: ";
61 16
        $buf .= $this->dateTime;
62 16
        $buf .= " compare: ";
63 16
        if ($this->cmp === 0) {
64 3
            $buf .= "before";
65 13
        } elseif ($this->cmp === 1) {
66 3
            $buf .= "after";
67
        } else {
68 10
            $buf .= "equal";
69
        }
70 16
        $buf .= " granularity: ";
71 16
        $buf .= $this->granularity;
72 16
        $buf .= "}";
73
74 16
        return $buf;
75
    }
76
77
    /**
78
     * For users that prefer to express time in seconds since 1970
79
     *
80
     * @param int $seconds the time to compare file's last modified date to,
81
     *                     expressed in seconds
82
     */
83 37
    public function setSeconds($seconds)
84
    {
85 37
        $this->seconds = (int) $seconds;
86 37
    }
87
88
    /**
89
     * Returns the seconds value the selector is set for.
90
     */
91
    public function getSeconds()
92
    {
93
        return $this->seconds;
94
    }
95
96
    /**
97
     * @param int $millis the time to compare file's last modified date to, expressed in milliseconds
98
     */
99 4
    public function setMillis($millis)
100
    {
101 4
        $this->setSeconds((int) $millis / 1000);
102 4
    }
103
104
    /**
105
     * Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM
106
     * format
107
     *
108
     * @param string $dateTime a string in MM/DD/YYYY HH:MM AM_PM format
109
     */
110 27
    public function setDatetime($dateTime)
111
    {
112 27
        $dt = strtotime($dateTime);
113 27
        if (false === $dt) {
114 1
            $this->setError(
115 1
                "Date of " . $dateTime
116 1
                . " Cannot be parsed correctly. It should be in"
117 1
                . " a format parsable by PHP's strtotime() function."
118
            );
119
        } else {
120 26
            $this->dateTime = $dateTime;
121 26
            $this->setSeconds($dt);
122
        }
123 27
    }
124
125
    /**
126
     * Should we be checking dates on directories?
127
     *
128
     * @param boolean $includeDirs whether to check the timestamp on directories
129
     */
130
    public function setCheckdirs($includeDirs)
131
    {
132
        $this->includeDirs = (bool) $includeDirs;
133
    }
134
135
    /**
136
     * Sets the number of seconds leeway we will give before we consider
137
     * a file not to have matched a date.
138
     *
139
     * @param int $granularity
140
     */
141 3
    public function setGranularity($granularity)
142
    {
143 3
        $this->granularity = (int) $granularity;
144 3
    }
145
146
    /**
147
     * Sets the type of comparison to be done on the file's last modified
148
     * date.
149
     *
150
     * @param string $cmp The comparison to perform
151
     */
152 9
    public function setWhen($cmp)
153
    {
154 9
        $idx = array_search($cmp, self::$timeComparisons, true);
155 9
        if (false === $idx) {
156 1
            $this->setError("Invalid value for " . self::WHEN_KEY . ": " . $cmp);
157
        } else {
158 8
            $this->cmp = $idx;
159
        }
160 9
    }
161
162
    /**
163
     * When using this as a custom selector, this method will be called.
164
     * It translates each parameter into the appropriate setXXX() call.
165
     *
166
     * @param array $parameters the complete set of parameters for this selector
167
     * @return mixed|void
168
     */
169
    public function setParameters(array $parameters): void
170
    {
171
        parent::setParameters($parameters);
172
        if ($parameters !== null) {
0 ignored issues
show
introduced by
The condition $parameters !== null is always true.
Loading history...
173
            for ($i = 0, $size = count($parameters); $i < $size; $i++) {
174
                $paramname = $parameters[$i]->getName();
175
                switch (strtolower($paramname)) {
176
                    case self::MILLIS_KEY:
177
                        $this->setMillis($parameters[$i]->getValue());
178
                        break;
179
                    case self::SECONDS_KEY:
180
                        $this->setSeconds($parameters[$i]->getValue());
181
                        break;
182
                    case self::DATETIME_KEY:
183
                        $this->setDatetime($parameters[$i]->getValue());
184
                        break;
185
                    case self::CHECKDIRS_KEY:
186
                        $this->setCheckdirs($parameters[$i]->getValue());
187
                        break;
188
                    case self::GRANULARITY_KEY:
189
                        $this->setGranularity($parameters[$i]->getValue());
190
                        break;
191
                    case self::WHEN_KEY:
192
                        $this->setWhen($parameters[$i]->getValue());
193
                        break;
194
                    default:
195
                        $this->setError("Invalid parameter " . $paramname);
196
                } // switch
197
            }
198
        }
199
    }
200
201
    /**
202
     * This is a consistency check to ensure the selector's required
203
     * values have been set.
204
     */
205 16
    public function verifySettings()
206
    {
207 16
        if ($this->dateTime === null && $this->seconds < 0) {
208 1
            $this->setError(
209 1
                "You must provide a datetime or the number of seconds."
210
            );
211 15
        } elseif ($this->seconds < 0) {
212 1
            $this->setError(
213 1
                "Date of " . $this->dateTime
214 1
                . " results in negative seconds"
215 1
                . " value relative to epoch (January 1, 1970, 00:00:00 GMT)."
216
            );
217
        }
218 16
    }
219
220
    /**
221
     * The heart of the matter. This is where the selector gets to decide
222
     * on the inclusion of a file in a particular fileset.
223
     *
224
     * @param  PhingFile $basedir the base directory the scan is being done from
225
     * @param  string $filename is the name of the file to check
226
     * @param  PhingFile $file is a PhingFile object the selector can use
227
     * @return boolean   Whether the file should be selected or not
228
     */
229 18
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
230
    {
231 18
        $this->validate();
232 14
        if ($file->isDirectory() && ($this->includeDirs === false)) {
233 12
            return true;
234
        }
235 14
        if ($this->cmp === 0) {
236 4
            return (($file->lastModified() - $this->granularity) < $this->seconds);
237
        }
238
239 10
        if ($this->cmp === 1) {
240 4
            return (($file->lastModified() - $this->granularity) > $this->seconds);
241
        }
242
243 6
        return (abs($file->lastModified() - $this->seconds) <= $this->granularity);
244
    }
245
}
246