Skip to content

Commit

Permalink
feat(instabox): add same day delivery (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen authored Jan 27, 2022
1 parent e1af3ce commit e0725a9
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 70 deletions.
16 changes: 16 additions & 0 deletions src/Adapter/DeliveryOptions/AbstractShipmentOptionsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ abstract class AbstractShipmentOptionsAdapter
*/
protected $age_check;

/**
* @var bool|null
*/
protected $same_day_delivery;

/**
* @var bool|null
*/
Expand Down Expand Up @@ -81,6 +86,11 @@ public function isReturn(): ?bool
return $this->return;
}

public function isSameDayDelivery(): ?bool
{
return $this->same_day_delivery;
}

/**
* @return int|null
*/
Expand Down Expand Up @@ -152,6 +162,11 @@ public function setReturn(?bool $return): void
$this->return = $return;
}

public function setSameDayDelivery(?bool $sameDayDelivery): void
{
$this->same_day_delivery = $sameDayDelivery;
}

/**
* @param null|bool $largeFormat
*
Expand All @@ -173,6 +188,7 @@ public function toArray(): array
'age_check' => $this->hasAgeCheck(),
'only_recipient' => $this->hasOnlyRecipient(),
'return' => $this->isReturn(),
'same_day_delivery' => $this->isSameDayDelivery(),
'large_format' => $this->hasLargeFormat(),
'label_description' => $this->getLabelDescription(),
];
Expand Down
15 changes: 8 additions & 7 deletions src/Adapter/DeliveryOptions/ShipmentOptionsV3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class ShipmentOptionsV3Adapter extends AbstractShipmentOptionsAdapter
*/
public function __construct(array $shipmentOptions)
{
$this->signature = $shipmentOptions["signature"] ?? null;
$this->only_recipient = $shipmentOptions["only_recipient"] ?? null;
$this->insurance = $shipmentOptions["insurance"] ?? null;
$this->age_check = $shipmentOptions["age_check"] ?? null;
$this->large_format = $shipmentOptions["large_format"] ?? null;
$this->return = $shipmentOptions["return"] ?? null;
$this->label_description = $shipmentOptions["label_description"] ?? null;
$this->signature = $shipmentOptions['signature'] ?? null;
$this->only_recipient = $shipmentOptions['only_recipient'] ?? null;
$this->insurance = $shipmentOptions['insurance'] ?? null;
$this->age_check = $shipmentOptions['age_check'] ?? null;
$this->large_format = $shipmentOptions['large_format'] ?? null;
$this->return = $shipmentOptions['return'] ?? null;
$this->same_day_delivery = $shipmentOptions['same_day_delivery'] ?? null;
$this->label_description = $shipmentOptions['label_description'] ?? null;
}
}
23 changes: 3 additions & 20 deletions src/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MyParcelNL\Sdk\src\Model;

use MyParcelNL\Sdk\src\Support\Helpers;

abstract class BaseModel
{
/**
Expand All @@ -19,25 +21,6 @@ public function toArray(): array
*/
public function toArrayWithoutNull(): array
{
return array_filter(
$this->toArray(),
static function ($item) {
return null !== $item;
}
);
}

/**
* @param $param
*
* @return int|null
*/
protected function intOrNull($param): ?int
{
if ($param) {
return (int) $param;
}

return null;
return Helpers::toArrayWithoutNull($this->toArray());
}
}
40 changes: 34 additions & 6 deletions src/Model/Consignment/AbstractConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ abstract class AbstractConsignment
*/
use HasApiKey;

public const SHIPMENT_OPTION_AGE_CHECK = 'age_check';
public const SHIPMENT_OPTION_INSURANCE = 'insurance';
public const SHIPMENT_OPTION_LARGE_FORMAT = 'large_format';
public const SHIPMENT_OPTION_ONLY_RECIPIENT = 'only_recipient';
public const SHIPMENT_OPTION_RETURN = 'return';
public const SHIPMENT_OPTION_SIGNATURE = 'signature';
public const SHIPMENT_OPTION_AGE_CHECK = 'age_check';
public const SHIPMENT_OPTION_INSURANCE = 'insurance';
public const SHIPMENT_OPTION_LARGE_FORMAT = 'large_format';
public const SHIPMENT_OPTION_ONLY_RECIPIENT = 'only_recipient';
public const SHIPMENT_OPTION_RETURN = 'return';
public const SHIPMENT_OPTION_SIGNATURE = 'signature';
public const SHIPMENT_OPTION_SAME_DAY_DELIVERY = 'same_day_delivery';

