You are here

class USPSShipment in Commerce USPS 8

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

@package Drupal\commerce_usps

Hierarchy

Expanded class hierarchy of USPSShipment

1 file declares its use of USPSShipment
USPSRateRequestTest.php in tests/src/Unit/USPSRateRequestTest.php
1 string reference to 'USPSShipment'
commerce_usps.services.yml in ./commerce_usps.services.yml
commerce_usps.services.yml
1 service uses USPSShipment
commerce_usps.usps_shipment in ./commerce_usps.services.yml
Drupal\commerce_usps\USPSShipment

File

src/USPSShipment.php, line 13

Namespace

Drupal\commerce_usps
View source
class USPSShipment extends USPSShipmentBase implements USPSShipmentInterface {

  /**
   * Returns an initialized rate package object.
   *
   * @return \USPS\RatePackage
   *   The rate package entity.
   */
  public function buildPackage() {
    parent::buildPackage();
    $this
      ->setService();
    $this
      ->setShipFrom();
    $this
      ->setShipTo();
    $this
      ->setWeight();
    $this
      ->setContainer();
    $this
      ->setDimensions();
    $this
      ->setPackageSize();
    $this
      ->setExtraOptions();
    return $this->uspsPackage;
  }

  /**
   * Sets the ship to for a given shipment.
   */
  protected function setShipTo() {

    /** @var \CommerceGuys\Addressing\Address $address */
    $address = $this->commerceShipment
      ->getShippingProfile()
      ->get('address')
      ->first();
    $to_address = new Address();
    $to_address
      ->setAddress($address
      ->getAddressLine1());
    $to_address
      ->setApt($address
      ->getAddressLine2());
    $to_address
      ->setCity($address
      ->getLocality());
    $to_address
      ->setState($address
      ->getAdministrativeArea());

    // Due to API limitations, only accept the first 5 digits of the Zip Code.
    $this->uspsPackage
      ->setZipDestination(substr($address
      ->getPostalCode(), 0, 5));
  }

  /**
   * Sets the ship from for a given shipment.
   */
  protected function setShipFrom() {

    /** @var \CommerceGuys\Addressing\Address $address */
    $address = $this->commerceShipment
      ->getOrder()
      ->getStore()
      ->getAddress();
    $from_address = new Address();
    $from_address
      ->setAddress($address
      ->getAddressLine1());
    $from_address
      ->setCity($address
      ->getLocality());
    $from_address
      ->setState($address
      ->getAdministrativeArea());

    // Due to API limitations, only accept the first 5 digits of the Zip Code.
    $this->uspsPackage
      ->setZipOrigination(substr($address
      ->getPostalCode(), 0, 5));
  }

  /**
   * Sets the package size.
   */
  protected function setPackageSize() {
    $this->uspsPackage
      ->setSize(RatePackage::SIZE_REGULAR);
  }

  /**
   * Sets the services for the shipment.
   */
  protected function setService() {
    $rate_class = RatePackage::SERVICE_ALL;

    // Determine if the shipping method uses a special
    // rate class.
    if (!empty($this->configuration['rate_options']['rate_class'])) {
      switch ($this->configuration['rate_options']['rate_class']) {
        case 'online':
        case 'commercial':
          $rate_class = RatePackage::SERVICE_ONLINE;
          break;
        case 'commercial_plus':
          $rate_class = 'PLUS';
          break;
      }
    }
    $this->uspsPackage
      ->setService($rate_class);
  }

  /**
   * Sets the package container for the shipment.
   */
  protected function setContainer() {
    $this->uspsPackage
      ->setContainer(RatePackage::CONTAINER_VARIABLE);
  }

  /**
   * Sets any extra options specific to the shipment like ship date etc.
   */
  protected function setExtraOptions() {
    $this->uspsPackage
      ->setField('Machinable', TRUE);
    $this->uspsPackage
      ->setField('ShipDate', $this
      ->getProductionDate());
  }

  /**
   * Returns the current date.
   *
   * @return string
   *   The current date, formatted.
   */
  protected function getProductionDate() {
    $date = date('Y-m-d', strtotime("now"));
    return $date;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
USPSShipment::buildPackage public function Returns an initialized rate package object. Overrides USPSShipmentBase::buildPackage
USPSShipment::getProductionDate protected function Returns the current date.
USPSShipment::setContainer protected function Sets the package container for the shipment.
USPSShipment::setExtraOptions protected function Sets any extra options specific to the shipment like ship date etc.
USPSShipment::setPackageSize protected function Sets the package size.
USPSShipment::setService protected function Sets the services for the shipment.
USPSShipment::setShipFrom protected function Sets the ship from for a given shipment.
USPSShipment::setShipTo protected function Sets the ship to for a given shipment.
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::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.