Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit --testdox"
}
}
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,9 @@ class Client
/** @var \Mvdnbrk\DhlParcel\Endpoints\TrackTrace */
public $tracktrace;

/** @var Capabilities */
public $capabilities;

public function __construct()
{
$this->httpClient = new HttpClient([
Expand All @@ -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);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Endpoints/BaseEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
75 changes: 75 additions & 0 deletions src/Endpoints/Capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Mvdnbrk\DhlParcel\Endpoints;

use Mvdnbrk\DhlParcel\Resources\Capability as CapabilityResource;
use Mvdnbrk\DhlParcel\Resources\Dimensions;
use Mvdnbrk\DhlParcel\Resources\Option;
use Mvdnbrk\DhlParcel\Resources\ParcelType;
use Mvdnbrk\DhlParcel\Resources\Product;
use Tightenco\Collect\Support\Collection;

class Capabilities extends BaseEndpoint
{
public function get(string $senderType, array $filters = []): Collection
{
$queryString = $this->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;
}
}
33 changes: 33 additions & 0 deletions src/Resources/Capability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

use Tightenco\Collect\Support\Collection;

class Capability extends BaseResource
{
/** @var int */
public $rank;

/** @var string */
public $fromCountryCode;

/** @var string */
public $toCountryCode;

/** @var Product */
public $product;

/** @var ParcelType */
public $parcelType;

/** @var \Tightenco\Collect\Support\Collection */
public $options;

public function __construct(array $attributes = [])
{
$this->options = new Collection;

parent::__construct($attributes);
}
}
15 changes: 15 additions & 0 deletions src/Resources/Dimensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class Dimensions extends BaseResource
{
/** @var int */
public $maxLengthCm;

/** @var int */
public $maxWidthCm;

/** @var int */
public $maxHeightCm;
}
21 changes: 21 additions & 0 deletions src/Resources/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class Option extends BaseResource
{
/** @var string */
public $key;

/** @var string */
public $description;

/** @var int */
public $rank;

/** @var int */
public $code;

/** @var string */
public $optionType;
}
17 changes: 15 additions & 2 deletions src/Resources/Parcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,24 @@ 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.
*
* @param \Mvdnbrk\DhlParcel\Resources\ShipmentOptions|array $value
* @return void
* @return void
*/
public function setOptionsAttribute($value): void
{
Expand Down Expand Up @@ -115,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
{
Expand Down
33 changes: 33 additions & 0 deletions src/Resources/ParcelType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class ParcelType extends BaseResource
{
/** @var string */
public $key;

/** @var string */
public $category;

/** @var int */
public $minWeightKg;

/** @var int */
public $maxWeightKg;

/** @var int */
public $defaultWeightKg;

/** @var Dimensions */
public $dimensions;

/** @var int */
public $minWeightGrams;

/** @var int */
public $maxWeightGrams;

/** @var int */
public $defaultWeightGrams;
}
30 changes: 30 additions & 0 deletions src/Resources/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class Product extends BaseResource
{
/** @var string */
public $key;

/** @var string */
public $label;

/** @var string */
public $code;

/** @var string */
public $menuCode;

/** @var bool */
public $businessProduct;

/** @var bool */
public $monoColloProduct;

/** @var string */
public $softwareCharacteristic;

/** @var bool */
public $returnProduct;
}
20 changes: 20 additions & 0 deletions src/Resources/ShipmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ShipmentOptions extends BaseResource
/** @var bool */
public $signature;

/** @var int|float */
protected $insured;

public function __construct(array $attributes = [])
{
$this->setDefaultOptions();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -136,6 +150,12 @@ public function toArray(): array
'key' => 'EVE',
]);
})
->when($this->insured, function ($collection) {
return $collection->push([
'key' => 'INS',
'input' => $this->insured,
]);
})
->all();
}
}
Loading