You are here

public static function FedEx::packageToFedexDimensions in Commerce FedEx 8

Get FedEx dimensions object for the given package.

Parameters

\Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageTypeInterface $package: The package.

Return value

\NicholasCreativeMedia\FedExPHP\Structs\Dimensions The dimensions.

Throws

\Exception

File

src/Plugin/Commerce/ShippingMethod/FedEx.php, line 1078

Class

FedEx
Provides the FedEx shipping method.

Namespace

Drupal\commerce_fedex\Plugin\Commerce\ShippingMethod

Code

public static function packageToFedexDimensions(PackageTypeInterface $package) {
  $length = $package
    ->getLength();
  $width = $package
    ->getWidth();
  $height = $package
    ->getHeight();
  if ($width
    ->getUnit() != $length
    ->getUnit()) {
    $width = $width
      ->convert($length
      ->getUnit());
  }
  if ($height
    ->getUnit() != $length
    ->getUnit()) {
    $height = $height
      ->convert($length
      ->getUnit());
  }
  switch ($length
    ->getUnit()) {
    case PhysicalLengthUnits::MILLIMETER:
      $length = $length
        ->divide(1000);
      $width = $width
        ->divide(1000);
      $height = $height
        ->divide(1000);
      $unit = FedexLengthUnits::VALUE_CM;
      break;
    case PhysicalLengthUnits::CENTIMETER:
      $unit = FedexLengthUnits::VALUE_CM;
      break;
    case PhysicalLengthUnits::METER:
      $length = $length
        ->multiply(1000);
      $width = $width
        ->multiply(1000);
      $height = $height
        ->multiply(1000);
      $unit = FedexLengthUnits::VALUE_CM;
      break;
    case PhysicalLengthUnits::FOOT:
      $length = $length
        ->multiply(12);
      $width = $width
        ->multiply(12);
      $height = $height
        ->multiply(12);
      $unit = FedexLengthUnits::VALUE_IN;
      break;
    case PhysicalLengthUnits::INCH:
      $unit = FedexLengthUnits::VALUE_IN;
      break;
    default:
      throw new \Exception("Invalid Units for Length");
  }
  return new Dimensions($length
    ->getNumber(), $width
    ->getNumber(), $height
    ->getNumber(), $unit);
}