abstract class USPSRateRequestBase in Commerce USPS 8
Class USPSRateRequest.
@package Drupal\commerce_usps
Hierarchy
- class \Drupal\commerce_usps\USPSRequest implements USPSRequestInterface
- class \Drupal\commerce_usps\USPSRateRequestBase implements USPSRateRequestInterface
Expanded class hierarchy of USPSRateRequestBase
File
- src/
USPSRateRequestBase.php, line 17
Namespace
Drupal\commerce_uspsView source
abstract class USPSRateRequestBase extends USPSRequest implements USPSRateRequestInterface {
/**
* The commerce shipment entity.
*
* @var \Drupal\commerce_shipping\Entity\ShipmentInterface
*/
protected $commerceShipment;
/**
* The shipping method being rated.
*
* @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface
*/
protected $shippingMethod;
/**
* The configuration array from a CommerceShippingMethod.
*
* @var array
*/
protected $configuration;
/**
* The USPS rate request API.
*
* @var \USPS\Rate
*/
protected $uspsRequest;
/**
* The USPS Shipment object.
*
* @var \Drupal\commerce_usps\USPSShipmentInterface
*/
protected $uspsShipment;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* USPSRateRequest constructor.
*
* @param \Drupal\commerce_usps\USPSShipmentInterface $usps_shipment
* The USPS shipment object.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
*/
public function __construct(USPSShipmentInterface $usps_shipment, EventDispatcherInterface $event_dispatcher) {
$this->uspsShipment = $usps_shipment;
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public function setConfig(array $configuration) {
parent::setConfig($configuration);
// Set the configuration on the USPS Shipment service.
$this->uspsShipment
->setConfig($configuration);
}
/**
* Fetch rates from the USPS API.
*
* @param \Drupal\commerce_shipping\Entity\ShipmentInterface $commerce_shipment
* The commerce shipment.
* @param \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method
* The shipping method being rated.
*
* @throws \Exception
* Exception when required properties are missing.
*
* @return array
* An array of ShippingRate objects.
*/
public function getRates(ShipmentInterface $commerce_shipment, ShippingMethodInterface $shipping_method) {
// Validate a commerce shipment has been provided.
if ($commerce_shipment === NULL) {
throw new \Exception('Shipment not provided');
}
// Set the necessary info needed for the request.
$this
->setShipment($commerce_shipment);
$this
->setShippingMethod($shipping_method);
// Build the rate request.
$this
->buildRate();
// Allow others to alter the rate.
$this
->alterRate();
// Fetch the rates.
$this
->logRequest();
$this->uspsRequest
->getRate();
$this
->logResponse();
$response = $this->uspsRequest
->getArrayResponse();
return $this
->resolveRates($response);
}
/**
* Build the rate request.
*/
public function buildRate() {
$this->uspsRequest = new Rate($this->configuration['api_information']['user_id']);
$this
->setMode();
}
/**
* Allow rate to be altered.
*/
public function alterRate() {
// Allow other modules to alter the rate request before it's submitted.
$rateRequestEvent = new USPSRateRequestEvent($this->uspsRequest, $this->commerceShipment);
$this->eventDispatcher
->dispatch(USPSEvents::BEFORE_RATE_REQUEST, $rateRequestEvent);
}
/**
* Set the commerce shipment.
*
* @param \Drupal\commerce_shipping\Entity\ShipmentInterface $commerce_shipment
* The commerce shipment entity.
*/
public function setShipment(ShipmentInterface $commerce_shipment) {
$this->commerceShipment = $commerce_shipment;
}
/**
* Set the shipping method being rated.
*
* @param \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method
* The shipping method.
*/
public function setShippingMethod(ShippingMethodInterface $shipping_method) {
$this->shippingMethod = $shipping_method;
}
/**
* Logs the request data.
*/
public function logRequest() {
if (!empty($this->configuration['options']['log']['request'])) {
$request = $this->uspsRequest
->getPostData();
\Drupal::logger('commerce_usps')
->info('@message', [
'@message' => print_r($request, TRUE),
]);
}
}
/**
* Logs the response data.
*/
public function logResponse() {
if (!empty($this->configuration['options']['log']['response'])) {
\Drupal::logger('commerce_usps')
->info('@message', [
'@message' => print_r($this->uspsRequest
->getResponse(), TRUE),
]);
}
}
/**
* Set the mode to either test/live.
*/
protected function setMode() {
$this->uspsRequest
->setTestMode($this
->isTestMode());
}
/**
* Get an array of USPS packages.
*
* @return array
* An array of USPS packages.
*/
public function getPackages() {
// @todo: Support multiple packages.
return [
$this->uspsShipment
->getPackage($this->commerceShipment),
];
}
/**
* Utility function to clean the USPS service name.
*
* @param string $service
* The service id.
*
* @return string
* The cleaned up service id.
*/
public function cleanServiceName($service) {
// Remove the html encoded trademark markup since it's
// not supported in radio labels.
$service = str_replace('<sup>™</sup>', '', $service);
$service = str_replace('<sup>®</sup>', '', $service);
return $service;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
USPSRateRequestBase:: |
protected | property | The commerce shipment entity. | |
USPSRateRequestBase:: |
protected | property |
The configuration array from a CommerceShippingMethod. Overrides USPSRequest:: |
|
USPSRateRequestBase:: |
protected | property | The event dispatcher. | |
USPSRateRequestBase:: |
protected | property | The shipping method being rated. | |
USPSRateRequestBase:: |
protected | property | The USPS rate request API. | |
USPSRateRequestBase:: |
protected | property | The USPS Shipment object. | |
USPSRateRequestBase:: |
public | function |
Allow rate to be altered. Overrides USPSRateRequestInterface:: |
|
USPSRateRequestBase:: |
public | function |
Build the rate request. Overrides USPSRateRequestInterface:: |
2 |
USPSRateRequestBase:: |
public | function | Utility function to clean the USPS service name. | |
USPSRateRequestBase:: |
public | function | Get an array of USPS packages. | |
USPSRateRequestBase:: |
public | function |
Fetch rates from the USPS API. Overrides USPSRateRequestInterface:: |
|
USPSRateRequestBase:: |
public | function | Logs the request data. | |
USPSRateRequestBase:: |
public | function | Logs the response data. | |
USPSRateRequestBase:: |
public | function |
Set the request configuration. Overrides USPSRequest:: |
|
USPSRateRequestBase:: |
protected | function | Set the mode to either test/live. | |
USPSRateRequestBase:: |
public | function | Set the commerce shipment. | |
USPSRateRequestBase:: |
public | function | Set the shipping method being rated. | |
USPSRateRequestBase:: |
public | function | USPSRateRequest constructor. | |
USPSRateRequestInterface:: |
public | function | Parse the rate response and return shipping rates. | 2 |
USPSRequest:: |
protected | function | Returns authentication array for a request. | |
USPSRequest:: |
protected | function | Determines if the shipping method is in test method.. |