Completed
Push — master ( 290b17...ca95d4 )
by Hiraku
02:29
created

HttpGetRequest::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 8.8571
cc 5
eloc 12
nc 6
nop 3
crap 5
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo\Aspects;
8
9
use Composer\IO;
10
use Composer\Composer;
11
12
/**
13
 * Simple Container for http-get request
14
 */
15
class HttpGetRequest
16
{
17
    public $origin;
18
    public $scheme = 'http';
19
    public $host = 'example.com';
20
    public $port = 80;
21
    public $path = '/';
22
23
    public $special = null;
24
25
    public $query = array();
26
    public $headers = array();
27
28
    public $curlOpts = array();
29
30
    public $username = null;
31
    public $password = null;
32
33
    public $maybePublic = true;
34
    public $verbose = false;
35
36
    /**
37
     * normalize url and authentication info
38
     * @param string $origin domain text
39
     * @param string $url
40
     * @param IO/IOInterface $io
0 ignored issues
show
Documentation introduced by
The doc-type IO/IOInterface could not be parsed: Unknown type name "IO/IOInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
41
     */
42 6
    public function __construct($origin, $url, IO\IOInterface $io)
43
    {
44
        // normalize github origin
45 6
        if (substr($origin, -10) === 'github.com') {
46 2
            $origin = 'github.com';
47 2
            $this->special = 'github';
48 2
        }
49 6
        $this->origin = $origin;
50
51 6
        $this->importURL($url);
52
53 6
        if ($this->username && $this->password) {
54 1
            $io->setAuthentication($origin, $this->username, $this->password);
55 6
        } elseif ($io->hasAuthentication($origin)) {
56 1
            $auth = $io->getAuthentication($origin);
57 1
            $this->username = $auth['username'];
58 1
            $this->password = $auth['password'];
59 1
        }
60 6
    }
61
62 6
    public function importURL($url)
63
    {
64 6
        $struct = parse_url($url);
65
        // @codeCoverageIgnoreStart
66
        if (! $struct) {
67
            throw new \InvalidArgumentException("$url is not valid URL");
68
        }
69
        // @codeCoverageIgnoreEnd
70
71 6
        $this->scheme = self::setOr($struct, 'scheme', $this->scheme);
72 6
        $this->host = self::setOr($struct, 'host', $this->host);
73 6
        $this->port = self::setOr($struct, 'port', null);
74 6
        $this->path = self::setOr($struct, 'path', '');
75 6
        $this->username = self::setOr($struct, 'user', null);
76 6
        $this->password = self::setOr($struct, 'pass', null);
77
78 6
        if (! empty($struct['query'])) {
79 1
            parse_str($struct['query'], $this->query);
80 1
        }
81 6
    }
82
83
    // utility for __construct
84 6
    private static function setOr(array $struct, $key, $default=null)
85
    {
86 6
        if (!empty($struct[$key])) {
87 6
            return $struct[$key];
88
        }
89
90 6
        return $default;
91
    }
92
93 1
    public function getCurlOpts()
94
    {
95 1
        $curlOpts = $this->curlOpts + array(
96 1
            CURLOPT_HTTPGET => true,
97 1
            CURLOPT_FOLLOWLOCATION => true,
98 1
            CURLOPT_MAXREDIRS => 20,
99 1
            CURLOPT_ENCODING => 'gzip',
100 1
            CURLOPT_HTTPHEADER => $this->headers,
101 1
            CURLOPT_USERAGENT => $this->genUA(),
102 1
        );
103
104 1
        $curlOpts[CURLOPT_VERBOSE] = (bool) $this->verbose;
105
106 1
        if ($this->username && $this->password) {
107 1
            $curlOpts[CURLOPT_USERPWD] = "$this->username:$this->password";
108 1
        } else {
109 1
            unset($curlOpts[CURLOPT_USERPWD]);
110
        }
111
112 1
        $curlOpts[CURLOPT_URL] = $this->getUrl();
113
114 1
        return $curlOpts;
115
    }
116
117 2
    public function getURL()
118
    {
119 2
        if ($this->scheme) {
120 2
            $url = "$this->scheme://";
121 2
        } else {
122 1
            $url = '';
123
        }
124 2
        $url .= $this->host;
125
126 2
        if ($this->port) {
127 1
            $url .= ":$this->port";
128 1
        }
129
130 2
        $url .= $this->path;
131
132 2
        if ($this->query) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133 1
            $url .= '?' . http_build_query($this->query);
134 1
        }
135
136 2
        return $url;
137
    }
138
139
    /**
140
     * special domain special flag
141
     * @param array $map
142
     */
143 1
    public function setSpecial(array $map)
144
    {
145 1
        foreach ($map as $key => $domains) {
146 1
            if (in_array($this->origin, $domains)) {
147 1
                $this->special = $key;
148 1
                return;
149
            }
150 1
        }
151 1
    }
152
153 1
    public static function genUA()
154
    {
155 1
        static $ua;
156 1
        if ($ua) {
157 1
            return $ua;
158
        }
159 1
        $phpVersion = defined('HHVM_VERSION') ? 'HHVM ' . HHVM_VERSION : 'PHP ' . PHP_VERSION;
160
161 1
        return $ua = sprintf(
162 1
            'Composer/%s (%s; %s; %s)',
163 1
            str_replace('@package_version@', 'source', Composer::VERSION),
164 1
            php_uname('s'),
165 1
            php_uname('r'),
166
            $phpVersion
167 1
        );
168
    }
169
}
170