Completed
Push — master ( df10ee...95deef )
by Hiraku
8s
created

src/FetchRequest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\IO;
10
use Composer\Config;
11
12
class FetchRequest extends BaseRequest
13
{
14
    protected static $defaultCurlOptions = array(
15
        CURLOPT_HTTPGET => true,
16
        CURLOPT_FOLLOWLOCATION => true,
17
        CURLOPT_MAXREDIRS => 20,
18
        CURLOPT_ENCODING => '',
19
        CURLOPT_RETURNTRANSFER => true,
20
    );
21
22
    private $headers = array();
23
    private $errno, $error, $info;
0 ignored issues
show
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
24
25
    /**
26
     * @param string $url
27
     * @param IO\IOInterface $io
28
     * @param Config $config
29
     */
30
    public function __construct($url, IO\IOInterface $io, Config $config)
31
    {
32
        $this->setURL($url);
33
        $this->setCA($config->get('capath'), $config->get('cafile'));
34
        $this->setupAuthentication($io, false, $config->get('github-domains'), $config->get('gitlab-domains'));
35
    }
36
37
    public function getCurlOptions()
38
    {
39
        $curlOpts = parent::getCurlOptions();
40
        $curlOpts[CURLOPT_RETURNTRANSFER] = true;
41
        $curlOpts[CURLOPT_HEADERFUNCTION] = array($this, 'headerCallback');
42
        return $curlOpts;
43
    }
44
45
    private static function getCurl($key)
46
    {
47
        static $curlCache = array();
48
49
        if (isset($curlCache[$key])) {
50
            return $curlCache[$key];
51
        }
52
53
        $ch = curl_init();
54
        Share::setup($ch);
55
56
        return $curlCache[$key] = $ch;
57
    }
58
59
    /**
60
     * @return string|false
61
     */
62
    public function fetch()
63
    {
64
        $ch = self::getCurl($this->getOriginURL());
65
        curl_setopt_array($ch, $this->getCurlOptions());
66
67
        $result = curl_exec($ch);
68
69
        $this->errno = $errno = curl_errno($ch);
70
        $this->error = curl_error($ch);
71
        $this->info = $info = curl_getinfo($ch);
72
73
        if ($errno === CURLE_OK && $info['http_code'] === 200) {
74
            return $result;
75
        } else {
76
            return false;
77
        }
78
    }
79
80
    public function getLastError()
81
    {
82
        if ($this->errno || $this->error) {
83
            return array($this->errno, $this->error);
84
        } else {
85
            return array();
86
        }
87
    }
88
89
    public function getLastHeaders()
90
    {
91
        return $this->headers;
92
    }
93
94
    public function headerCallback($ch, $headerString)
95
    {
96
        $len = strlen($headerString);
97
        $this->headers[] = $headerString;
98
        return $len;
99
    }
100
}
101