public const EXTRA_OPTION_DELIVERY_DATE = 'delivery_date';
public const EXTRA_OPTION_DELIVERY_MONDAY = 'delivery_monday';
Expand Down Expand Up @@ -341,6 +342,12 @@ abstract class AbstractConsignment
*/
public $return;

/**
* @internal
* @var bool|null
*/
public $same_day_delivery;

/**
* @internal
* @var bool|null
Expand Down Expand Up @@ -1398,6 +1405,27 @@ public function setReturn(bool $return): self
return $this;
}

/**
* @return bool|null
*/
public function isSameDayDelivery(): ?bool
{
return $this->same_day_delivery;
}

/**
* @param bool $sameDay
*
* @return $this
*/
public function setSameDayDelivery(bool $sameDay): self
{
$this->same_day_delivery = $sameDay
&& in_array(self::SHIPMENT_OPTION_SAME_DAY_DELIVERY, $this->getAllowedShipmentOptions(), true);

return $this;
}

/**
* @return bool
*/
Expand Down
1 change: 1 addition & 0 deletions src/Model/Consignment/InstaboxConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function getAllowedShipmentOptions(): array
self::SHIPMENT_OPTION_LARGE_FORMAT,
self::SHIPMENT_OPTION_ONLY_RECIPIENT,
self::SHIPMENT_OPTION_RETURN,
self::SHIPMENT_OPTION_SAME_DAY_DELIVERY,
];
}

Expand Down
9 changes: 5 additions & 4 deletions src/Model/Fulfilment/OrderLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MyParcelNL\Sdk\src\Model\Fulfilment;

use MyParcelNL\Sdk\src\Model\BaseModel;
use MyParcelNL\Sdk\src\Support\Helpers;

