Skip to content

Commit

Permalink
add the function addConsignmentByConsignmentIds and addConsignmentByR…
Browse files Browse the repository at this point in the history
…eferenceIds
  • Loading branch information
RichardPerdaan committed Apr 1, 2019
1 parent 243003a commit 48e57c0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "myparcelnl/sdk",
"version": "v2.1.0-beta.3",
"version": "v2.1.0-beta.4",
"description": "This package is designed to send and receive data from MyParcel by means of an API.",
"homepage": "https://www.myparcel.nl",
"tags": ["MyParcel", "My Parcel", "Flespakket", "Post NL", "PostNL"],
Expand Down
12 changes: 7 additions & 5 deletions src/Adapter/ConsignmentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class ConsignmentAdapter

/**
* ConsignmentDecode constructor.
*
* @param array $data
* @param string $apiKey
* @param MyParcelConsignment $consignment
*
* @throws \Exception
*/
public function __construct($data, $apiKey)
public function __construct($data, $consignment)
{
$this->data = $data;
$this->consignment = (new MyParcelConsignment())->setApiKey($apiKey);
$this->consignment = $consignment;

$this
->baseOptions()
Expand Down Expand Up @@ -67,8 +69,8 @@ private function baseOptions()
->setPostalCode($recipient['postal_code'])
->setStreet($recipient['street'])
->setCity($recipient['city'])
->setEmail($recipient['email'])
->setPhone($recipient['phone'])
->setEmail( isset($recipient['email']) ? $recipient['email'] : '')

This comment has been minimized.

Copy link
@reindert-vetter

reindert-vetter Apr 1, 2019

Contributor

->setEmail(isset($recipient['email']) ? $recipient['email'] : '')

This comment has been minimized.

Copy link
@RichardPerdaan

RichardPerdaan Jul 8, 2019

Author Member

done

->setPhone( isset($recipient['phone']) ? $recipient['phone'] : '')
->setPackageType($options['package_type'])
->setLabelDescription(isset($options['label_description']) ? $options['label_description'] : '')
;
Expand Down
54 changes: 48 additions & 6 deletions src/Helper/MyParcelCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,46 @@ public function addConsignment(MyParcelConsignment $consignment)
return $this;
}

/**
* @param int[] $ids
* @param sting $apiKey
*
* @return self
* @throws \Exception
*/
public function addConsignmentByConsignmentIds($ids, $apiKey)
{
foreach ($ids as $consignmentId) {
$consignment = (new MyParcelConsignment())
->setApiKey($apiKey)
->setMyParcelConsignmentId($consignmentId);

$this->addConsignment($consignment);
}

return $this;
}

/**
* @param string[] $ids
* @param string $apiKey
*
* @return self
* @throws \Exception
*/
public function addConsignmentByReferenceIds($ids, $apiKey)
{
foreach ($ids as $referenceId) {
$consignment = (new MyParcelConsignment())
->setApiKey($apiKey)
->setReferenceId($referenceId);

$this->addConsignment($consignment);
}

return $this;
}

/**
* @param MyParcelConsignment $consignment
* @param $amount
Expand All @@ -185,9 +225,11 @@ public function addMultiCollo(MyParcelConsignment $consignment, $amount): self
{
$i = 1;

$consignment->setMultiCollo();
if ($amount > 1) {
$consignment->setMultiCollo();
}

if (null == $consignment->getReferenceId()) {
if ($consignment->isPartOfMultiCollo() && null == $consignment->getReferenceId()) {
$consignment->setReferenceId('random_multi_collo_' . uniqid());
}

Expand Down Expand Up @@ -320,7 +362,7 @@ public function setLatestDataWithoutIds($key, $size = 300)
}

foreach ($request->getResult()['data']['shipments'] as $shipment) {
$consignmentAdapter = new ConsignmentAdapter($shipment, $key);
$consignmentAdapter = new ConsignmentAdapter($shipment, (new MyParcelConsignment())->setApiKey($key));
$this->addConsignment($consignmentAdapter->getConsignment());
}

Expand Down Expand Up @@ -545,7 +587,7 @@ public function getUserAgent()
* @param string $platform
* @param string $version
* @internal param string $user_agent
* @return $this
* @return self
*/
public function setUserAgent($platform, $version = null)
{
Expand Down Expand Up @@ -603,14 +645,14 @@ private function getNewCollectionFromResult($result)
$consignments = $this->getConsignmentsByReferenceId($shipment['reference_identifier']);
}

$consignmentAdapter = new ConsignmentAdapter($shipment, $consignments->first()->getApiKey());
$consignmentAdapter = new ConsignmentAdapter($shipment, $consignments->first());
$isMultiCollo = ! empty($shipment['secondary_shipments']);
$newCollection->addConsignment($consignmentAdapter->getConsignment()->setMultiCollo($isMultiCollo));

foreach ($shipment['secondary_shipments'] as $secondaryShipment) {

$secondaryShipment = Arr::arrayMergeRecursiveDistinct($shipment, $secondaryShipment);
$consignmentAdapter = new ConsignmentAdapter($secondaryShipment, $this->getConsignmentsByReferenceId($secondaryShipment['reference_identifier']));
$consignmentAdapter = new ConsignmentAdapter($secondaryShipment, $this->getConsignmentsByReferenceId($secondaryShipment['reference_identifier'])->first());
$newCollection->addConsignment($consignmentAdapter->getConsignment()->setMultiCollo($isMultiCollo));

}
Expand Down
5 changes: 5 additions & 0 deletions src/Model/MyParcelConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,14 @@ public function getApiKey()
* @param string $apiKey
*
* @return $this
* @throws \Exception
*/
public function setApiKey($apiKey)
{
if (! is_string($apiKey)) {
throw new \Exception('Api key need to be a type of sting');
}

$this->api_key = $apiKey;

return $this;
Expand Down

0 comments on commit 48e57c0

Please sign in to comment.