You are here

abstract class USPSRateRequestBase in Commerce USPS 8

Class USPSRateRequest.

@package Drupal\commerce_usps

Hierarchy

Expanded class hierarchy of USPSRateRequestBase

File

src/USPSRateRequestBase.php, line 17

Namespace

Drupal\commerce_usps
View 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

Namesort descending Modifiers Type Description Overrides
USPSRateRequestBase::$commerceShipment protected property The commerce shipment entity.
USPSRateRequestBase::$configuration protected property The configuration array from a CommerceShippingMethod. Overrides USPSRequest::$configuration
USPSRateRequestBase::$eventDispatcher protected property The event dispatcher.
USPSRateRequestBase::$shippingMethod protected property The shipping method being rated.
USPSRateRequestBase::$uspsRequest protected property The USPS rate request API.
USPSRateRequestBase::$uspsShipment protected property The USPS Shipment object.
USPSRateRequestBase::alterRate public function Allow rate to be altered. Overrides USPSRateRequestInterface::alterRate
USPSRateRequestBase::buildRate public function Build the rate request. Overrides USPSRateRequestInterface::buildRate 2
USPSRateRequestBase::cleanServiceName public function Utility function to clean the USPS service name.
USPSRateRequestBase::getPackages public function Get an array of USPS packages.
USPSRateRequestBase::getRates public function Fetch rates from the USPS API. Overrides USPSRateRequestInterface::getRates
USPSRateRequestBase::logRequest public function Logs the request data.
USPSRateRequestBase::logResponse public function Logs the response data.
USPSRateRequestBase::setConfig public function Set the request configuration. Overrides USPSRequest::setConfig
USPSRateRequestBase::setMode protected function Set the mode to either test/live.
USPSRateRequestBase::setShipment public function Set the commerce shipment.
USPSRateRequestBase::setShippingMethod public function Set the shipping method being rated.
USPSRateRequestBase::__construct public function USPSRateRequest constructor.
USPSRateRequestInterface::resolveRates public function Parse the rate response and return shipping rates. 2
USPSRequest::getAuth protected function Returns authentication array for a request.
USPSRequest::isTestMode protected function Determines if the shipping method is in test method..