Passed
Push — master ( 30bcbc...eba4fe )
by Siad
06:53
created

HttpCondition   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 56.41%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 114
ccs 22
cts 39
cp 0.5641
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrl() 0 3 1
A setQuiet() 0 3 1
A setRequestMethod() 0 3 2
B evaluate() 0 40 8
A setFollowRedirects() 0 3 1
A setErrorsBeginAt() 0 3 1
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
 * Condition to wait for a HTTP request to succeed.
22
 *
23
 * Attributes are:
24
 * - url - the URL of the request.
25
 * - errorsBeginAt - number at which errors begin at; default=400.
26
 *
27
 * @author  Siad Ardroumli <[email protected]>
28
 * @package phing.tasks.system.condition
29
 */
30
class HttpCondition extends ProjectComponent implements Condition
31
{
32
    const DEFAULT_REQUEST_METHOD = 'GET';
33
34
    private $errorsBeginAt = 400;
35
    private $url;
36
    private $quiet = false;
37
    private $requestMethod = self::DEFAULT_REQUEST_METHOD;
38
    private $followRedirects = true;
39
40
    /**
41
     * Set the url attribute.
42
     *
43
     * @param string $url the url of the request
44
     *
45
     * @return void
46
     */
47 1
    public function setUrl($url)
48
    {
49 1
        $this->url = $url;
50 1
    }
51
52
    /**
53
     * Set the errorsBeginAt attribute.
54
     *
55
     * @param string $errorsBeginAt number at which errors begin at, default is 400
56
     *
57
     * @return void
58
     */
59 1
    public function setErrorsBeginAt($errorsBeginAt)
60
    {
61 1
        $this->errorsBeginAt = $errorsBeginAt;
62 1
    }
63
64
    /**
65
     * Sets the method to be used when issuing the HTTP request.
66
     *
67
     * @param string $method The HTTP request method to use. Valid values are
68
     *               "GET", "HEAD", "TRACE", etc. The default
69
     *               if not specified is "GET".
70
     */
71
    public function setRequestMethod($method)
72
    {
73
        $this->requestMethod = $method === null ? self::DEFAULT_REQUEST_METHOD : strtoupper($method);
0 ignored issues
show
introduced by
The condition $method === null is always false.
Loading history...
74
    }
75
76
    /**
77
     * Whether redirects sent by the server should be followed,
78
     * defaults to true.
79
     *
80
     * @param boolean $f
81
     */
82
    public function setFollowRedirects($f)
83
    {
84
        $this->followRedirects = $f;
85
    }
86
87
    /**
88
     * Set quiet mode, which suppresses warnings if curl_exec() fails.
89
     *
90
     * @param $bool
91
     */
92
    public function setQuiet($bool)
93
    {
94
        $this->quiet = $bool;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @return true if the HTTP request succeeds
101
     *
102
     * @throws BuildException if an error occurs
103
     */
104 1
    public function evaluate()
105
    {
106 1
        if ($this->url === null) {
107
            throw new BuildException("No url specified in http condition");
108
        }
109
110 1
        if (!filter_var($this->url, FILTER_VALIDATE_URL)) {
111
            $this->log(
112
                "Possible malformed URL: " . $this->url,
113
                $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN
114
            );
115
        }
116
117 1
        $this->log("Checking for " . $this->url, Project::MSG_VERBOSE);
118
119 1
        $handle = curl_init($this->url);
120 1
        curl_setopt($handle, CURLOPT_NOBODY, true);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
        curl_setopt(/** @scrutinizer ignore-type */ $handle, CURLOPT_NOBODY, true);
Loading history...
121 1
        curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $this->requestMethod);
122 1
        curl_setopt($handle, CURLOPT_FOLLOWLOCATION, $this->followRedirects);
123
124 1
        if (!curl_exec($handle)) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
        if (!curl_exec(/** @scrutinizer ignore-type */ $handle)) {
Loading history...
125
            $this->log(
126
                "No response received from URL: " . $this->url,
127
                $this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR
128
            );
129
130
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
131
        }
132
133 1
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_getinfo() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
        $httpCode = curl_getinfo(/** @scrutinizer ignore-type */ $handle, CURLINFO_HTTP_CODE);
Loading history...
134 1
        curl_close($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
        curl_close(/** @scrutinizer ignore-type */ $handle);
Loading history...
135
136 1
        $this->log("Result code for " . $this->url . " was " . $httpCode, Project::MSG_VERBOSE);
137
138 1
        $result = false;
139 1
        if ($httpCode > 0 && $httpCode < $this->errorsBeginAt) {
140 1
            $result = true;
141
        }
142
143 1
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type boolean which is incompatible with the documented return type true.
Loading history...
144
    }
145
}
146