Passed
Push — master ( 473b33...57ebbd )
by Shahrad
01:22
created

ProxyServer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 17
c 1
b 0
f 0
dl 0
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProxy() 0 3 3
A getAuth() 0 3 3
A setProxy() 0 8 1
A setType() 0 8 2
1
<?php
2
3
namespace EasyHttp\Model;
4
5
/**
6
 * Proxy Server
7
 *
8
 * @link    https://github.com/shahradelahi/easy-http
9
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
10
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
11
 */
12
class ProxyServer
13
{
14
15
    /**
16
     * Proxy Server IP Address - IP or Domain
17
     *
18
     * @var ?string
19
     */
20
    public ?string $ip = null;
21
22
    /**
23
     * Proxy Server Port - 1-65535
24
     *
25
     * @var ?int
26
     */
27
    public ?int $port = null;
28
29
    /**
30
     * Proxy Server Username
31
     *
32
     * @var ?string
33
     */
34
    public ?string $username = null;
35
36
    /**
37
     * Proxy Server Password
38
     *
39
     * @var ?string
40
     */
41
    public ?string $password = null;
42
43
    /**
44
     * Proxy Server Type
45
     *
46
     * @var ?int [CURLPROXY_SOCKS5|CURLPROXY_SOCKS4|CURLPROXY_HTTP]
47
     */
48
    public ?int $type = null;
49
50
    /**
51
     * Setup Proxy Server
52
     *
53
     * @param array $proxy ["host", "port", "user", "pass"]
54
     * @return ProxyServer
55
     */
56
    public function setProxy(array $proxy): self
57
    {
58
        $this->ip = $proxy['host'];
59
        $this->port = $proxy['port'];
60
        $this->username = $proxy['user'];
61
        $this->password = $proxy['pass'];
62
63
        return $this;
64
    }
65
66
    /**
67
     * Set Proxy Server Type
68
     *
69
     * @param int $type [CURLPROXY_SOCKS5|CURLPROXY_SOCKS4|CURLPROXY_HTTP]
70
     * @return ProxyServer
71
     */
72
    public function setType(int $type): self
73
    {
74
        if (!in_array($type, [CURLPROXY_SOCKS5, CURLPROXY_SOCKS4, CURLPROXY_HTTP])) {
75
            throw new \InvalidArgumentException('Invalid Proxy Type');
76
        }
77
        $this->type = $type;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Get Proxy Server
84
     *
85
     * @return ?string
86
     */
87
    public function getProxy(): ?string
88
    {
89
        return !empty($this->host) && !empty($this->port) ? "$this->host:$this->port" : null;
0 ignored issues
show
Bug Best Practice introduced by
The property host does not exist on EasyHttp\Model\ProxyServer. Did you maybe forget to declare it?
Loading history...
90
    }
91
92
    /**
93
     * Get auth data
94
     *
95
     * @return ?string
96
     */
97
    public function getAuth(): ?string
98
    {
99
        return !empty($this->username) && !empty($this->password) ? "$this->username:$this->password" : null;
100
    }
101
102
}