Skip to content

Commit ce6d623

Browse files
authored
feat: allow sender with feature header (#517)
* feat: allow sender with feature header INT-781 * feat: allow sender with feature header INT-781 * refactor: remove debug statement * fix: use groupBy correctly eliminating the need for a new method * refactor: make compatible with php7.1 * refactor: make compatible with php7.1 * refactor: add expected type in group callable * fix: implement feedback
1 parent 14a7fc6 commit ce6d623

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

src/Helper/MyParcelCollection.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,24 @@ public function createConcepts(): self
269269
$newConsignments = $this->where('consignment_id', '!=', null)->toArray();
270270
$this->addMissingReferenceId();
271271

272+
$grouped = $this->where('consignment_id', null)->groupBy(function(AbstractConsignment $item) {
273+
return $item->getApiKey() . ($item->hasSender() ? '-sender' : '');
274+
});
275+
272276
/* @var MyParcelCollection $consignments */
273-
foreach ($this->where('consignment_id', null)->groupBy('api_key') as $consignments) {
277+
foreach ($grouped as $consignments) {
278+
$headers = MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT;
279+
if ($consignments->first()->hasSender()) {
280+
$headers += MyParcelRequest::HEADER_SET_CUSTOM_SENDER;
281+
}
282+
274283
$data = (new CollectionEncode($consignments))->encode();
275284
$request = (new MyParcelRequest())
276285
->setUserAgents($this->getUserAgent())
277286
->setRequestParameters(
278287
$consignments->first()->apiKey,
279288
$data,
280-
MyParcelRequest::HEADER_CONTENT_TYPE_SHIPMENT
289+
$headers
281290
)
282291
->sendRequest();
283292

src/Model/Consignment/AbstractConsignment.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier;
1919
use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory;
2020
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
21+
use MyParcelNL\Sdk\src\Model\Recipient;
2122
use MyParcelNL\Sdk\src\Services\CountryCodes;
2223
use MyParcelNL\Sdk\src\Services\CountryService;
2324
use MyParcelNL\Sdk\src\Support\Str;
@@ -522,6 +523,11 @@ abstract class AbstractConsignment
522523
*/
523524
private $state;
524525

526+
/**
527+
* @var null|\MyParcelNL\Sdk\src\Model\Recipient
528+
*/
529+
private $sender;
530+
525531
/**
526532
* @throws \Exception
527533
*/
@@ -532,6 +538,34 @@ public function __construct()
532538
: null;
533539
}
534540

541+
/**
542+
* If you set a sender, a feature header will be added to the request.
543+
* Exporting the consignment to MyParcel will throw an error if your shop (by apikey) does not have this permission.
544+
*
545+
* @param Recipient $sender
546+
* @return $this
547+
*/
548+
public function setSender(Recipient $sender): AbstractConsignment
549+
{
550+
$this->sender = $sender;
551+
552+
return $this;
553+
}
554+
555+
public function hasSender(): bool
556+
{
557+
return isset($this->sender);
558+
}
559+
560+
public function getSender(): ?Recipient
561+
{
562+
if (!$this->hasSender()) {
563+
return null;
564+
}
565+
566+
return $this->sender;
567+
}
568+
535569
/**
536570
* @return null|\MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier
537571
*/

src/Model/MyParcelRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MyParcelRequest
4040
];
4141
public const HEADER_ACCEPT_APPLICATION_PDF = ['Accept' => 'application/pdf'];
4242
public const HEADER_CONTENT_TYPE_RETURN_SHIPMENT = ['Content-Type' => 'application/vnd.return_shipment+json; charset=utf-8'];
43+
public const HEADER_SET_CUSTOM_SENDER = ['x-dmp-set-custom-sender' => 'true'];
4344

4445
/* @deprecated use HEADER_CONTENT_TYPE_SHIPMENT, HEADER_ACCEPT_APPLICATION_PDF or HEADER_CONTENT_TYPE_RETURN_SHIPMENT */
4546
public const REQUEST_HEADER_SHIPMENT = 'Content-Type: application/vnd.shipment+json;charset=utf-8;version=1.1';

src/Services/CollectionEncode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ private function groupMultiColloConsignments()
5555
{
5656
return $this->consignments->groupBy(function (AbstractConsignment $consignment) {
5757
if ($consignment->isPartOfMultiCollo()) {
58-
return $consignment->getReferenceId();
58+
return $consignment->getReferenceIdentifier();
5959
}
6060

61-
return 'random_to_prevent_multi_collo_' . uniqid();
61+
return 'random_to_prevent_multi_collo_' . uniqid('', true);
6262
})->toArray();
6363
}
6464
}

src/Services/ConsignmentEncode.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,17 @@ private function encodeBase(): self
205205
'carrier' => $consignment->getCarrierId(),
206206
];
207207

208-
if ($consignment->getReferenceId()) {
209-
$this->consignmentEncoded['reference_identifier'] = $consignment->getReferenceId();
208+
209+
if (($sender = $consignment->getSender())) {
210+
$this->consignmentEncoded['sender'] = $sender->toArrayWithoutNull();
211+
}
212+
213+
if (($id = $consignment->getReferenceIdentifier())) {
214+
$this->consignmentEncoded['reference_identifier'] = $id;
210215
}
211216

212-
if ($consignment->getCompany()) {
213-
$this->consignmentEncoded['recipient']['company'] = $consignment->getCompany();
217+
if (($company = $consignment->getCompany())) {
218+
$this->consignmentEncoded['recipient']['company'] = $company;
214219
}
215220

216221
return $this;

0 commit comments

Comments
 (0)