Skip to content

Commit

Permalink
Require newer version of Guzzle and PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
villermen committed Nov 14, 2020
1 parent 759368f commit c4b6606
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
"config": {
"sort-packages": true,
"platform": {
"php": "7.1.3"
"php": "7.2.5"
}
},
"require": {
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3",
"php": ">=7.1.3"
"guzzlehttp/guzzle": "^7.0.1",
"php": ">=7.2.5"
},
"require-dev": {
"phpunit/phpunit": "^7.5.7",
Expand Down
62 changes: 33 additions & 29 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace JouwWeb\SendCloud;

use JouwWeb\SendCloud\Model\ParcelItem;
use function GuzzleHttp\default_user_agent;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Utils;
use JouwWeb\SendCloud\Exception\SendCloudClientException;
use JouwWeb\SendCloud\Exception\SendCloudRequestException;
use JouwWeb\SendCloud\Exception\SendCloudStateException;
use JouwWeb\SendCloud\Exception\SendCloudWebhookException;
use JouwWeb\SendCloud\Model\Address;
use JouwWeb\SendCloud\Model\Parcel;
use JouwWeb\SendCloud\Model\ParcelItem;
use JouwWeb\SendCloud\Model\SenderAddress;
use JouwWeb\SendCloud\Model\ShippingMethod;
use JouwWeb\SendCloud\Model\User;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function __construct(
$secretKey,
],
'headers' => [
'User-Agent' => 'jouwweb/sendcloud ' . default_user_agent(),
'User-Agent' => 'jouwweb/sendcloud ' . Utils::defaultUserAgent(),
],
];

Expand All @@ -76,8 +77,8 @@ public function getUser(): User
{
try {
return new User(json_decode((string)$this->guzzleClient->get('user')->getBody(), true)['user']);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'An error occurred while fetching the Sendcloud user.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'An error occurred while fetching the Sendcloud user.');
}
}

Expand Down Expand Up @@ -116,8 +117,8 @@ public function getShippingMethods(?int $servicePointId = null): array
});

return $shippingMethods;
} catch (RequestException $exception) {
throw $this->parseRequestException(
} catch (TransferException $exception) {
throw $this->parseGuzzleException(
$exception,
'An error occurred while fetching shipping methods from the Sendcloud API.'
);
Expand Down Expand Up @@ -169,8 +170,8 @@ public function createParcel(
]);

return new Parcel(json_decode((string)$response->getBody(), true)['parcel']);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not create parcel in Sendcloud.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not create parcel in Sendcloud.');
}
}

Expand Down Expand Up @@ -206,8 +207,8 @@ public function updateParcel($parcel, Address $shippingAddress): Parcel
]);

return new Parcel(json_decode((string)$response->getBody(), true)['parcel']);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not update parcel in SendCloud.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not update parcel in SendCloud.');
}
}

Expand Down Expand Up @@ -245,8 +246,8 @@ public function createLabel($parcel, int $shippingMethodId, $senderAddress): Par
]);

return new Parcel(json_decode((string)$response->getBody(), true)['parcel']);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not create parcel with Sendcloud.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not create parcel with Sendcloud.');
}
}

Expand All @@ -262,15 +263,18 @@ public function cancelParcel($parcel): bool
try {
$this->guzzleClient->post(sprintf('parcels/%s/cancel', $this->parseParcelArgument($parcel)));
return true;
} catch (RequestException $exception) {
$statusCode = $exception->hasResponse() ? $exception->getResponse()->getStatusCode() : 0;
} catch (TransferException $exception) {
$statusCode = ($exception instanceof RequestException && $exception->hasResponse()
? $exception->getResponse()->getStatusCode()
: 0
);

// Handle documented rejections
if (in_array($statusCode, [400, 410])) {
return false;
}

throw $this->parseRequestException($exception, 'An error occurred while cancelling the parcel.');
throw $this->parseGuzzleException($exception, 'An error occurred while cancelling the parcel.');
}
}

