Passed
Push — main ( 4889b2...808ce0 )
by Michiel
07:43
created

AvailableTask::checkFile1()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 12
nop 1
crap 7
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
namespace Phing\Task\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\File;
24
use Phing\Io\FileSystem;
25
use Phing\Io\IOException;
26
use Phing\Phing;
27
use Phing\Project;
28
use Phing\Task;
29
use Phing\Task\System\Condition\Condition;
30
use Phing\Type\Path;
31
32
/**
33
 * <available> task.
34
 *
35
 * Note: implements condition interface (see condition/Condition.php)
36
 *
37
 * @author    Andreas Aderhold <[email protected]>
38
 * @copyright 2001,2002 THYRELL. All rights reserved
39
 */
40
class AvailableTask extends Task implements Condition
41
{
42
    /**
43
     * Property to check for.
44
     */
45
    private $property;
46
47
    /**
48
     * Value property should be set to.
49
     */
50
    private $value = "true";
51
52
    /**
53
     * File/directory to check existence
54
     */
55
    private $file;
56
57
    /**
58
     * Resource to check for
59
     */
60
    private $resource;
61
62
    /**
63
     * Extension to check if is loaded
64
     */
65
    private $extension;
66
67
    private $type = null;
68
    private $filepath = null;
69
70
    private $followSymlinks = false;
71
72
    /**
73
     * @param $property
74
     */
75 6
    public function setProperty($property)
76
    {
77 6
        $this->property = (string) $property;
78 6
    }
79
80
    /**
81
     * @param $value
82
     */
83
    public function setValue($value)
84
    {
85
        $this->value = (string) $value;
86
    }
87
88
    /**
89
     * @param File $file
90
     */
91 10
    public function setFile(File $file)
92
    {
93 10
        $this->file = $file;
94 10
    }
95
96
    /**
97
     * @param $resource
98
     */
99
    public function setResource($resource)
100
    {
101
        $this->resource = (string) $resource;
102
    }
103
104
    /**
105
     * @param $extension
106
     */
107
    public function setExtension($extension)
108
    {
109
        $this->extension = (string) $extension;
110
    }
111
112
    /**
113
     * @param $type
114
     */
115 6
    public function setType($type)
116
    {
117 6
        $this->type = (string) strtolower($type);
118 6
    }
119
120
    /**
121
     * @param $followSymlinks
122
     */
123 5
    public function setFollowSymlinks(bool $followSymlinks)
124
    {
125 5
        $this->followSymlinks = $followSymlinks;
126 5
    }
127
128
    /**
129
     * Set the path to use when looking for a file.
130
     *
131
     * @param Path $filepath a Path instance containing the search path for files.
132
     */
133
    public function setFilepath(Path $filepath)
134
    {
135
        if ($this->filepath === null) {
136
            $this->filepath = $filepath;
137
        } else {
138
            $this->filepath->append($filepath);
139
        }
140
    }
141
142
    /**
143
     * Creates a path to be configured
144
     *
145
     * @return Path
146
     */
147
    public function createFilepath()
148
    {
149
        if ($this->filepath === null) {
150
            $this->filepath = new Path($this->project);
151
        }
152
153
        return $this->filepath->createPath();
154
    }
155
156 6
    public function main()
157
    {
158 6
        if ($this->property === null) {
159
            throw new BuildException("property attribute is required", $this->getLocation());
160
        }
161 6
        if ($this->evaluate()) {
162 4
            $this->project->setProperty($this->property, $this->value);
163
        }
164 6
    }
165
166
    /**
167
     * @return bool
168
     * @throws BuildException
169
     */
170 10
    public function evaluate()
171
    {
172 10
        if ($this->file === null && $this->resource === null && $this->extension === null) {
173
            throw new BuildException("At least one of (file|resource|extension) is required", $this->getLocation());
174
        }
175
176 10
        if ($this->type !== null && ($this->type !== "file" && $this->type !== "dir")) {
177
            throw new BuildException("Type must be one of either dir or file", $this->getLocation());
178
        }
179
180 10
        if (($this->file !== null) && !$this->checkFile()) {
181 4
            $this->log(
182 4
                "Unable to find " . $this->file->__toString() . " to set property " . $this->property,
183 4
                Project::MSG_VERBOSE
184
            );
185
186 4
            return false;
187
        }
188
189 6
        if (($this->resource !== null) && !$this->checkResource($this->resource)) {
190
            $this->log(
191
                "Unable to load resource " . $this->resource . " to set property " . $this->property,
192
                Project::MSG_VERBOSE
193
            );
194
195
            return false;
196
        }
197
198 6
        if ($this->extension !== null && !extension_loaded($this->extension)) {
199
            $this->log(
200
                "Unable to load extension " . $this->extension . " to set property " . $this->property,
201
                Project::MSG_VERBOSE
202
            );
203
204
            return false;
205
        }
206
207 6
        return true;
208
    }
209
210
    // this is prepared for the path type
211
212
    /**
213
     * @return bool
214
     */
215 10
    private function checkFile()
216
    {
217 10
        if ($this->filepath === null) {
218 10
            return $this->checkFile1($this->file);
219
        }
220
221
        $paths = $this->filepath->listPaths();
222
        foreach ($paths as $path) {
223
            $this->log("Searching " . $path, Project::MSG_VERBOSE);
224
            $tmp = new File($path, $this->file->getName());
225
            if ($tmp->isFile()) {
226
                return true;
227
            }
228
        }
229
230
        return false;
231
    }
232
233
    /**
234
     * @param File $file
235
     * @return bool
236
     * @throws IOException
237
     */
238 10
    private function checkFile1(File $file)
239
    {
240
        // Resolve symbolic links
241 10
        if ($this->followSymlinks && $file->isLink()) {
242 5
            $linkTarget = new File($file->getLinkTarget());
243 5
            if ($linkTarget->isAbsolute()) {
244 2
                $file = $linkTarget;
245
            } else {
246 3
                $fs = FileSystem::getFileSystem();
247 3
                $file = new File(
248 3
                    $fs->resolve(
249 3
                        $fs->normalize($file->getParent()),
250 3
                        $fs->normalize($file->getLinkTarget())
251
                    )
252
                );
253
            }
254
        }
255
256 10
        if ($this->type !== null) {
257 6
            if ($this->type === "dir") {
258 3
                return $file->isDirectory();
259
            }
260
261 3
            if ($this->type === "file") {
262 3
                return $file->isFile();
263
            }
264
        }
265
266 4
        return $file->exists();
267
    }
268
269
    /**
270
     * @param $resource
271
     * @return bool
272
     */
273
    private function checkResource($resource)
274
    {
275
        if (null != ($resourcePath = Phing::getResourcePath($resource))) {
276
            return $this->checkFile1(new File($resourcePath));
277
        }
278
279
        return false;
280
    }
281
}
282