Passed
Pull Request — develop (#30)
by Darío
04:39 queued 02:18
created

GuzzleRequest::getHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 13
    public function __construct(string $method, string $uri, array $options = [])
28
    {
29 13
        $this->method  = $method;
30 13
        $this->uri     = $uri;
31 13
        $this->options = $options;
32 13
    }
33
34 13
    public function getMethod(): string
35
    {
36 13
        return $this->method;
37
    }
38
39 13
    public function getUri(): string
40
    {
41 13
        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
    public function setMethod(string $method): self
60
    {
61
        $this->method = $method;
62
63
        return $this;
64
    }
65
66
    public function setUri(string $uri): self
67
    {
68
        $this->uri = $uri;
69
70
        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 13
    public function options()
107
    {
108
        $options = [
109 13
            'timeout' => $this->timeout,
110 13
            'verify' => $this->ssl,
111 13
            'headers' => $this->headers,
112
        ];
113
114 13
        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 13
        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 13
        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 13
        return $options;
128
    }
129
}
130