Proxy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 43
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasProxy() 0 3 1
A setProxy() 0 15 2
A getProxy() 0 3 1
1
<?php
2
/**
3
 * Options of proxy
4
 * User: moyo
5
 * Date: 2018/4/2
6
 * Time: 8:32 PM
7
 */
8
9
namespace Carno\HTTP\Client\Options;
10
11
use Carno\HTTP\Exception\InvalidOptionsException;
12
13
trait Proxy
14
{
15
    /**
16
     * @var array
17
     */
18
    private $proxy = [];
19
20
    /**
21
     * @return bool
22
     */
23
    public function hasProxy() : bool
24
    {
25
        return ! empty($this->proxy);
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    public function getProxy() : array
32
    {
33
        return $this->proxy;
34
    }
35
36
    /**
37
     * http://user:password@localhost:3128
38
     * @param string $dsn
39
     * @return static
40
     */
41
    public function setProxy(string $dsn) : self
42
    {
43
        switch (($parsed = parse_url($dsn))['scheme'] ?? 'http') {
44
            case 'http':
45
                $this->proxy = [
46
                    'host' => $parsed['host'] ?? '127.0.0.1',
47
                    'port' => $parsed['port'] ?? 80,
48
                    'user' => $parsed['user'] ?? null,
49
                    'pass' => $parsed['pass'] ?? null,
50
                ];
51
                break;
52
            default:
53
                throw new InvalidOptionsException(sprintf('Unsupported of proxy type "%s"', $parsed['scheme']));
54
        }
55
        return $this;
56
    }
57
}
58