You are here

class USPSShipmentBase in Commerce USPS 8

Class that sets the shipment details needed for the USPS request.

@package Drupal\commerce_usps

Hierarchy

Expanded class hierarchy of USPSShipmentBase

File

src/USPSShipmentBase.php, line 16

Namespace

Drupal\commerce_usps
View source
class USPSShipmentBase implements USPSShipmentInterface {

  /**
   * Event disptcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The commerce shipment entity.
   *
   * @var \Drupal\commerce_shipping\Entity\ShipmentInterface
   */
  protected $commerceShipment;

  /**
   * The USPS rate package entity.
   *
   * @var \USPS\RatePackage
   */
  protected $uspsPackage;

  /**
   * The shipping method configuration.
   *
   * @var array
   */
  protected $configuration;

  /**
   * USPSShipmentBase constructor.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(EventDispatcherInterface $event_dispatcher) {
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * Store the shipping method configuration.
   *
   * @param array $configuration
   */
  public function setConfig(array $configuration) {
    $this->configuration = $configuration;
  }

  /**
   * Get the RatePackage object.
   *
   * @param \Drupal\commerce_shipping\Entity\ShipmentInterface $commerce_shipment
   *   The commerce shipment entity.
   *
   * @return \USPS\RatePackage
   *   The RatePackage object.
   */
  public function getPackage(ShipmentInterface $commerce_shipment) {
    $this->commerceShipment = $commerce_shipment;
    $this
      ->buildPackage($commerce_shipment);
    $this
      ->alterPackage();
    return $this->uspsPackage;
  }

  /**
   * Allow rate to be altered.
   */
  public function alterPackage() {

    // Allow other modules to alter the rate request before it's submitted.
    $shipment_event = new USPSShipmentEvent($this->uspsPackage, $this->commerceShipment);
    $this->eventDispatcher
      ->dispatch(USPSEvents::AFTER_BUILD_SHIPMENT, $shipment_event);
  }

  /**
   * Get the USPS RatePackage object.
   *
   * @return \USPS\RatePackage
   *   The RatePackage object.
   */
  public function buildPackage() {
    $this->uspsPackage = new RatePackage();
    return $this->uspsPackage;
  }

  /**
   * Sets the package dimensions.
   */
  public function setDimensions() {
    $package_type = $this
      ->getPackageType();
    if (!empty($package_type)) {
      $length = ceil($package_type
        ->getLength()
        ->convert('in')
        ->getNumber());
      $width = ceil($package_type
        ->getWidth()
        ->convert('in')
        ->getNumber());
      $height = ceil($package_type
        ->getHeight()
        ->convert('in')
        ->getNumber());
      $size = $length > 12 || $width > 12 || $height > 12 ? 'LARGE' : 'REGULAR';
      $this->uspsPackage
        ->setField('Size', $size);
      $this->uspsPackage
        ->setField('Width', $width);
      $this->uspsPackage
        ->setField('Length', $length);
      $this->uspsPackage
        ->setField('Height', $height);
      $this->uspsPackage
        ->setField('Girth', 0);
    }
  }

  /**
   * Sets the package weight.
   */
  protected function setWeight() {
    $weight = $this->commerceShipment
      ->getWeight();
    if ($weight
      ->getNumber() > 0) {
      $ounces = ceil($weight
        ->convert('oz')
        ->getNumber());
      $this->uspsPackage
        ->setPounds(floor($ounces / 16));
      $this->uspsPackage
        ->setOunces($ounces % 16);
    }
  }

  /**
   * Determine the package type for the shipment.
   *
   * @return \Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageTypeInterface
   *   The package type.
   */
  protected function getPackageType() {

    // If the package is set on the shipment, use that.
    if (!empty($this->commerceShipment
      ->getPackageType())) {
      return $this->commerceShipment
        ->getPackageType();
    }

    // TODO return default shipment for shipping method.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
USPSShipmentBase::$commerceShipment protected property The commerce shipment entity.
USPSShipmentBase::$configuration protected property The shipping method configuration.
USPSShipmentBase::$eventDispatcher protected property Event disptcher.
USPSShipmentBase::$uspsPackage protected property The USPS rate package entity.
USPSShipmentBase::alterPackage public function Allow rate to be altered. Overrides USPSShipmentInterface::alterPackage
USPSShipmentBase::buildPackage public function Get the USPS RatePackage object. Overrides USPSShipmentInterface::buildPackage 2
USPSShipmentBase::getPackage public function Get the RatePackage object. Overrides USPSShipmentInterface::getPackage
USPSShipmentBase::getPackageType protected function Determine the package type for the shipment.
USPSShipmentBase::setConfig public function Store the shipping method configuration. Overrides USPSShipmentInterface::setConfig
USPSShipmentBase::setDimensions public function Sets the package dimensions.
USPSShipmentBase::setWeight protected function Sets the package weight.
USPSShipmentBase::__construct public function USPSShipmentBase constructor.