Completed
Pull Request — master (#101)
by Hiraku
05:27
created

FetchRequest::getCurlOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Hirak\Prestissimo;
3
4
use Composer\IO;
5
use Composer\Config;
6
7
class FetchRequest extends BaseRequest
8
{
9
    protected static $defaultCurlOptions = array(
10
        CURLOPT_HTTPGET => true,
11
        CURLOPT_FOLLOWLOCATION => true,
12
        CURLOPT_MAXREDIRS => 20,
13
        CURLOPT_ENCODING => '',
14
        CURLOPT_RETURNTRANSFER => true,
15
    );
16
17
    private $headers = array();
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
    private $errno, $error, $info;
0 ignored issues
show
Coding Style introduced by
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...
19
20
    /**
21
     * @param string $url
22
     * @param IO\IOInterface $io
23
     * @param Config $config
24
     */
25
    public function __construct($url, IO\IOInterface $io, Config $config)
26
    {
27
        $this->setURL($url);
28
        $this->setCA($config->get('capath'), $config->get('cafile'));
29
        $this->setupAuthentication($io, false, $config->get('github-domains'), $config->get('gitlab-domains'));
30
    }
31
32
    public function getCurlOptions()
33
    {
34
        $curlOpts = parent::getCurlOptions();
35
        $curlOpts[CURLOPT_RETURNTRANSFER] = true;
36
        $curlOpts[CURLOPT_HEADERFUNCTION] = array($this, 'headerCallback');
37
        return $curlOpts;
38
    }
39
40
    private static function getCurl($key)
41
    {
42
        static $curlCache = array();
43
44
        if (isset($curlCache[$key])) {
45
            return $curlCache[$key];
46
        }
47
48
        $ch = curl_init();
49
        Share::setup($ch);
50
51
        return $curlCache[$key] = $ch;
52
    }
53
54
    /**
55
     * @return string|false
56
     */
57
    public function fetch()
58
    {
59
        $ch = self::getCurl($this->getOriginURL());
60
        curl_setopt_array($ch, $this->getCurlOptions());
61
62
        $result = curl_exec($ch);
63
64
        $this->errno = $errno = curl_errno($ch);
65
        $this->error = curl_error($ch);
66
        $this->info = $info = curl_getinfo($ch);
67
68
        if ($errno === CURLE_OK && $info['http_code'] === 200) {
69
            return $result;
70
        } else {
71
            return false;
72
        }
73
    }
74
75
    public function getLastError()
76
    {
77
        if ($this->errno || $this->error) {
78
            return array($this->errno, $this->error);
79
        } else {
80
            return array();
81
        }
82
    }
83
84
    public function getLastHeaders()
85
    {
86
        return $this->headers;
87
    }
88
89
    public function headerCallback($ch, $headerString)
90
    {
91
        $len = strlen($headerString);
92
        $this->headers[] = $headerString;
93
        return $len;
94
    }
95
}
96