Client   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.07%

Importance

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

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A doRequest() 0 16 2
A getPageCount() 0 8 3
A getItemCount() 0 8 3
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 $method
57
     * @param $uri
58
     * @param array $options
59
     * @return array
60
     * @throws \GuzzleHttp\Exception\GuzzleException
61 3
     */
62
    public function doRequest($method, $uri, array $options = []) : array
63 3
    {
64 3
        $optionsResolver = new OptionsResolver();
65
        $this->configureOptions($optionsResolver);
66 3
67 3
        $url = $this->baseUrl . $uri;
68 3
        $options = $optionsResolver->resolve(array_replace($this->defaultOptions, $options));
69
        $this->lastResponse = $this->getHttpClient()->request($method, $url, $options);
70 3
        try {
71 3
            $res = \GuzzleHttp\json_decode((string)$this->lastResponse->getBody(), true);
72 3
        } catch (\InvalidArgumentException $e) {
73
            $res = ['error' => '['.$this->lastResponse->getStatusCode().'] The response body was not correctly formatted JSON. Inspect the last response to figure out the reason for this.'];
74
        }
75 3
76
        return $res;
77
    }
78
79
    /******************
80
     * Helper methods *
81 9
     *****************/
82
    /**
83 9
     * Returns the number of pages in the collection
84 6
     * Returns 0 if the header isn't set
85
     *
86 9
     * @return int
87
     */
88
    public function getPageCount() : int
89
    {
90
        if($this->lastResponse && $this->lastResponse->getHeaderLine('X-Total-Pages')) {
91
            return (int)$this->lastResponse->getHeaderLine('X-Total-Pages');
92
        }
93 3
94
        return 0;
95 3
    }
96 3
97
    /**
98
     * Returns the total item count in the collection
99
     * Returns 0 if the header isn't set
100
     *
101
     * @return int
102 6
     */
103
    public function getItemCount() : int
104 6
    {
105
        if($this->lastResponse && $this->lastResponse->getHeaderLine('X-Total-Count')) {
106
            return (int)$this->lastResponse->getHeaderLine('X-Total-Count');
107
        }
108
109
        return 0;
110
    }
111 3
112
    /**
113 3
     * @return GuzzleClientInterface
114 3
     */
115
    public function getHttpClient() : GuzzleClientInterface
116
    {
117
        if (!$this->httpClient) {
118
            $this->httpClient = new GuzzleClient();
119
        }
120 6
        return $this->httpClient;
121
    }
122 6
123
    /**
124
     * @param GuzzleClientInterface $httpClient
125
     * @return Client
126
     */
127
    public function setHttpClient(GuzzleClientInterface $httpClient) : self
128
    {
129 3
        $this->httpClient = $httpClient;
130
        return $this;
131 3
    }
132 3
133
    /**
134
     * @return string
135
     */
136
    public function getUsername(): string
137
    {
138 3
        return $this->username;
139
    }
140 3
141
    /**
142
     * @param string $username
143
     * @return Client
144
     */
145
    public function setUsername(string $username) : self
146
    {
147 3
        $this->username = $username;
148
        return $this;
149 3
    }
150 3
151
    /**
152
     * @return string
153
     */
154
    public function getPassword(): string
155
    {
156 6
        return $this->password;
157
    }
158 6
159
    /**
160
     * @param string $password
161
     * @return Client
162
     */
163
    public function setPassword(string $password) : self
164
    {
165 3
        $this->password = $password;
166
        return $this;
167 3
    }
168 3
169
    /**
170
     * @return string
171
     */
172
    public function getBaseUrl() : string
173
    {
174
        return $this->baseUrl;
175
    }
176
177
    /**
178
     * @param string $baseUrl
179
     * @return Client
180
     */
181
    public function setBaseUrl(string $baseUrl) : self
182
    {
183
        $this->baseUrl = rtrim($baseUrl, '/');
184
        return $this;
185
    }
186
187
    /**
188
     * @return array
189 3
     */
190
    public function getDefaultOptions() : array
191
    {
192 3
        return $this->defaultOptions;
193 3
    }
194
195
    /**
196 3
     * @param array $options
197 3
     * @return Client
198
     */
199 3
    public function setDefaultOptions(array $options) : self
200
    {
201
        $this->defaultOptions = $options;
202 3
        return $this;
203 3
    }
204
205
    /**
206 3
     * @return ResponseInterface
207
     */
208
    public function getLastResponse(): ResponseInterface
209
    {
210
        return $this->lastResponse;
211
    }
212
213
    /**
214
     * @param ResponseInterface $lastResponse
215
     * @return Client
216
     */
217
    public function setLastResponse(ResponseInterface $lastResponse) : self
218
    {
219
        $this->lastResponse = $lastResponse;
220
        return $this;
221
    }
222
223
    protected function configureOptions(OptionsResolver $optionsResolver)
224
    {
225
        // add request options from Guzzle
226
        $reflection = new \ReflectionClass(RequestOptions::class);
227
        $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...
228
229
        // set defaults
230
        $optionsResolver->setDefaults([
231
            'allow_redirects' => false,
232
            'cookies' => false,
233
            'timeout' => 60,
234
            'http_errors' => false,
235
            'auth' => [
236
                $this->username,
237
                $this->password
238
            ]
239
        ]);
240
    }
241
}
242