From 4052b950cd2d91cdf856dfb130fe6a8f9d3f6d5b Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:42:03 +0200 Subject: [PATCH 1/9] Added capabilities endpoint --- composer.json | 2 +- src/Client.php | 5 ++ src/Endpoints/Capabilities.php | 76 +++++++++++++++++++ src/Resources/Capability.php | 34 +++++++++ src/Resources/Dimensions.php | 17 +++++ src/Resources/Option.php | 12 +++ src/Resources/ParcelType.php | 33 ++++++++ src/Resources/Product.php | 30 ++++++++ tests/Feature/Endpoints/CapabilitiesTest.php | 79 ++++++++++++++++++++ 9 files changed, 287 insertions(+), 1 deletion(-) create mode 100755 src/Endpoints/Capabilities.php create mode 100644 src/Resources/Capability.php create mode 100644 src/Resources/Dimensions.php create mode 100644 src/Resources/Option.php create mode 100644 src/Resources/ParcelType.php create mode 100644 src/Resources/Product.php create mode 100644 tests/Feature/Endpoints/CapabilitiesTest.php diff --git a/composer.json b/composer.json index 8d88af1..989d781 100755 --- a/composer.json +++ b/composer.json @@ -48,6 +48,6 @@ } }, "scripts": { - "test": "vendor/bin/phpunit" + "test": "vendor/bin/phpunit --testdox" } } diff --git a/src/Client.php b/src/Client.php index 6ae293a..127b534 100755 --- a/src/Client.php +++ b/src/Client.php @@ -8,6 +8,7 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\RequestOptions; use Mvdnbrk\DhlParcel\Endpoints\Authentication; +use Mvdnbrk\DhlParcel\Endpoints\Capabilities; use Mvdnbrk\DhlParcel\Endpoints\Labels; use Mvdnbrk\DhlParcel\Endpoints\ServicePoints; use Mvdnbrk\DhlParcel\Endpoints\Shipments; @@ -48,6 +49,9 @@ class Client /** @var \Mvdnbrk\DhlParcel\Endpoints\TrackTrace */ public $tracktrace; + /** @var Capabilities */ + public $capabilities; + public function __construct() { $this->httpClient = new HttpClient([ @@ -64,6 +68,7 @@ public function initializeEndpoints(): void $this->servicePoints = new ServicePoints($this); $this->shipments = new Shipments($this); $this->tracktrace = new TrackTrace($this); + $this->capabilities = new Capabilities($this); } /** diff --git a/src/Endpoints/Capabilities.php b/src/Endpoints/Capabilities.php new file mode 100755 index 0000000..be07128 --- /dev/null +++ b/src/Endpoints/Capabilities.php @@ -0,0 +1,76 @@ +buildQueryString($filters); + + $response = $this->performApiCall( + 'GET', + 'capabilities/'.$senderType.$queryString + ); + + $capabilities = new Collection(); + + collect($response)->each(function ($capability) use ($capabilities) { + $capabilityResource = new CapabilityResource([ + 'rank' => $capability->rank, + 'fromCountryCode' => $capability->fromCountryCode, + 'toCountryCode' => $capability->toCountryCode, + ]); + + $capabilityResource->product = new Product([ + "key" => $capability->product->key, + "label" => $capability->product->label, + "code" => $capability->product->code, + "menuCode" => $capability->product->menuCode, + "businessProduct" => $capability->product->businessProduct, + "monoColloProduct" => $capability->product->monoColloProduct, + "softwareCharacteristic" => $capability->product->softwareCharacteristic, + "returnProduct" => $capability->product->returnProduct, + ]); + + $capabilityResource->parcelType = new ParcelType([ + "key" => $capability->parcelType->key, + "category" => $capability->parcelType->category, + "minWeightKg" => $capability->parcelType->minWeightKg, + "maxWeightKg" => $capability->parcelType->maxWeightKg, + "defaultWeightKg" => $capability->parcelType->defaultWeightKg, + "minWeightGrams" => $capability->parcelType->minWeightGrams, + "maxWeightGrams" => $capability->parcelType->maxWeightGrams, + "defaultWeightGrams" => $capability->parcelType->defaultWeightGrams, + ]); + + $capabilityResource->parcelType->dimensions = new Dimensions([ + "maxLengthCm" => $capability->parcelType->dimensions->maxLengthCm, + "maxWidthCm" => $capability->parcelType->dimensions->maxWidthCm, + "maxHeightCm" => $capability->parcelType->dimensions->maxHeightCm, + ]); + + collect($capability->options)->each(function ($option) use ($capabilityResource) { + $capabilityResource->options->add(new Option([ + "key" => $option->key, + "description" => $option->description, + "rank" => $option->rank, + "code" => $option->code, + "optionType" => $option->optionType, + ])); + }); + + $capabilities->add($capabilityResource); + }); + + return $capabilities; + } +} diff --git a/src/Resources/Capability.php b/src/Resources/Capability.php new file mode 100644 index 0000000..ab2b5a8 --- /dev/null +++ b/src/Resources/Capability.php @@ -0,0 +1,34 @@ +options = new Collection; + + parent::__construct($attributes); + } +} diff --git a/src/Resources/Dimensions.php b/src/Resources/Dimensions.php new file mode 100644 index 0000000..e83c882 --- /dev/null +++ b/src/Resources/Dimensions.php @@ -0,0 +1,17 @@ +client->capabilities->get('business'); + + $this->assertGreaterThan(0, $capabilities->count()); + $this->assertInstanceOf(Capability::class, $capabilities->get(0)); + $this->assertInstanceOf(ParcelType::class, $capabilities->get(0)->parcelType); + $this->assertInstanceOf(Product::class, $capabilities->get(0)->product); + $this->assertInstanceOf(Option::class, $capabilities->get(0)->options->get(0)); + + $this->assertNotNull($capabilities->get(0)->rank); + $this->assertNotNull($capabilities->get(0)->fromCountryCode); + $this->assertNotNull($capabilities->get(0)->toCountryCode); + $this->assertNotNull($capabilities->get(0)->product); + $this->assertNotNull($capabilities->get(0)->parcelType); + $this->assertNotNull($capabilities->get(0)->options); + + $this->assertNotNull($capabilities->get(0)->parcelType->key); + $this->assertNotNull($capabilities->get(0)->parcelType->category); + $this->assertNotNull($capabilities->get(0)->parcelType->minWeightKg); + $this->assertNotNull($capabilities->get(0)->parcelType->maxWeightKg); + $this->assertNotNull($capabilities->get(0)->parcelType->defaultWeightKg); + $this->assertNotNull($capabilities->get(0)->parcelType->dimensions); + $this->assertNotNull($capabilities->get(0)->parcelType->minWeightGrams); + $this->assertNotNull($capabilities->get(0)->parcelType->maxWeightGrams); + $this->assertNotNull($capabilities->get(0)->parcelType->defaultWeightGrams); + + $this->assertNotNull($capabilities->get(0)->product->key); + $this->assertNotNull($capabilities->get(0)->product->label); + $this->assertNotNull($capabilities->get(0)->product->code); + $this->assertNotNull($capabilities->get(0)->product->menuCode); + $this->assertNotNull($capabilities->get(0)->product->businessProduct); + $this->assertNotNull($capabilities->get(0)->product->monoColloProduct); + $this->assertNotNull($capabilities->get(0)->product->softwareCharacteristic); + $this->assertNotNull($capabilities->get(0)->product->returnProduct); + + $this->assertNotNull($capabilities->get(0)->options->get(0)->key); + $this->assertNotNull($capabilities->get(0)->options->get(0)->description); + $this->assertNotNull($capabilities->get(0)->options->get(0)->rank); + $this->assertNotNull($capabilities->get(0)->options->get(0)->code); + $this->assertNotNull($capabilities->get(0)->options->get(0)->optionType); + } + + public function invalid_sender_type() + { + $this->expectException(DhlParcelException::class); + + $this->client->capabilities->get('somethingnonexisting'); + } + + public function test_specific_to_and_from_country() + { + $capabilities = $this->client->capabilities->get('business', [ + 'fromCountry' => 'NL', + 'toCountry' => 'BE', + ]); + + /** @var Capability $capability */ + $capability = $capabilities->get(0); + + $this->assertEquals('NL', $capability->fromCountryCode); + $this->assertEquals('BE', $capability->toCountryCode); + } +} From 4e8281bdcafdca067c92076d374eb8a529bed819 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:46:47 +0200 Subject: [PATCH 2/9] Added phpdoc --- src/Resources/Option.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Resources/Option.php b/src/Resources/Option.php index 3f6e3ec..de1551f 100644 --- a/src/Resources/Option.php +++ b/src/Resources/Option.php @@ -4,9 +4,18 @@ class Option extends BaseResource { + /** @var string */ public $key; + + /** @var string */ public $description; + + /** @var int */ public $rank; + + /** @var int */ public $code; + + /** @var string */ public $optionType; } From bd939f2a439d12b19357e7240976f76429ec07f4 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:53:48 +0200 Subject: [PATCH 3/9] Apply codestyle --- src/Endpoints/Capabilities.php | 49 ++++++++++---------- src/Resources/Dimensions.php | 2 - src/Resources/Product.php | 6 +-- tests/Feature/Endpoints/CapabilitiesTest.php | 1 - 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/Endpoints/Capabilities.php b/src/Endpoints/Capabilities.php index be07128..83ad0c3 100755 --- a/src/Endpoints/Capabilities.php +++ b/src/Endpoints/Capabilities.php @@ -7,7 +7,6 @@ use Mvdnbrk\DhlParcel\Resources\Option; use Mvdnbrk\DhlParcel\Resources\ParcelType; use Mvdnbrk\DhlParcel\Resources\Product; -use Mvdnbrk\DhlParcel\Resources\ShipmentPiece; use Tightenco\Collect\Support\Collection; class Capabilities extends BaseEndpoint @@ -31,40 +30,40 @@ public function get(string $senderType, array $filters = []): Collection ]); $capabilityResource->product = new Product([ - "key" => $capability->product->key, - "label" => $capability->product->label, - "code" => $capability->product->code, - "menuCode" => $capability->product->menuCode, - "businessProduct" => $capability->product->businessProduct, - "monoColloProduct" => $capability->product->monoColloProduct, - "softwareCharacteristic" => $capability->product->softwareCharacteristic, - "returnProduct" => $capability->product->returnProduct, + 'key' => $capability->product->key, + 'label' => $capability->product->label, + 'code' => $capability->product->code, + 'menuCode' => $capability->product->menuCode, + 'businessProduct' => $capability->product->businessProduct, + 'monoColloProduct' => $capability->product->monoColloProduct, + 'softwareCharacteristic' => $capability->product->softwareCharacteristic, + 'returnProduct' => $capability->product->returnProduct, ]); $capabilityResource->parcelType = new ParcelType([ - "key" => $capability->parcelType->key, - "category" => $capability->parcelType->category, - "minWeightKg" => $capability->parcelType->minWeightKg, - "maxWeightKg" => $capability->parcelType->maxWeightKg, - "defaultWeightKg" => $capability->parcelType->defaultWeightKg, - "minWeightGrams" => $capability->parcelType->minWeightGrams, - "maxWeightGrams" => $capability->parcelType->maxWeightGrams, - "defaultWeightGrams" => $capability->parcelType->defaultWeightGrams, + 'key' => $capability->parcelType->key, + 'category' => $capability->parcelType->category, + 'minWeightKg' => $capability->parcelType->minWeightKg, + 'maxWeightKg' => $capability->parcelType->maxWeightKg, + 'defaultWeightKg' => $capability->parcelType->defaultWeightKg, + 'minWeightGrams' => $capability->parcelType->minWeightGrams, + 'maxWeightGrams' => $capability->parcelType->maxWeightGrams, + 'defaultWeightGrams' => $capability->parcelType->defaultWeightGrams, ]); $capabilityResource->parcelType->dimensions = new Dimensions([ - "maxLengthCm" => $capability->parcelType->dimensions->maxLengthCm, - "maxWidthCm" => $capability->parcelType->dimensions->maxWidthCm, - "maxHeightCm" => $capability->parcelType->dimensions->maxHeightCm, + 'maxLengthCm' => $capability->parcelType->dimensions->maxLengthCm, + 'maxWidthCm' => $capability->parcelType->dimensions->maxWidthCm, + 'maxHeightCm' => $capability->parcelType->dimensions->maxHeightCm, ]); collect($capability->options)->each(function ($option) use ($capabilityResource) { $capabilityResource->options->add(new Option([ - "key" => $option->key, - "description" => $option->description, - "rank" => $option->rank, - "code" => $option->code, - "optionType" => $option->optionType, + 'key' => $option->key, + 'description' => $option->description, + 'rank' => $option->rank, + 'code' => $option->code, + 'optionType' => $option->optionType, ])); }); diff --git a/src/Resources/Dimensions.php b/src/Resources/Dimensions.php index e83c882..f530ef5 100644 --- a/src/Resources/Dimensions.php +++ b/src/Resources/Dimensions.php @@ -12,6 +12,4 @@ class Dimensions extends BaseResource /** @var int */ public $maxHeightCm; - - } diff --git a/src/Resources/Product.php b/src/Resources/Product.php index 5388997..0f9eaa1 100644 --- a/src/Resources/Product.php +++ b/src/Resources/Product.php @@ -16,15 +16,15 @@ class Product extends BaseResource /** @var string */ public $menuCode; - /** @var boolean */ + /** @var bool */ public $businessProduct; - /** @var boolean */ + /** @var bool */ public $monoColloProduct; /** @var string */ public $softwareCharacteristic; - /** @var boolean */ + /** @var bool */ public $returnProduct; } diff --git a/tests/Feature/Endpoints/CapabilitiesTest.php b/tests/Feature/Endpoints/CapabilitiesTest.php index cf6a414..b311e36 100644 --- a/tests/Feature/Endpoints/CapabilitiesTest.php +++ b/tests/Feature/Endpoints/CapabilitiesTest.php @@ -4,7 +4,6 @@ use Mvdnbrk\DhlParcel\Exceptions\DhlParcelException; use Mvdnbrk\DhlParcel\Resources\Capability; -use Mvdnbrk\DhlParcel\Resources\Dimensions; use Mvdnbrk\DhlParcel\Resources\Option; use Mvdnbrk\DhlParcel\Resources\ParcelType; use Mvdnbrk\DhlParcel\Resources\Product; From 44774224fe16943ec95084e6ceabb5c8b65be423 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:54:57 +0200 Subject: [PATCH 4/9] Apply codestyle --- src/Resources/Capability.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Resources/Capability.php b/src/Resources/Capability.php index ab2b5a8..42ef22c 100644 --- a/src/Resources/Capability.php +++ b/src/Resources/Capability.php @@ -24,7 +24,6 @@ class Capability extends BaseResource /** @var \Tightenco\Collect\Support\Collection */ public $options; - public function __construct(array $attributes = []) { $this->options = new Collection; From 9ee74184d59b3c432169642683b37fd865ac8424 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 13:28:34 +0200 Subject: [PATCH 5/9] Added INS option to ShipmentOptions --- src/Resources/Parcel.php | 13 ++++++++++++ src/Resources/ShipmentOptions.php | 20 ++++++++++++++++++ tests/Unit/Resources/ShipmentOptionsTest.php | 22 ++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 4209296..e44c065 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -76,6 +76,19 @@ public function signature(): self return $this; } + /** + * Set the amount for "Insured" in EUR. + * + * @param int|float $value + * @return $this + */ + public function setInsured($value): self + { + $this->options->setInsured($value); + + return $this; + } + /** * Set the shipment options for this parcel. * diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index eb510ec..59a3738 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -30,6 +30,9 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; + /** @var int|float */ + protected $insured; + public function __construct(array $attributes = []) { $this->setDefaultOptions(); @@ -90,6 +93,17 @@ public function setCashOnDelivery($value): void $this->cash_on_delivery = $value; } + /** + * Set the amount for the option "INS" in EUR. + * + * @param int|float $value + * @return void + */ + public function setInsured($value): void + { + $this->insured = $value; + } + public function toArray(): array { return collect() @@ -136,6 +150,12 @@ public function toArray(): array 'key' => 'EVE', ]); }) + ->when($this->insured, function($collection) { + return $collection->push([ + 'key' => 'INS', + 'input' => $this->insured, + ]); + }) ->all(); } } diff --git a/tests/Unit/Resources/ShipmentOptionsTest.php b/tests/Unit/Resources/ShipmentOptionsTest.php index cd295c1..7458f0f 100644 --- a/tests/Unit/Resources/ShipmentOptionsTest.php +++ b/tests/Unit/Resources/ShipmentOptionsTest.php @@ -201,4 +201,26 @@ public function to_array_with_cash_on_delivery() ], ], $array); } + + /** @test */ + public function to_array_with_insured() + { + $options = new ShipmentOptions([ + 'insured' => 150, + ]); + + $array = $options->toArray(); + + $this->assertIsArray($array); + + $this->assertEquals([ + [ + 'key' => 'DOOR', + ], + [ + 'key' => 'INS', + 'input' => 150, + ], + ], $array); + } } From 8fc9c14f07fbb856e97907ec05cfe5d7e55c0f89 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 13:33:53 +0200 Subject: [PATCH 6/9] Apply codestyle --- src/Resources/Parcel.php | 6 +++--- src/Resources/ShipmentOptions.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index e44c065..1c6e818 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -80,7 +80,7 @@ public function signature(): self * Set the amount for "Insured" in EUR. * * @param int|float $value - * @return $this + * @return $this */ public function setInsured($value): self { @@ -93,7 +93,7 @@ public function setInsured($value): self * Set the shipment options for this parcel. * * @param \Mvdnbrk\DhlParcel\Resources\ShipmentOptions|array $value - * @return void + * @return void */ public function setOptionsAttribute($value): void { @@ -128,7 +128,7 @@ public function servicePoint(string $value): self * Set the amount for "Cash On Delivery" in EUR. * * @param int|float $value - * @return $this + * @return $this */ public function cashOnDelivery($value): self { diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index 59a3738..055d588 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -150,7 +150,7 @@ public function toArray(): array 'key' => 'EVE', ]); }) - ->when($this->insured, function($collection) { + ->when($this->insured, function ($collection) { return $collection->push([ 'key' => 'INS', 'input' => $this->insured, From 0152b3325d3175eef562848ddfab9a223d945dd1 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 15 May 2024 12:15:20 +0200 Subject: [PATCH 7/9] Added a check for as 401 befdore parsing of json --- src/Endpoints/BaseEndpoint.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Endpoints/BaseEndpoint.php b/src/Endpoints/BaseEndpoint.php index 40f584e..5453da5 100644 --- a/src/Endpoints/BaseEndpoint.php +++ b/src/Endpoints/BaseEndpoint.php @@ -60,6 +60,10 @@ protected function performApiCall(string $httpMethod, string $apiMethod, ?string return $response->getBody()->getContents(); } + if ($response->getStatusCode() == 401) { + throw new DhlParcelException("Authentication failed", 0, $response); + } + $body = $response->getBody()->getContents(); $object = @json_decode($body); From 60ae025d5947e9432ba5e31251e444ac1b8ded00 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 15 May 2024 12:16:32 +0200 Subject: [PATCH 8/9] Fixed codestyle issue --- src/Endpoints/BaseEndpoint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Endpoints/BaseEndpoint.php b/src/Endpoints/BaseEndpoint.php index 5453da5..480a81c 100644 --- a/src/Endpoints/BaseEndpoint.php +++ b/src/Endpoints/BaseEndpoint.php @@ -61,7 +61,7 @@ protected function performApiCall(string $httpMethod, string $apiMethod, ?string } if ($response->getStatusCode() == 401) { - throw new DhlParcelException("Authentication failed", 0, $response); + throw new DhlParcelException("Authentication failed", 0, $response); } $body = $response->getBody()->getContents(); From f7f7a5b76defd5f99c3476cdfe69615be19e8305 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 15 May 2024 12:17:04 +0200 Subject: [PATCH 9/9] Fixed codestyle issue --- src/Endpoints/BaseEndpoint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Endpoints/BaseEndpoint.php b/src/Endpoints/BaseEndpoint.php index 480a81c..6f26894 100644 --- a/src/Endpoints/BaseEndpoint.php +++ b/src/Endpoints/BaseEndpoint.php @@ -61,7 +61,7 @@ protected function performApiCall(string $httpMethod, string $apiMethod, ?string } if ($response->getStatusCode() == 401) { - throw new DhlParcelException("Authentication failed", 0, $response); + throw new DhlParcelException('Authentication failed', 0, $response); } $body = $response->getBody()->getContents();