Skip to content

Commit

Permalink
Refactor high-level wrapper, move debugging there as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ameenross committed May 13, 2015
1 parent 6900fa6 commit 47125fe
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 35 deletions.
8 changes: 0 additions & 8 deletions src/BarcodeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
8 changes: 0 additions & 8 deletions src/ConfirmingClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
15 changes: 15 additions & 0 deletions src/Exceptions/InvalidBarcodeTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace DivideBV\Postnl\Exceptions;

use Exception;

class InvalidBarcodeTypeException extends Exception
{

/**
* @param mixed $type
*/
public function __construct($type)
{
parent::__construct("'{$type}' is not a valid barcode type.");
}
}
128 changes: 109 additions & 19 deletions src/Postnl.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
<?php namespace DivideBV\Postnl;

use DateTime;
use DOMDocument;

/**
* This class is a high-level wrapper around the various CIF services.
*/
class Postnl
{

/**
* @var string $customerNumber
*/
protected $customerNumber = null;

/**
* @var string $customerCode
*/
protected $customerCode = null;

/**
* @var string $customerName
*/
protected $customerName = null;

/**
* @var ComplexTypes\SecurityHeader $securityHeader
*/
protected $securityHeader = null;

/**
* @var string $collectionLocation
*/
protected $collectionLocation = null;

/**
* @var string $globalPackCustomerCode
*/
protected $globalPackCustomerCode = null;

/**
* @var bool $sandbox
*/
Expand All @@ -22,13 +50,31 @@ class Postnl
protected $barcodeClient = null;

/**
* @param string $username
* @param string $password
* @param bool $sandbox
* @param string $customerNumber
* @param string $customerCode
* @param string $customerName
* @param string $username
* @param string $password
* @param string $collectionLocation
* @param string $globalPack
* @param bool $sandbox
*/
public function __construct($username, $password, $sandbox = false)
{
public function __construct(
$customerNumber,
$customerCode,
$customerName,
$username,
$password,
$collectionLocation,
$globalPack,
$sandbox = false
) {
$this->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;
}

Expand All @@ -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()];
}
}

0 comments on commit 47125fe

Please sign in to comment.