Added validation that all necessary properties are set

pull/9/head
Pereyaslov Konstantin 10 years ago
parent 5dafdf17a0
commit 10f8bde7a2

@ -7,6 +7,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote; use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
@ -15,10 +16,10 @@ class Rate extends RateAdapter
private $urlDev = 'https://gatewaybeta.fedex.com/web-services/'; private $urlDev = 'https://gatewaybeta.fedex.com/web-services/';
private $urlProd = 'https://gateway.fedex.com/web-services/'; private $urlProd = 'https://gateway.fedex.com/web-services/';
private $key = 'XXX'; private $key;
private $password = 'XXX'; private $password;
private $accountNumber = 'XXX'; private $accountNumber;
private $meterNumber = 'XXX'; private $meterNumber;
private $dropOffType = 'BUSINESS_SERVICE_CENTER'; private $dropOffType = 'BUSINESS_SERVICE_CENTER';
public $approvedCodes = [ public $approvedCodes = [
@ -66,7 +67,26 @@ class Rate extends RateAdapter
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post()));
} }
protected function validate()
{
foreach ($this->shipment->getPackages() as $package) {
Validator::checkIfNull($package->getWeight(), 'weight');
Validator::checkIfNull($package->getLength(), 'length');
Validator::checkIfNull($package->getHeight(), 'height');
}
Validator::checkIfNull($this->key, 'key');
Validator::checkIfNull($this->password, 'password');
Validator::checkIfNull($this->accountNumber, 'accountNumber');
Validator::checkIfNull($this->meterNumber, 'meterNumber');
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
Validator::checkIfNull($this->shipment->getFromCountryCode(), 'fromCountryCode');
Validator::checkIfNull($this->shipment->getFromIsResidential(), 'fromIsResidential');
Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode');
Validator::checkIfNull($this->shipment->getToCountryCode(), 'toCountryCode');
Validator::checkIfNull($this->shipment->getToIsResidential(), 'toIsResidential');
return $this;
}
protected function prepare() protected function prepare()
{ {
$date = time(); $date = time();
@ -103,7 +123,6 @@ class Rate extends RateAdapter
'</Dimensions>' . '</Dimensions>' .
'</RequestedPackageLineItems>'; '</RequestedPackageLineItems>';
} }
$this->data = '<?xml version="1.0"?>' . $this->data = '<?xml version="1.0"?>' .
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' .
'xmlns="http://fedex.com/ws/rate/v13">' . 'xmlns="http://fedex.com/ws/rate/v13">' .

@ -16,6 +16,10 @@ abstract class RateAdapter
/** @var @var RateRequest\Adapter */ /** @var @var RateRequest\Adapter */
protected $rateRequest; protected $rateRequest;
/**
* Make sure all necessary fields are set
*/
abstract protected function validate();
/** /**
* Prepare XML * Prepare XML
*/ */
@ -47,6 +51,7 @@ abstract class RateAdapter
public function getRates() public function getRates()
{ {
$this $this
->validate()
->prepare() ->prepare()
->execute() ->execute()
->process() ->process()

@ -5,6 +5,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote; use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
@ -13,10 +14,10 @@ class Rate extends RateAdapter
private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate'; private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate';
private $urlProd = 'https://www.ups.com/ups.app/xml/Rate'; private $urlProd = 'https://www.ups.com/ups.app/xml/Rate';
private $accessKey = 'XXX'; private $accessKey;
private $userId = 'XXX'; private $userId;
private $password = 'XXX'; private $password;
private $shipperNumber = 'XXX'; private $shipperNumber;
public $approvedCodes = [ public $approvedCodes = [
'03', '03',
@ -94,7 +95,26 @@ class Rate extends RateAdapter
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Post()));
} }
protected function validate()
{
foreach ($this->shipment->getPackages() as $package) {
Validator::checkIfNull($package->getWeight(), 'weight');
Validator::checkIfNull($package->getLength(), 'length');
Validator::checkIfNull($package->getHeight(), 'height');
}
Validator::checkIfNull($this->accessKey, 'accessKey');
Validator::checkIfNull($this->userId, 'userId');
Validator::checkIfNull($this->password, 'password');
Validator::checkIfNull($this->shipperNumber, 'shipperNumber');
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
Validator::checkIfNull($this->shipment->getFromCountryCode(), 'fromCountryCode');
Validator::checkIfNull($this->shipment->getFromIsResidential(), 'fromIsResidential');
Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode');
Validator::checkIfNull($this->shipment->getToCountryCode(), 'toCountryCode');
Validator::checkIfNull($this->shipment->getToIsResidential(), 'toIsResidential');
return $this;
}
protected function prepare() protected function prepare()
{ {
$service_code = '03'; $service_code = '03';

@ -6,6 +6,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote; use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter; use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest; use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument; use DOMDocument;
use Exception; use Exception;
@ -14,8 +15,8 @@ class Rate extends RateAdapter
private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll'; private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll';
private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll'; private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll';
private $username = 'XXX'; private $username;
private $password = 'XXX'; private $password;
public $approvedCodes = [ public $approvedCodes = [
'1', '1',
@ -76,7 +77,20 @@ class Rate extends RateAdapter
$this->approvedCodes = Arr::get($options, 'approvedCodes'); $this->approvedCodes = Arr::get($options, 'approvedCodes');
$this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get())); $this->setRequestAdapter(Arr::get($options, 'requestAdapter', new RateRequest\Get()));
} }
protected function validate()
{
foreach ($this->shipment->getPackages() as $package) {
Validator::checkIfNull($package->getWeight(), 'weight');
Validator::checkIfNull($package->getLength(), 'length');
Validator::checkIfNull($package->getHeight(), 'height');
}
Validator::checkIfNull($this->username, 'username');
Validator::checkIfNull($this->password, 'password');
Validator::checkIfNull($this->shipment->getFromPostalCode(), 'fromPostalCode');
Validator::checkIfNull($this->shipment->getToPostalCode(), 'toPostalCode');
return $this;
}
protected function prepare() protected function prepare()
{ {
$packages = ''; $packages = '';

@ -0,0 +1,10 @@
<?php
namespace pdt256\Shipping;
class Validator {
public static function checkIfNull($value, $name) {
if ($value === null) {
throw new \LogicException("$name is not set");
}
}
}
Loading…
Cancel
Save