Completed
Push — master ( cd5217...cab1ff )
by Joachim
20:15
created

Client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.07%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 198
ccs 51
cts 56
cp 0.9107
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A doRequest() 0 16 2
A getHttpClient() 0 7 2
A setHttpClient() 0 5 1
A getUsername() 0 4 1
A setUsername() 0 5 1
A getPassword() 0 4 1
A setPassword() 0 5 1
A getBaseUrl() 0 4 1
A setBaseUrl() 0 5 1
A getDefaultOptions() 0 4 1
A setDefaultOptions() 0 5 1
A getLastResponse() 0 4 1
A setLastResponse() 0 5 1
A configureOptions() 0 18 1
1
<?php
2
namespace Loevgaard\Pakkelabels;
3
4
use GuzzleHttp\Client as GuzzleClient;
5
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
6
use GuzzleHttp\RequestOptions;
7
use Psr\Http\Message\ResponseInterface;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class Client
11
{
12
    /**
13
     * @var GuzzleClientInterface
14
     */
15
    protected $httpClient;
16
17
    /**
18
     * This is the API username which you find/generate under Settings > API
19
     *
20
     * @var string
21
     */
22
    private $username;
23
24
    /**
25
     * This is the API password which you find/generate under Settings > API
26
     *
27
     * @var string
28
     */
29
    private $password;
30
31
    /**
32
     * This is the base url for the API
33
     *
34
     * @var string
35
     */
36
    private $baseUrl = 'https://app.pakkelabels.dk/api/public/v3';
37
38
    /**
39
     * @var array
40
     */
41
    private $defaultOptions;
42
43
    /**
44
     * @var ResponseInterface
45
     */
46
    private $lastResponse;
47
48 15
    public function __construct($username, $password)
49
    {
50 15
        $this->username = $username;
51 15
        $this->password = $password;
52 15
        $this->defaultOptions = [];
53 15
    }
54
55
    /**
56
     * @param string $method
57
     * @param string $uri
58
     * @param array $options
59
     * @return array
60
     */
61 3
    public function doRequest($method, $uri, array $options = []) : array
62
    {
63 3
        $optionsResolver = new OptionsResolver();
64 3
        $this->configureOptions($optionsResolver);
65
66 3
        $url = $this->baseUrl . $uri;
67 3
        $options = $optionsResolver->resolve(array_replace($this->defaultOptions, $options));
68 3
        $this->lastResponse = $this->getHttpClient()->request($method, $url, $options);
69
        try {
70 3
            $res = \GuzzleHttp\json_decode((string)$this->lastResponse->getBody(), true);
71 3
        } catch (\InvalidArgumentException $e) {
72 3
            $res = ['error' => '['.$this->lastResponse->getStatusCode().'] The response body was not correctly formatted JSON. Inspect the last response to figure out the reason for this.'];
73
        }
74
75 3
        return $res;
76
    }
77
78
    /**
79
     * @return GuzzleClientInterface
80
     */
81 9
    public function getHttpClient() : GuzzleClientInterface
82
    {
83 9
        if (!$this->httpClient) {
84 6
            $this->httpClient = new GuzzleClient();
85
        }
86 9
        return $this->httpClient;
87
    }
88
89
    /**
90
     * @param GuzzleClientInterface $httpClient
91
     * @return Client
92
     */
93 3
    public function setHttpClient(GuzzleClientInterface $httpClient) : self
94
    {
95 3
        $this->httpClient = $httpClient;
96 3
        return $this;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 6
    public function getUsername(): string
103
    {
104 6
        return $this->username;
105
    }
106
107
    /**
108
     * @param string $username
109
     * @return Client
110
     */
111 3
    public function setUsername(string $username) : self
112
    {
113 3
        $this->username = $username;
114 3
        return $this;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 6
    public function getPassword(): string
121
    {
122 6
        return $this->password;
123
    }
124
125
    /**
126
     * @param string $password
127
     * @return Client
128
     */
129 3
    public function setPassword(string $password) : self
130
    {
131 3
        $this->password = $password;
132 3
        return $this;
133
    }
134
135
    /**
136
     * @return string
137
     */
138 3
    public function getBaseUrl() : string
139
    {
140 3
        return $this->baseUrl;
141
    }
142
143
    /**
144
     * @param string $baseUrl
145
     * @return Client
146
     */
147 3
    public function setBaseUrl(string $baseUrl) : self
148
    {
149 3
        $this->baseUrl = rtrim($baseUrl, '/');
150 3
        return $this;
151
    }
152
153
    /**
154
     * @return array
155
     */
156 6
    public function getDefaultOptions() : array
157
    {
158 6
        return $this->defaultOptions;
159
    }
160
161
    /**
162
     * @param array $options
163
     * @return Client
164
     */
165 3
    public function setDefaultOptions(array $options) : self
166
    {
167 3
        $this->defaultOptions = $options;
168 3
        return $this;
169
    }
170
171
    /**
172
     * @return ResponseInterface
173
     */
174
    public function getLastResponse(): ResponseInterface
175
    {
176
        return $this->lastResponse;
177
    }
178
179
    /**
180
     * @param ResponseInterface $lastResponse
181
     * @return Client
182
     */
183
    public function setLastResponse(ResponseInterface $lastResponse) : self
184
    {
185
        $this->lastResponse = $lastResponse;
186
        return $this;
187
    }
188
189 3
    protected function configureOptions(OptionsResolver $optionsResolver)
190
    {
191
        // add request options from Guzzle
192 3
        $reflection = new \ReflectionClass(RequestOptions::class);
193 3
        $optionsResolver->setDefined($reflection->getConstants());
0 ignored issues
show
Documentation introduced by
$reflection->getConstants() is of type array<string,integer|double|string|boolean>, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
194
195
        // set defaults
196 3
        $optionsResolver->setDefaults([
197 3
            'allow_redirects' => false,
198
            'cookies' => false,
199 3
            'timeout' => 60,
200
            'http_errors' => false,
201
            'auth' => [
202 3
                $this->username,
203 3
                $this->password
204
            ]
205
        ]);
206 3
    }
207
}
208