Completed
Push — master ( e1a483...2b8e5b )
by Joachim
15:02
created

Client::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 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
    public function __construct($username, $password)
49
    {
50
        $this->username = $username;
51
        $this->password = $password;
52
        $this->defaultOptions = [];
53
    }
54
55
    /**
56
     * @param string $method
57
     * @param string $uri
58
     * @param array $options
59
     * @return array
60
     */
61
    public function doRequest($method, $uri, array $options = []) : array
62
    {
63
        $optionsResolver = new OptionsResolver();
64
        $this->configureOptions($optionsResolver);
65
66
        $url = $this->baseUrl . $uri;
67
        $options = $optionsResolver->resolve(array_replace($this->defaultOptions, $options));
68
        $this->lastResponse = $this->getHttpClient()->request($method, $url, $options);
69
        try {
70
            $res = \GuzzleHttp\json_decode((string)$this->lastResponse->getBody());
71
        } catch (\InvalidArgumentException $e) {
72
            $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
        return $res;
76
    }
77
78
    /**
79
     * @return GuzzleClientInterface
80
     */
81
    public function getHttpClient() : GuzzleClientInterface
82
    {
83
        if (!$this->httpClient) {
84
            $this->httpClient = new GuzzleClient();
85
        }
86
        return $this->httpClient;
87
    }
88
89
    /**
90
     * @param GuzzleClientInterface $httpClient
91
     * @return Client
92
     */
93
    public function setHttpClient(GuzzleClientInterface $httpClient) : self
94
    {
95
        $this->httpClient = $httpClient;
96
        return $this;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getUsername(): string
103
    {
104
        return $this->username;
105
    }
106
107
    /**
108
     * @param string $username
109
     * @return Client
110
     */
111
    public function setUsername(string $username) : self
112
    {
113
        $this->username = $username;
114
        return $this;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getPassword(): string
121
    {
122
        return $this->password;
123
    }
124
125
    /**
126
     * @param string $password
127
     * @return Client
128
     */
129
    public function setPassword(string $password) : self
130
    {
131
        $this->password = $password;
132
        return $this;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getBaseUrl() : string
139
    {
140
        return $this->baseUrl;
141
    }
142
143
    /**
144
     * @param string $baseUrl
145
     * @return Client
146
     */
147
    public function setBaseUrl(string $baseUrl) : self
148
    {
149
        $this->baseUrl = rtrim($baseUrl, '/');
150
        return $this;
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function getDefaultOptions() : array
157
    {
158
        return $this->defaultOptions;
159
    }
160
161
    /**
162
     * @param array $options
163
     * @return Client
164
     */
165
    public function setDefaultOptions(array $options) : self
166
    {
167
        $this->defaultOptions = $options;
168
        return $this;
169
    }
170
171
    protected function configureOptions(OptionsResolver $optionsResolver)
172
    {
173
        // add request options from Guzzle
174
        $reflection = new \ReflectionClass(RequestOptions::class);
175
        $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...
176
177
        // set defaults
178
        $optionsResolver->setDefaults([
179
            'allow_redirects' => false,
180
            'cookies' => false,
181
            'timeout' => 60,
182
            'http_errors' => false,
183
            'auth' => [
184
                $this->username,
185
                $this->password
186
            ]
187
        ]);
188
    }
189
}
190