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\RateAdapter;
use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
@ -15,10 +16,10 @@ class Rate extends RateAdapter
private $urlDev = 'https://gatewaybeta.fedex.com/web-services/';
private $urlProd = 'https://gateway.fedex.com/web-services/';
private $key = 'XXX';
private $password = 'XXX';
private $accountNumber = 'XXX';
private $meterNumber = 'XXX';
private $key;
private $password;
private $accountNumber;
private $meterNumber;
private $dropOffType = 'BUSINESS_SERVICE_CENTER';
public $approvedCodes = [
@ -66,7 +67,26 @@ class Rate extends RateAdapter
$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()
{
$date = time();
@ -103,7 +123,6 @@ class Rate extends RateAdapter
'</Dimensions>' .
'</RequestedPackageLineItems>';
}
$this->data = '<?xml version="1.0"?>' .
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ' .
'xmlns="http://fedex.com/ws/rate/v13">' .

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

@ -5,6 +5,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
@ -13,10 +14,10 @@ class Rate extends RateAdapter
private $urlDev = 'https://wwwcie.ups.com/ups.app/xml/Rate';
private $urlProd = 'https://www.ups.com/ups.app/xml/Rate';
private $accessKey = 'XXX';
private $userId = 'XXX';
private $password = 'XXX';
private $shipperNumber = 'XXX';
private $accessKey;
private $userId;
private $password;
private $shipperNumber;
public $approvedCodes = [
'03',
@ -94,7 +95,26 @@ class Rate extends RateAdapter
$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()
{
$service_code = '03';

@ -6,6 +6,7 @@ use pdt256\Shipping\Arr;
use pdt256\Shipping\Quote;
use pdt256\Shipping\RateAdapter;
use pdt256\Shipping\RateRequest;
use pdt256\Shipping\Validator;
use DOMDocument;
use Exception;
@ -14,8 +15,8 @@ class Rate extends RateAdapter
private $urlDev = 'http://production.shippingapis.com/ShippingAPI.dll';
private $urlProd = 'http://production.shippingapis.com/ShippingAPI.dll';
private $username = 'XXX';
private $password = 'XXX';
private $username;
private $password;
public $approvedCodes = [
'1',
@ -76,7 +77,20 @@ class Rate extends RateAdapter
$this->approvedCodes = Arr::get($options, 'approvedCodes');
$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()
{
$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