Skip to content

Commit 0873386

Browse files
committed
Fix merging pending requests
1 parent b242eab commit 0873386

File tree

12 files changed

+146
-30
lines changed

12 files changed

+146
-30
lines changed

src/HttpClient/BaseHttpClient.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use function array_push;
1818
use function is_null;
1919
use function max;
20+
use function user_error;
21+
use const E_USER_DEPRECATED;
2022

2123
abstract class BaseHttpClient
2224
{
@@ -263,10 +265,27 @@ public function clearRequests()
263265
* @param RequestInterface[] $requests
264266
*
265267
* @return HttpClientException[]|ResponseInterface[]
268+
*
269+
* @throws InvalidArgumentException
266270
*/
267271
public function doRequests($requests = [])
268272
{
269-
$requests = array_merge($this->pendingRequests, $requests);
273+
if ($requests instanceof RequestInterface) {
274+
user_error(
275+
'Passing a single request to HttpClientInterface::doRequests is deprecated',
276+
E_USER_DEPRECATED
277+
);
278+
$requests = [$requests];
279+
}
280+
if (!is_array($requests)) {
281+
throw new InvalidArgumentException('Invalid requests array passed');
282+
}
283+
if (!is_array($this->pendingRequests)) {
284+
$this->pendingRequests = [];
285+
}
286+
287+
// Handle pending requests as well
288+
$requests = $this->pendingRequests + $requests;
270289

271290
$responses = [];
272291
foreach ($requests as $id => $request) {

src/HttpClient/CurlClient.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,26 @@
2727
namespace Firstred\PostNL\HttpClient;
2828

2929
use Composer\CaBundle\CaBundle;
30+
use Exception;
3031
use Firstred\PostNL\Exception\ApiConnectionException;
3132
use Firstred\PostNL\Exception\ApiException;
3233
use Firstred\PostNL\Exception\HttpClientException;
34+
use Firstred\PostNL\Exception\InvalidArgumentException;
3335
use GuzzleHttp\Psr7\Message as PsrMessage;
3436
use Psr\Http\Message\RequestInterface;
3537
use Psr\Http\Message\ResponseInterface;
3638
use Psr\Log\LoggerAwareInterface;
3739
use Psr\Log\LogLevel;
3840
use function define;
3941
use function defined;
42+
use function is_array;
43+
use function user_error;
4044
use const CURLOPT_FOLLOWLOCATION;
4145
use const CURLOPT_HTTPHEADER;
4246
use const CURLOPT_PROTOCOLS;
4347
use const CURLOPT_REDIR_PROTOCOLS;
4448
use const CURLOPT_SSL_VERIFYPEER;
49+
use const E_USER_DEPRECATED;
4550

4651
if (!defined('CURL_SSLVERSION_TLSv1')) {
4752
define('CURL_SSLVERSION_TLSv1', 1);
@@ -136,12 +141,30 @@ public function doRequest(RequestInterface $request)
136141
* @param RequestInterface[] $requests
137142
*
138143
* @return ResponseInterface[]|HttpClientException[]
144+
*
145+
* @throws InvalidArgumentException
139146
*/
140147
public function doRequests($requests = [])
141148
{
149+
if ($requests instanceof RequestInterface) {
150+
user_error(
151+
'Passing a single request to HttpClientInterface::doRequests is deprecated',
152+
E_USER_DEPRECATED
153+
);
154+
$requests = [$requests];
155+
}
156+
if (!is_array($requests)) {
157+
throw new InvalidArgumentException('Invalid requests array passed');
158+
}
159+
if (!is_array($this->pendingRequests)) {
160+
$this->pendingRequests = [];
161+
}
162+
142163
// Reset request headers array
143164
$curlHandles = [];
144165
$mh = curl_multi_init();
166+
167+
// Handle pending requests as well
145168
$requests = $this->pendingRequests + $requests;
146169
foreach ($requests as $uuid => $request) {
147170
$curl = curl_init();

src/HttpClient/GuzzleClient.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
namespace Firstred\PostNL\HttpClient;
2828

2929
use Composer\CaBundle\CaBundle;
30+
use Exception;
3031
use Firstred\PostNL\Exception\HttpClientException;
32+
use Firstred\PostNL\Exception\InvalidArgumentException;
3133
use Firstred\PostNL\Exception\ResponseException;
3234
use GuzzleHttp\Client;
3335
use GuzzleHttp\Exception\ConnectException;
@@ -45,7 +47,10 @@
4547
use Psr\Log\LoggerAwareInterface;
4648
use Psr\Log\LoggerInterface;
4749
use Psr\Log\LogLevel;
50+
use function is_array;
4851
use function method_exists;
52+
use function user_error;
53+
use const E_USER_DEPRECATED;
4954

5055
/**
5156
* Class GuzzleClient.
@@ -247,19 +252,26 @@ public function doRequest(RequestInterface $request)
247252
* @param RequestInterface[] $requests
248253
*
249254
* @return HttpClientException[]|ResponseInterface[]
255+
*
256+
* @throws InvalidArgumentException
250257
*/
251258
public function doRequests($requests = [])
252259
{
253-
// If this is a single request, create the requests array
254-
if (!is_array($requests)) {
255-
if (!$requests instanceof RequestInterface) {
256-
return [];
257-
}
258-
260+
if ($requests instanceof RequestInterface) {
261+
user_error(
262+
'Passing a single request to HttpClientInterface::doRequests is deprecated',
263+
E_USER_DEPRECATED
264+
);
259265
$requests = [$requests];
260266
}
267+
if (!is_array($requests)) {
268+
throw new InvalidArgumentException('Invalid requests array passed');
269+
}
270+
if (!is_array($this->pendingRequests)) {
271+
$this->pendingRequests = [];
272+
}
261273

262-
// Handle pending requests
274+
// Handle pending requests as well
263275
$requests = $this->pendingRequests + $requests;
264276
$this->clearRequests();
265277

src/HttpClient/HTTPlugClient.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Firstred\PostNL\Exception\HttpClientException;
7+
use Firstred\PostNL\Exception\InvalidArgumentException;
78
use Firstred\PostNL\Util\EachPromise;
89
use GuzzleHttp\Psr7\Message as PsrMessage;
910
use Http\Client\Exception\HttpException;
@@ -22,6 +23,9 @@
2223
use Psr\Http\Message\ResponseInterface;
2324
use Psr\Log\LoggerInterface;
2425
use Psr\Log\LogLevel;
26+
use function is_array;
27+
use function user_error;
28+
use const E_USER_DEPRECATED;
2529

2630
/**
2731
* Class HTTPlugClient.
@@ -100,15 +104,29 @@ public function __construct(
100104
*
101105
* Exceptions are captured into the result array
102106
*
103-
* @param array $requests
107+
* @param RequestInterface[] $requests
104108
*
105-
* @psalm-param array<string, RequestInterface> $requests
109+
* @return HttpClientException[]|ResponseInterface[]
106110
*
107-
* @return array
111+
* @throws InvalidArgumentException
108112
*/
109113
public function doRequests($requests = [])
110114
{
111-
// Handle pending requests
115+
if ($requests instanceof RequestInterface) {
116+
user_error(
117+
'Passing a single request to HttpClientInterface::doRequests is deprecated',
118+
E_USER_DEPRECATED
119+
);
120+
$requests = [$requests];
121+
}
122+
if (!is_array($requests)) {
123+
throw new InvalidArgumentException('Invalid requests array passed');
124+
}
125+
if (!is_array($this->pendingRequests)) {
126+
$this->pendingRequests = [];
127+
}
128+
129+
// Handle pending requests as well
112130
$requests = $this->pendingRequests + $requests;
113131
$this->clearRequests();
114132

src/HttpClient/MockClient.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
namespace Firstred\PostNL\HttpClient;
2828

29+
use Exception;
2930
use Firstred\PostNL\Exception\HttpClientException;
31+
use Firstred\PostNL\Exception\InvalidArgumentException;
3032
use Firstred\PostNL\Exception\ResponseException;
3133
use GuzzleHttp\Client;
3234
use GuzzleHttp\Exception\GuzzleException;
@@ -38,6 +40,9 @@
3840
use Psr\Http\Message\ResponseInterface;
3941
use Psr\Log\LoggerAwareInterface;
4042
use Psr\Log\LogLevel;
43+
use function is_array;
44+
use function user_error;
45+
use const E_USER_DEPRECATED;
4146

4247
/**
4348
* Class MockClient.
@@ -182,19 +187,26 @@ public function doRequest(RequestInterface $request)
182187
* @param RequestInterface[] $requests
183188
*
184189
* @return ResponseInterface[]|HttpClientException[]
190+
*
191+
* @throws InvalidArgumentException
185192
*/
186193
public function doRequests($requests = [])
187194
{
188-
// If this is a single request, create the requests array
189-
if (!is_array($requests)) {
190-
if (!$requests instanceof RequestInterface) {
191-
return [];
192-
}
193-
195+
if ($requests instanceof RequestInterface) {
196+
user_error(
197+
'Passing a single request to HttpClientInterface::doRequests is deprecated',
198+
E_USER_DEPRECATED
199+
);
194200
$requests = [$requests];
195201
}
202+
if (!is_array($requests)) {
203+
throw new InvalidArgumentException('Invalid requests array passed');
204+
}
205+
if (!is_array($this->pendingRequests)) {
206+
$this->pendingRequests = [];
207+
}
196208

197-
// Handle pending requests
209+
// Handle pending requests as well
198210
$requests = $this->pendingRequests + $requests;
199211
$this->clearRequests();
200212

src/HttpClient/SymfonyHttpClient.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
namespace Firstred\PostNL\HttpClient;
2828

2929
use Composer\CaBundle\CaBundle;
30+
use Exception;
3031
use Firstred\PostNL\Exception\HttpClientException;
32+
use Firstred\PostNL\Exception\InvalidArgumentException;
3133
use Firstred\PostNL\Exception\NotSupportedException;
3234
use GuzzleHttp\Psr7\Message as PsrMessage;
3335
use Psr\Http\Message\RequestInterface;
@@ -45,6 +47,9 @@
4547
use Symfony\Contracts\HttpClient\HttpClientInterface;
4648
use Symfony\Contracts\HttpClient\ResponseInterface as SymfonyHttpClientResponseInterface;
4749
use function array_merge;
50+
use function is_array;
51+
use function user_error;
52+
use const E_USER_DEPRECATED;
4853

4954
/**
5055
* Class SymfonyHttpClientInterface.
@@ -231,9 +236,26 @@ public function doRequest(RequestInterface $request)
231236
* @param RequestInterface[] $requests
232237
*
233238
* @return HttpClientException[]|ResponseInterface[]
239+
*
240+
* @throws InvalidArgumentException
234241
*/
235242
public function doRequests($requests = [])
236243
{
244+
if ($requests instanceof RequestInterface) {
245+
user_error(
246+
'Passing a single request to HttpClientInterface::doRequests is deprecated',
247+
E_USER_DEPRECATED
248+
);
249+
$requests = [$requests];
250+
}
251+
if (!is_array($requests)) {
252+
throw new InvalidArgumentException('Invalid requests array passed');
253+
}
254+
if (!is_array($this->pendingRequests)) {
255+
$this->pendingRequests = [];
256+
}
257+
258+
// Handle pending requests as well
237259
$requests = $this->pendingRequests + $requests;
238260
$httpClient = $this->getClient();
239261
$responses = [];

src/PostNL.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,7 @@ function ($barcode) {
18051805
* @throws PsrCacheInvalidArgumentException
18061806
* @throws ResponseException
18071807
* @throws NotFoundException
1808+
* @throws ShipmentNotFoundException
18081809
*
18091810
* @since 1.2.0
18101811
*/

src/Service/BarcodeService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function generateBarcodeSOAP(GenerateBarcode $generateBarcode)
141141
* @throws HttpClientException
142142
* @throws ResponseException
143143
* @throws InvalidConfigurationException
144+
* @throws \Firstred\PostNL\Exception\InvalidArgumentException
144145
*
145146
* @since 1.0.0
146147
*/
@@ -176,6 +177,7 @@ public function generateBarcodesREST(array $generateBarcodes)
176177
* @throws CifException
177178
* @throws HttpClientException
178179
* @throws ResponseException
180+
* @throws \Firstred\PostNL\Exception\InvalidArgumentException
179181
*
180182
* @since 1.0.0
181183
*/

src/Service/ConfirmingService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public function confirmShipmentSOAP(Confirming $confirming)
195195
* @throws CifException
196196
* @throws HttpClientException
197197
* @throws ResponseException
198+
* @throws InvalidArgumentException
198199
*
199200
* @since 1.0.0
200201
*/

src/Service/LabellingService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ public function generateLabelSOAP(GenerateLabel $generateLabel, $confirm = true)
268268
* @throws HttpClientException
269269
* @throws PsrCacheInvalidArgumentException
270270
* @throws ResponseException
271+
* @throws PostNLInvalidArgumentException
271272
*
272273
* @since 1.0.0
273274
*/

0 commit comments

Comments
 (0)