Completed
Push — master ( 65ccd9...84e9fb )
by Hiraku
7s
created

AspectProxy::before()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0084

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
ccs 17
cts 18
cp 0.9444
rs 6.7272
cc 7
eloc 16
nc 7
nop 1
crap 7.0084
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 SplObserver;
10
use SplSubject;
11
use Composer\Util\NoProxyPattern;
12
13
/**
14
 * setting for proxy server
15
 */
16
class AspectProxy implements SplObserver
17
{
18 11
    public function update(SplSubject $ev)
19
    {
20 11
        if ('pre-download' === (string)$ev) {
21 11
            $this->before($ev->refRequest());
22 11
        }
23 11
    }
24
25 11
    private static function before(HttpGetRequest $req)
26
    {
27
        // no_proxy skip
28 11
        if (isset($_SERVER['no_proxy'])) {
29 1
            $pattern = new NoProxyPattern($_SERVER['no_proxy']);
30 1
            if ($pattern->test($req->getURL())) {
31 1
                unset($req->curlOpts[CURLOPT_PROXY]);
32 1
                return;
33
            }
34
        }
35
36 10
        $httpProxy = self::issetOr($_SERVER, 'http_proxy', 'HTTP_PROXY');
37 10
        if ($httpProxy && $req->scheme === 'http') {
38 1
            $req->curlOpts[CURLOPT_PROXY] = $httpProxy;
39 1
            return;
40
        }
41
42 9
        $httpsProxy = self::issetOr($_SERVER, 'https_proxy', 'HTTPS_PROXY');
43 9
        if ($httpsProxy && $req->scheme === 'https') {
44 1
            $req->curlOpts[CURLOPT_PROXY] = $httpsProxy;
45 1
            return;
46
        }
47
48 8
        unset($req->curlOpts[CURLOPT_PROXY]);
49 8
        unset($req->curlOpts[CURLOPT_PROXYUSERPWD]);
50 8
    }
51
52 10
    private static function issetOr(array $arr, $key1, $key2)
53
    {
54 10
        if (isset($arr[$key1])) {
55 2
            return $arr[$key1];
56
        }
57 10
        if (isset($arr[$key2])) {
58 2
            return $arr[$key2];
59
        }
60 9
        return null;
61
    }
62
}
63