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

FetchRequest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 89
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCurlOptions() 0 7 1
A getCurl() 0 13 2
A fetch() 0 17 3
A getLastError() 0 8 3
A getLastHeaders() 0 4 1
A headerCallback() 0 6 1
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