Expand Down Expand Up @@ -302,8 +306,8 @@ public function getLabelPdf($parcel, int $format): string

try {
return (string)$this->guzzleClient->get($labelUrl)->getBody();
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not retrieve label.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve label.');
}
}

Expand Down Expand Up @@ -337,8 +341,8 @@ public function getBulkLabelPdf(array $parcels, int $format): string
]
],
]);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not retrieve label information.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve label information.');
}

$labelData = json_decode((string)$response->getBody(), true);
Expand All @@ -349,8 +353,8 @@ public function getBulkLabelPdf(array $parcels, int $format): string

try {
return (string)$this->guzzleClient->get($labelUrl)->getBody();
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not retrieve label PDF data.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve label PDF data.');
}
}

Expand All @@ -369,8 +373,8 @@ public function getSenderAddresses(): array
return array_map(function (array $senderAddressData) {
return new SenderAddress($senderAddressData);
}, $senderAddressesData);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not retrieve sender addresses.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve sender addresses.');
}
}

Expand All @@ -386,8 +390,8 @@ public function getParcel($parcel): Parcel
try {
$response = $this->guzzleClient->get('parcels/' . $this->parseParcelArgument($parcel));
return new Parcel(json_decode((string)$response->getBody(), true)['parcel']);
} catch (RequestException $exception) {
throw $this->parseRequestException($exception, 'Could not retrieve parcel.');
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve parcel.');
}
}

Expand Down Expand Up @@ -579,16 +583,16 @@ protected function getParcelData(
return $parcelData;
}

protected function parseRequestException(
RequestException $exception,
protected function parseGuzzleException(
TransferException $exception,
string $defaultMessage
): SendCloudRequestException {
$message = $defaultMessage;
$code = SendCloudRequestException::CODE_UNKNOWN;

$responseCode = null;
$responseMessage = null;
if ($exception->hasResponse()) {
if ($exception instanceof RequestException && $exception->hasResponse()) {
$responseData = json_decode((string)$exception->getResponse()->getBody(), true);
$responseCode = $responseData['error']['code'] ?? null;
$responseMessage = $responseData['error']['message'] ?? null;
Expand Down
4 changes: 2 additions & 2 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testCreateParcelCustoms(): void
$this->guzzleClientMock->expects($this->once())->method('request')
->willReturnCallback(function () {
$this->assertEquals([
'post',
'POST',
'parcels',
['json' => ['parcel' => ['name' => 'Dr. Coffee', 'company_name' => '', 'address' => 'Street', 'house_number' => '123', 'address_2' => 'Unit 83', 'city' => 'Place', 'postal_code' => '7837', 'country' => 'BM', 'email' => '[email protected]', 'telephone' => '', 'country_state' => '', 'customs_invoice_nr' => 'customsInvoiceNumber', 'customs_shipment_type' => 2, 'parcel_items' => [0 => ['description' => 'green tea', 'quantity' => 1, 'weight' => '0.123', 'value' => 15.2, 'hs_code' => '090210', 'origin_country' => 'EC'], 1 => ['description' => 'cardboard', 'quantity' => 3, 'weight' => '0.05','value' => 0.2, 'hs_code' => '090210', 'origin_country' => 'NL']]]]],
], func_get_args());
Expand Down Expand Up @@ -166,7 +166,7 @@ public function testUpdateParcel(): void
$this->guzzleClientMock->expects($this->once())->method('request')
->willReturnCallback(function () {
$this->assertEquals([
'put',
'PUT',
'parcels',
['json' => ['parcel' => ['id' => 8293794, 'name' => 'Completely different person', 'company_name' => 'Some company', 'address' => 'Rosebud', 'address_2' => 'Above the skies', 'house_number' => '2134A', 'city' => 'Almanda', 'postal_code' => '9238DD', 'country' => 'NL', 'email' => '[email protected]', 'telephone' => '+31699999999', 'country_state' => 'CS']]]
], func_get_args());
Expand Down

0 comments on commit c4b6606

Please sign in to comment.