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

HttpOptions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 29
c 0
b 0
f 0
dl 0
loc 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setCurlOptions() 0 8 4
A getQueryString() 0 3 1
A __construct() 0 3 1
A setBody() 0 7 2
A setProxy() 0 3 1
A setOptions() 0 10 4
1
<?php
2
3
namespace EasyHttp\Model;
4
5
/**
6
 * Http Options
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 HttpOptions
13
{
14
15
    /**
16
     * Http Options constructor.
17
     *
18
     * @param array $options
19
     */
20
    public function __construct(array $options = [])
21
    {
22
        $this->setOptions($options);
23
    }
24
25
    /**
26
     * Set Options
27
     *
28
     * @param array $options
29
     * @return void
30
     */
31
    public function setOptions(array $options): void
32
    {
33
        foreach ($options as $key => $value) {
34
            if (method_exists($this, 'set' . ucfirst($key))) {
35
                $this->{'set' . ucfirst($key)}($value);
36
            } else {
37
                if (property_exists($this, $key)) {
38
                    $this->{$key} = $value;
39
                } else {
40
                    throw new \InvalidArgumentException("Invalid option: $key");
41
                }
42
            }
43
        }
44
    }
45
46
    /**
47
     * Set Body of Http request
48
     *
49
     * @param string|array $body The body of the request - On array it will be converted to json
50
     * @return void
51
     */
52
    public function setBody(string|array $body): void
53
    {
54
        if (is_array($body)) {
0 ignored issues
show
introduced by
The condition is_array($body) is always true.
Loading history...
55
            $this->body = json_encode($body);
56
            $this->headers['Content-Type'] = 'application/json';
57
        } else {
58
            $this->body = $body;
59
        }
60
    }
61
62
    /**
63
     * Set proxy server
64
     *
65
     * @param array $proxy ["host", "port", "user", "pass"]
66
     * @return void
67
     */
68
    public function setProxy(array $proxy): void
69
    {
70
        $this->proxy = (new ProxyServer())->setProxy($proxy);
71
    }
72
73
    /**
74
     * Get Query String
75
     *
76
     * @return string
77
     */
78
    public function getQueryString(): string
79
    {
80
        return http_build_query($this->queries);
0 ignored issues
show
Bug introduced by
It seems like $this->queries can also be of type null; however, parameter $data of http_build_query() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        return http_build_query(/** @scrutinizer ignore-type */ $this->queries);
Loading history...
81
    }
82
83
    /**
84
     * Set Curl Options
85
     *
86
     * @param array $options [{"CURLOPT_*": "value"}, ...]
87
     * @return void
88
     */
89
    public function setCurlOptions(array $options): void
90
    {
91
        if (count($options) > 0) {
92
            foreach ($options as $option => $value) {
93
                if (str_starts_with($option, 'CURLOPT_')) {
94
                    $this->curlOptions[$option] = $value;
95
                } else {
96
                    throw new \InvalidArgumentException("Invalid option: $option");
97
                }
98
            }
99
        }
100
    }
101
102
    /**
103
     * @var ?array
104
     */
105
    public ?array $headers = [];
106
107
    /**
108
     * @var ?array
109
     */
110
    public ?array $queries = [];
111
112
    /**
113
     * @var ?string
114
     */
115
    public ?string $body = null;
116
117
    /**
118
     * The proxy server to use
119
     *
120
     * @var ?ProxyServer
121
     */
122
    public ?ProxyServer $proxy = null;
123
124
    /**
125
     * Add specific opt to curl
126
     *
127
     * @var ?array
128
     */
129
    public ?array $curlOptions = [];
130
131
    /**
132
     * The timeout of the request
133
     *
134
     * @var ?int
135
     */
136
    public ?int $timeout = null;
137
138
}