Completed
Pull Request — master (#31)
by Hiraku
02:06
created

AspectProxy   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 58.06%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
c 2
b 0
f 1
lcom 0
cbo 2
dl 0
loc 49
ccs 18
cts 31
cp 0.5806
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A issetOr() 0 10 3
A update() 0 8 2
C before() 0 26 7
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 2
    public function update(SplSubject $ev)
19
    {
20 2
        switch ((string)$ev) {
21 2
            case 'pre-download':
22 2
                $this->before($ev->refRequest());
23 2
                return;
24
        }
25
    }
26
27 2
    public static function before(HttpGetRequest $req)
28
    {
29
        // no_proxy skip
30 2
        if (isset($_SERVER['no_proxy'])) {
31
            $pattern = new NoProxyPattern($_SERVER['no_proxy']);
32
            if ($pattern->test($req->getURL())) {
33
                $req->curlOpts[CURLOPT_PROXY] = null;
34
                return;
35
            }
36
        }
37
38 2
        $httpProxy = self::issetOr($_SERVER, 'http_proxy', 'HTTP_PROXY');
39 2
        if ($httpProxy && $req->scheme === 'http') {
40
            $req->curlOpts[CURLOPT_PROXY] = $httpProxy;
41
            return;
42
        }
43
44 2
        $httpsProxy = self::issetOr($_SERVER, 'https_proxy', 'HTTPS_PROXY');
45 2
        if ($httpsProxy && $req->scheme === 'https') {
46
            $req->curlOpts[CURLOPT_PROXY] = $httpsProxy;
47
            return;
48
        }
49
50 2
        $req->curlOpts[CURLOPT_PROXY] = null;
51 2
        $req->curlOpts[CURLOPT_PROXYUSERPWD] = null;
52 2
    }
53
54 2
    private static function issetOr(array $arr, $key1, $key2)
55
    {
56 2
        if (isset($arr[$key1])) {
57
            return $arr[$key1];
58
        }
59 2
        if (isset($arr[$key2])) {
60
            return $arr[$key2];
61
        }
62 2
        return null;
63
    }
64
}
65