Passed
Push — develop ( d5e170...468d1d )
by Darío
01:13 queued 11s
created

GuzzleRequest::options()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.8666
c 0
b 0
f 0
cc 4
nc 8
nop 0
crap 4
1
<?php
2
3
namespace EasyHttp\GuzzleLayer;
4
5
use EasyHttp\LayerContracts\Contracts\HttpClientRequest;
6
7
class GuzzleRequest implements HttpClientRequest
8
{
9
    protected string $method;
10
11
    protected string $uri;
12
13
    protected array $headers = [];
14
15
    protected array $json = [];
16
17
    protected array $query = [];
18
19
    protected int $timeout = 10;
20
21
    protected bool $ssl = false;
22
23
    protected array $options;
24
25
    protected array $basicAuth = [];
26
27 15
    public function __construct(string $method, string $uri, array $options = [])
28
    {
29 15
        $this->method  = $method;
30 15
        $this->uri     = $uri;
31 15
        $this->options = $options;
32 15
    }
33
34 15
    public function getMethod(): string
35
    {
36 15
        return $this->method;
37
    }
38
39 15
    public function getUri(): string
40
    {
41 15
        return $this->uri;
42
    }
43
44 1
    public function getHeader(string $key)
45
    {
46 1
        return $this->headers[$key] ?? null;
47
    }
48
49 1
    public function getJson(): array
50
    {
51 1
        return $this->json;
52
    }
53
54 1
    public function getQuery(): array
55
    {
56 1
        return $this->query;
57
    }
58
59 1
    public function setMethod(string $method): self
60
    {
61 1
        $this->method = $method;
62
63 1
        return $this;
64
    }
65
66 1
    public function setUri(string $uri): self
67
    {
68 1
        $this->uri = $uri;
69
70 1
        return $this;
71
    }
72
73 2
    public function setHeader(string $key, string $value): self
74
    {
75 2
        $this->headers[$key] = $value;
76
77 2
        return $this;
78
    }
79
80 1
    public function setJson(array $json): self
81
    {
82 1
        $this->json = $json;
83
84 1
        return $this;
85
    }
86
87 3
    public function setQuery(array $query): self
88
    {
89 3
        $this->query = $query;
90
91 3
        return $this;
92
    }
93
94
    public function ssl(bool $ssl): void
95
    {
96
        $this->ssl = $ssl;
97
    }
98
99 2
    public function setBasicAuth(string $username, string $password): self
100
    {
101 2
        $this->basicAuth = [$username, $password];
102
103 2
        return $this;
104
    }
105
106 15
    public function options(): array
107
    {
108
        $options = [
109 15
            'timeout' => $this->timeout,
110 15
            'verify' => $this->ssl,
111 15
            'headers' => $this->headers,
112
        ];
113
114 15
        if ($this->json) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->json of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115 1
            $options['headers'] = array_merge(['Content-Type' => 'application/json;charset=UTF-8'], $this->headers);
116 1
            $options['json']    = $this->json;
117
        }
118
119 15
        if ($this->query) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->query of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
120 3
            $options['query'] = $this->query;
121
        }
122
123 15
        if ($this->basicAuth) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->basicAuth of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124 2
            $options['auth'] = $this->basicAuth;
125
        }
126
127 15
        return $options;
128
    }
129
}
130