Completed
Push — master ( 20b0ec...0fa80a )
by Siad
15:26
created

HttpGetTask::processResponse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0061

Importance

Changes 0
Metric Value
cc 7
eloc 21
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 33
ccs 19
cts 20
cp 0.95
crap 7.0061
rs 8.6506
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
 * A HTTP download task.
22
 *
23
 * Downloads a file via HTTP GET method and saves it to a specified directory
24
 *
25
 * @package phing.tasks.ext
26
 * @author  Ole Markus With <[email protected]>
27
 */
28
class HttpGetTask extends HttpTask
29
{
30
    /**
31
     * Holds the filename to store the output in
32
     *
33
     * @var string
34
     */
35
    protected $filename = null;
36
37
    /**
38
     * Holds the save location
39
     *
40
     * @var string
41
     */
42
    protected $dir = null;
43
44
    /**
45
     * Holds value for "ssl_verify_peer" option
46
     *
47
     * @var boolean
48
     */
49
    protected $sslVerifyPeer = true;
50
51
    /**
52
     * Holds value for "follow_redirects" option
53
     *
54
     * @var null|bool
55
     */
56
    protected $followRedirects = null;
57
58
    /**
59
     * Holds the proxy
60
     *
61
     * @var string
62
     */
63
    protected $proxy = null;
64
65
    private $quiet = false;
66
67
    /**
68
     * @return HTTP_Request2
69
     * @throws BuildException
70
     * @throws HTTP_Request2_LogicException
71
     */
72 7
    protected function createRequest()
73
    {
74 7
        if (!isset($this->dir)) {
75 1
            throw new BuildException("Required attribute 'dir' is missing", $this->getLocation());
76
        }
77
78
        $config = [
79 6
            'ssl_verify_peer' => $this->sslVerifyPeer
80
        ];
81 6
        if (isset($this->proxy)) {
82 1
            $config['proxy'] = $this->proxy;
83
        }
84 6
        if (null !== $this->followRedirects) {
85 1
            $config['follow_redirects'] = $this->followRedirects;
86
        }
87
88 6
        $request = parent::createRequest();
89 6
        $request->setConfig($config);
90
91 6
        $this->log("Fetching " . $this->url);
92
93 6
        return $request;
94
    }
95
96
    /**
97
     * Saves the response body to a specified directory
98
     *
99
     * @param  HTTP_Request2_Response $response
100
     * @return void
101
     * @throws BuildException
102
     */
103 6
    protected function processResponse(HTTP_Request2_Response $response)
104
    {
105 6
        if ($response->getStatus() != 200) {
106 5
            throw new BuildException(
107 5
                "Request unsuccessful. Response from server: " . $response->getStatus()
108 5
                . " " . $response->getReasonPhrase(),
109 5
                $this->getLocation()
110
            );
111
        }
112
113 1
        $content = $response->getBody();
114 1
        $disposition = $response->getHeader('content-disposition');
115
116 1
        if ($this->filename) {
117 1
            $filename = $this->filename;
118
        } elseif (
119 1
            $disposition
120 1
            && 0 == strpos($disposition, 'attachment')
0 ignored issues
show
Bug introduced by
It seems like $disposition can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, 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
            && 0 == strpos(/** @scrutinizer ignore-type */ $disposition, 'attachment')
Loading history...
121 1
            && preg_match('/filename="([^"]+)"/', $disposition, $m)
0 ignored issues
show
Bug introduced by
It seems like $disposition can also be of type array; however, parameter $subject of preg_match() does only seem to accept string, 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

121
            && preg_match('/filename="([^"]+)"/', /** @scrutinizer ignore-type */ $disposition, $m)
Loading history...
122
        ) {
123 1
            $filename = basename($m[1]);
124
        } else {
125 1
            $filename = basename(parse_url($this->url, PHP_URL_PATH));
126
        }
127
128 1
        if (!is_writable($this->dir)) {
129
            throw new BuildException("Cannot write to directory: " . $this->dir, $this->getLocation());
130
        }
131
132 1
        $filename = $this->dir . "/" . $filename;
133 1
        file_put_contents($filename, $content);
134
135 1
        $this->log("Contents from " . $this->url . " saved to $filename");
136 1
    }
137
138
    /**
139
     * Sets the filename to store the output in
140
     *
141
     * @param string $filename
142
     */
143 1
    public function setFilename($filename)
144
    {
145 1
        $this->filename = $filename;
146 1
    }
147
148
    /**
149
     * Sets the save location
150
     *
151
     * @param string $dir
152
     */
153 7
    public function setDir($dir)
154
    {
155 7
        $this->dir = $dir;
156 7
    }
157
158
    /**
159
     * Sets the ssl_verify_peer option
160
     *
161
     * @param bool $value
162
     */
163 1
    public function setSslVerifyPeer($value)
164
    {
165 1
        $this->sslVerifyPeer = $value;
166 1
    }
167
168
    /**
169
     * Sets the follow_redirects option
170
     *
171
     * @param bool $value
172
     */
173 1
    public function setFollowRedirects($value)
174
    {
175 1
        $this->followRedirects = $value;
176 1
    }
177
178
    /**
179
     * Sets the proxy
180
     *
181
     * @param string $proxy
182
     */
183 1
    public function setProxy($proxy)
184
    {
185 1
        $this->proxy = $proxy;
186 1
    }
187
188
    /**
189
     * If true, set default log level to Project.MSG_ERR.
190
     *
191
     * @param boolean $v if "true" then be quiet
192
     */
193
    public function setQuiet($v)
194
    {
195
        $this->quiet = $v;
196
    }
197
198 6
    public function log($msg, $msgLevel = Project::MSG_INFO, Exception $t = null)
199
    {
200 6
        if (!$this->quiet || $msgLevel <= Project::MSG_ERR) {
201 6
            parent::log($msg, $msgLevel, $t);
202
        }
203 6
    }
204
}
205