class OrderLine extends BaseModel
{
Expand Down Expand Up @@ -63,10 +64,10 @@ class OrderLine extends BaseModel
public function __construct(array $data = [])
{
$this->uuid = $data['uuid'] ?? null;
$this->price = $this->intOrNull($data['price'] ?? null);
$this->vat = $this->intOrNull($data['vat'] ?? null);
$this->price_after_vat = $this->intOrNull($data['price_after_vat'] ?? null);
$this->quantity = $this->intOrNull($data['quantity'] ?? null);
$this->price = Helpers::intOrNull($data['price'] ?? null);
$this->vat = Helpers::intOrNull($data['vat'] ?? null);
$this->price_after_vat = Helpers::intOrNull($data['price_after_vat'] ?? null);
$this->quantity = Helpers::intOrNull($data['quantity'] ?? null);
$this->instructions = $data['instructions'] ?? null;

$this->product = new Product($data['product'] ?? []);
Expand Down
9 changes: 5 additions & 4 deletions src/Model/Fulfilment/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MyParcelNL\Sdk\src\Model\Fulfilment;

use MyParcelNL\Sdk\src\Model\BaseModel;
use MyParcelNL\Sdk\src\Support\Helpers;

class Product extends BaseModel
{
Expand Down Expand Up @@ -91,10 +92,10 @@ public function __construct(array $data = [])
$this->external_identifier = $data['external_identifier'] ?? null;
$this->name = $data['name'] ?? null;
$this->description = $data['description'] ?? null;
$this->width = $this->intOrNull($data['width'] ?? null);
$this->height = $this->intOrNull($data['height'] ?? null);
$this->length = $this->intOrNull($data['length'] ?? null);
$this->weight = $this->intOrNull($data['weight'] ?? null);
$this->width = Helpers::intOrNull($data['width'] ?? null);
$this->height = Helpers::intOrNull($data['height'] ?? null);
$this->length = Helpers::intOrNull($data['length'] ?? null);
$this->weight = Helpers::intOrNull($data['weight'] ?? null);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Model/MyParcelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public function sendRequest(string $method = 'POST', string $uri = self::REQUEST
$request = $this->instantiateCurl();

$request->write($method, $url, $this->getHeaders(), $this->getRequestBody());

$this->setResult($request);
$request->close();

Expand Down
12 changes: 7 additions & 5 deletions src/Services/ConsignmentEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
use MyParcelNL\Sdk\src\Support\Arr;
use MyParcelNL\Sdk\src\Support\Collection;
use MyParcelNL\Sdk\src\Support\Helpers;

class ConsignmentEncode
{
Expand Down Expand Up @@ -78,13 +79,14 @@ public static function encodeExtraOptions(array $consignmentEncoded, AbstractCon
$consignmentEncoded = array_merge_recursive(
$consignmentEncoded,
[
'options' => [
'options' => Helpers::toArrayWithoutNull([
'package_type' => $consignment->getPackageType(AbstractConsignment::DEFAULT_PACKAGE_TYPE),
'label_description' => $consignment->getLabelDescription(),
'only_recipient' => (int) $consignment->isOnlyRecipient(),
'signature' => (int) $consignment->isSignature(),
'return' => (int) $consignment->isReturn(),
],
'only_recipient' => Helpers::intOrNull($consignment->isOnlyRecipient()),
'signature' => Helpers::intOrNull($consignment->isSignature()),
'return' => Helpers::intOrNull($consignment->isReturn()),
'same_day_delivery' => Helpers::intOrNull($consignment->isSameDayDelivery()),
]),
]
);

Expand Down
18 changes: 3 additions & 15 deletions src/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1727,9 +1727,9 @@ static function ($value) {
public function toArrayWithoutNull(): array
{
return array_map(
function ($value) {
static function ($value) {
if (is_array($value)) {
return $this->filterNull($value);
return Helpers::toArrayWithoutNull($value);
}

if ($value && method_exists($value, 'toArrayWithoutNull')) {
Expand All @@ -1742,7 +1742,7 @@ function ($value) {

return $value;
},
$this->filterNull($this->items)
Helpers::toArrayWithoutNull($this->items)
);
}

Expand Down Expand Up @@ -1920,16 +1920,4 @@ public function __get($key)

return new HigherOrderCollectionProxy($this, $key);
}

/**
* @param array $value
*
* @return array
*/
private function filterNull(array $value): array
{
return array_filter($value, static function ($item) {
return null !== $item;
});
}
}
33 changes: 33 additions & 0 deletions src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,4 +1040,37 @@ public function with($value, callable $callback = null)
{
return is_null($callback) ? $value : $callback($value);
}

/**
* Convert value to int if it's truthy, return null otherwise.
*
* @param mixed $param
*
* @return int|null
*/
public static function intOrNull($param): ?int
{
if ($param) {
return (int) $param;
}

return null;
}

/**
* Filter null values from an array
*
* @param array $array
*
* @return array
*/
public static function toArrayWithoutNull(array $array): array
{
return array_filter(
$array,
static function ($item) {
return null !== $item;
}
);
}
}
8 changes: 6 additions & 2 deletions test/Bootstrap/ConsignmentTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ConsignmentTestCase extends TestCase
protected const RETURN = 'return';
protected const SAVE_RECIPIENT_ADDRESS = 'save_recipient_address';
protected const SIGNATURE = 'signature';
protected const SAME_DAY_DELIVERY = 'same_day_delivery';
protected const STREET = 'street';
protected const TOTAL_WEIGHT = 'total_weight';
protected const WEIGHT = 'weight';
Expand Down Expand Up @@ -278,14 +279,17 @@ protected function generateConsignment(array $data = [], bool $addDefaults = fal
/**
* Creates a delivery date two days in the future.
*
* @param string $interval
*
* @return string
* @throws \Exception
* @see https://www.php.net/manual/en/dateinterval.construct.php
*/
protected function generateDeliveryDate(): string
protected function generateDeliveryDate(string $interval = 'P2D'): string
{
return (new DateTime())
->setTime(0, 0)
->add(new DateInterval('P2D'))
->add(new DateInterval($interval))
->format('Y-m-d H:m:i');
}

Expand Down
18 changes: 11 additions & 7 deletions test/Model/Consignment/InstaboxConsignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class InstaboxConsignmentTest extends ConsignmentTestCase
public function provideRedJePakketjeConsignmentsData(): array
{
return $this->createConsignmentProviderDataset([
'NL -> NL' => [
self::ADD_DROPOFF_POINT => true,
self::FULL_STREET => 'Meander 631',
self::POSTAL_CODE => '6825ME',
self::CITY => 'Arnhem',
self::PHONE => '123456',
'NL -> NL' => [],
'same day delivery' => [
self::SAME_DAY_DELIVERY => false,
// Delivery date is today at 14:00, it's currently 0:00 so before the max cutoff time of 9:30.
self::DELIVERY_DATE => $this->generateDeliveryDate('PT14H'),
],
]);
}
Expand All @@ -47,7 +46,12 @@ protected function getDefaultConsignmentData(): array
return array_replace(
parent::getDefaultConsignmentData(),
[
self::CARRIER_ID => CarrierInstabox::ID,
self::CARRIER_ID => CarrierInstabox::ID,
self::ADD_DROPOFF_POINT => true,
self::FULL_STREET => 'Meander 631',
self::POSTAL_CODE => '6825ME',
self::CITY => 'Arnhem',
self::PHONE => '123456',
]
);
}
Expand Down

0 comments on commit e0725a9

Please sign in to comment.