From 47125fe2452501cb2588d7759c965ec9eaebdb6c Mon Sep 17 00:00:00 2001 From: Ameen Ross Date: Wed, 13 May 2015 14:25:43 +0200 Subject: [PATCH] Refactor high-level wrapper, move debugging there as well --- src/BarcodeClient.php | 8 -- src/ConfirmingClient.php | 8 -- .../InvalidBarcodeTypeException.php | 15 ++ src/Postnl.php | 128 +++++++++++++++--- 4 files changed, 124 insertions(+), 35 deletions(-) create mode 100644 src/Exceptions/InvalidBarcodeTypeException.php diff --git a/src/BarcodeClient.php b/src/BarcodeClient.php index 7b27542..306601e 100644 --- a/src/BarcodeClient.php +++ b/src/BarcodeClient.php @@ -67,12 +67,4 @@ public function generateBarcode(ComplexTypes\GenerateBarcodeMessage $GenerateBar { return $this->__soapCall('GenerateBarcode', [$GenerateBarcodeMessage]); } - - public function debug() - { - $requestXml = DOMDocument::loadXML($this->__getLastRequest()); - $requestXml->formatOutput = true; - - return $requestXml->saveXML(); - } } diff --git a/src/ConfirmingClient.php b/src/ConfirmingClient.php index 1c6cc15..f48a3f3 100644 --- a/src/ConfirmingClient.php +++ b/src/ConfirmingClient.php @@ -81,12 +81,4 @@ public function confirming(ComplexTypes\ConfirmingMessage $Confirming) { return $this->__soapCall('Confirming', [$Confirming]); } - - public function debug() - { - $requestXml = DOMDocument::loadXML($this->__getLastRequest()); - $requestXml->formatOutput = true; - - return $requestXml->saveXML(); - } } diff --git a/src/Exceptions/InvalidBarcodeTypeException.php b/src/Exceptions/InvalidBarcodeTypeException.php new file mode 100644 index 0000000..9d2895c --- /dev/null +++ b/src/Exceptions/InvalidBarcodeTypeException.php @@ -0,0 +1,15 @@ +customerNumber = $customerNumber; + $this->customerCode = $customerCode; + $this->customerName = $customerName; $this->securityHeader = new ComplexTypes\SecurityHeader($username, $password); + $this->collectionLocation = $collectionLocation; + $this->globalPackCustomerCode = preg_filter('/^.{2}(.{4})$/', '$1', $globalPack); $this->sandbox = $sandbox; } @@ -41,34 +87,78 @@ public function setBarcodeClient(BarcodeClient $barcodeClient) } /** - * @param string $messageId - * @param string $messageTimestamp + * @param string $type * @param string $customerCode + * Defaults to the customer code used to instantiate this object. * @param string $customerNumber - * @param string $type - * @param string $range + * Defaults to the customer number used to instantiate this object. * @param string $serie + * Defaults to the widest possible range. * * @see BarcodeClient::generateBarcode() */ public function generateBarcode( - $messageId, - $messageTimestamp, - $customerCode, - $customerNumber, $type, - $range, - $serie + $customerCode = null, + $customerNumber = null, + $serie = null ) { - // Instantiate barcode client if not yet set. - $this->barcodeClient = $this->barcodeClient ?: new BarcodeClient($this->securityHeader, $this->sandbox); + // Validate $type parameter. + if (!in_array($type, ['2S', '3S', 'CC', 'CP', 'CD', 'CF'])) { + throw new Exceptions\InvalidBarcodeTypeException($type); + } + + // Default customer code and number. + if (!$customerCode) { + // Use the separate Globalpack customer code for those shipments. + $customerCode = in_array($type, ['2S', '3S']) ? $this->customerCode : $this->globalPackCustomerCode; + } + $customerNumber = $customerNumber ?: $this->customerNumber; + + // Default serie. + if (!$serie) { + switch ($type) { + case '2S': + $serie = '0000000-9999999'; + break; + case '3S': + // 3S barcodes are the only ones that may be 15 characters + // long. + $serie = '000000000-999999999'; + break; + default: + // Globalpack is 4 digits, because the barcode is suffixed + // with the ISO country code. + $serie = '0000-9999'; + } + } // Prepare arguments. - $message = new ComplexTypes\Message($messageId, $messageTimestamp); + $message = new ComplexTypes\Message(1, (new DateTime)->format("d-m-Y H:i:s")); $customer = new ComplexTypes\GenerateBarcodeCustomer($customerCode, $customerNumber); - $barcode = new ComplexTypes\Barcode($type, $range, $serie); + $barcode = new ComplexTypes\Barcode($type, $customerCode, $serie); $generateBarcodeMessage = new ComplexTypes\GenerateBarcodeMessage($message, $customer, $barcode); + // Instantiate barcode client if not yet set. + $this->barcodeClient = $this->barcodeClient ?: new BarcodeClient($this->securityHeader, $this->sandbox); + + // Query the webservice and return the result. return $this->barcodeClient->generateBarcode($generateBarcodeMessage); } + + /** + * Get the raw XML of the last SOAP request and reponse. + * + * @param string $client + * The name of the client (ex. `barcodeClient`). + */ + public function debug($client) + { + $requestXml = DOMDocument::loadXML($this->{$client}->__getLastRequest()); + $requestXml->formatOutput = true; + $responseXml = DOMDocument::loadXML($this->{$client}->__getLastResponse()); + $responseXml->formatOutput = true; + + return ['request' => $requestXml->saveXML(), 'response' => $responseXml->saveXML()]; + } }