You are here

class DryIcePlugin in Commerce FedEx 8

Provides the Fedex Dry Ice Service.

Plugin annotation


@CommerceFedExPlugin(
  id = "dry_ice",
  label = @Translation("FedEx Dry Ice"),
  options_label = @Translation("Dry Ice Shipment Options"),
  options_description = @Translation("Enter your global shipping options for dry ice shipments")
)

Hierarchy

Expanded class hierarchy of DryIcePlugin

File

modules/dry_ice/src/Plugin/Commerce/FedEx/DryIcePlugin.php, line 29

Namespace

Drupal\commerce_fedex_dry_ice\Plugin\Commerce\FedEx
View source
class DryIcePlugin extends FedExPluginBase {
  const NOT_DRY_ICE = 0;
  const DRY_ICE = 1;

  /**
   * The Package Type Manager.
   *
   * @var \Drupal\commerce_shipping\PackageTypeManagerInterface
   */
  protected $packageTypeManager;

  /**
   * Creates a new Dry Ice FedEx Plugin object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\commerce_shipping\PackageTypeManagerInterface $package_type_manager
   *   The package type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, PackageTypeManagerInterface $package_type_manager) {
    $this->packageTypeManager = $package_type_manager;
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this
      ->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.commerce_package_type'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $config = [
      'domestic' => [
        'package_type' => 'custom_box',
        'weight' => [
          'number' => 0,
          'unit' => WeightUnit::KILOGRAM,
        ],
      ],
      'intl' => [
        'package_type' => 'custom_box',
        'weight' => [
          'number' => 0,
          'unit' => WeightUnit::KILOGRAM,
        ],
      ],
    ] + parent::defaultConfiguration();
    return $config;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $package_types = $this->packageTypeManager
      ->getDefinitionsByShippingMethod('fedex');
    $package_types = array_map(function ($package_type) {
      return $package_type['label'];
    }, $package_types);
    $form['domestic']['package_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Dry ice package type'),
      '#options' => $package_types,
      '#default_value' => $this->configuration['domestic']['package_type'],
      '#required' => TRUE,
      '#access' => count($package_types) > 1,
    ];
    $form['domestic']['weight'] = [
      '#type' => 'physical_measurement',
      '#measurement_type' => 'weight',
      '#title' => $this
        ->t('Dry Ice Weight'),
      '#default_value' => $this->configuration['domestic']['weight'],
      '#size' => 5,
      '#maxlength' => 4,
      '#required' => TRUE,
    ];
    $form['intl']['package_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('International dry ice package type'),
      '#options' => $package_types,
      '#default_value' => $this->configuration['intl']['package_type'],
      '#required' => TRUE,
      '#access' => count($package_types) > 1,
    ];
    $form['intl']['weight'] = [
      '#type' => 'physical_measurement',
      '#measurement_type' => 'weight',
      '#title' => $this
        ->t('International dry Ice Weight'),
      '#default_value' => $this->configuration['intl']['weight'],
      '#size' => 5,
      '#maxlength' => 4,
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getErrors()) {
      $values = $form_state
        ->getValues();
      $this->configuration['domestic']['package_type'] = $values['domestic']['package_type'];
      $this->configuration['domestic']['weight'] = $values['domestic']['weight'];
      $this->configuration['intl']['package_type'] = $values['intl']['package_type'];
      $this->configuration['intl']['weight'] = $values['intl']['weight'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function adjustPackage(RequestedPackageLineItem $package, array $shipment_items, ShipmentInterface $shipment) {
    $type = $this
      ->getType($shipment);
    if (!$this
      ->verifyPackage($shipment_items, $type)) {
      throw new \Exception("Package cannot be shipped, mix of Dry Ice and non Dry Ice Items");
    }
    if ($this
      ->isDryIceItem(reset($shipment_items), $type)) {
      $special_services_requested = $package
        ->getSpecialServicesRequested();
      if (empty($special_services_requested)) {
        $special_services_requested = new PackageSpecialServicesRequested();
      }
      $dry_ice_weight = new Weight($this->configuration[$type]['weight']['number'], $this->configuration[$type]['weight']['unit']);
      $special_services_requested
        ->addToSpecialServiceTypes(PackageSpecialServiceType::VALUE_DRY_ICE);
      $special_services_requested
        ->setDryIceWeight(FedEx::physicalWeightToFedex($dry_ice_weight));

      /** @var \Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageType $package_type */
      $package_type = $this->packageTypeManager
        ->createInstance($this->configuration[$type]['package_type']);
      $package
        ->setDimensions(Fedex::packageToFedexDimensions($package_type));
      $package
        ->setWeight(FedEx::packageTotalWeight($shipment_items, $package_type, $dry_ice_weight));
      $package
        ->setSpecialServicesRequested($special_services_requested);
    }
    $package = parent::adjustPackage($package, $shipment_items, $shipment);
    return $package;
  }

  /**
   * {@inheritdoc}
   */
  public function splitPackage(array $shipment_items, ShipmentInterface $shipment) {
    $type = $this
      ->getType($shipment);
    $packages = [
      static::NOT_DRY_ICE => [],
      static::DRY_ICE => [],
    ];
    foreach ($shipment_items as $shipment_item) {
      if ($this
        ->isDryIceItem($shipment_item, $type)) {
        $packages[static::DRY_ICE][] = $shipment_item;
      }
      else {
        $packages[static::NOT_DRY_ICE][] = $shipment_item;
      }
    }
    return $packages;
  }

  /**
   * Verified a package has all dry ic eor non-dry ice items.
   *
   * @param array $shipment_items
   *   The shipment items to check.
   * @param string $type
   *   The type, 'domestic' or 'intl' depending on the shipping distance.
   *
   * @throws \Exception
   *   If the provided $type is not of type 'string'.
   *
   * @return bool
   *   True if the package is internally consistant.
   */
  protected function verifyPackage(array $shipment_items, $type) {
    if (!is_string($type)) {
      $varType = gettype($type);
      throw new \Exception("Shipment type must be string. {$varType} given.");
    }
    $dryIceBox = $this
      ->isDryIceBox($shipment_items, $type);
    foreach ($shipment_items as $shipment_item) {
      if ($dryIceBox xor $this
        ->isDryIceItem($shipment_item, $type)) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Determine whether a droup of shipment items requires dry ice shipping.
   *
   * @param array $shipment_items
   *   The shipment items to check.
   * @param string $type
   *   The type, 'domestic' or 'intl' depending on the shipping distance.
   *
   * @throws \Exception
   *   If the provided $type is not of type 'string'.
   *
   * @return bool
   *   True if the package needs dry ice shipping.
   */
  protected function isDryIceBox(array $shipment_items, $type) {
    if (!is_string($type)) {
      $varType = gettype($type);
      throw new \Exception("Shipment type must be string. {$varType} given.");
    }
    return $this
      ->isDryIceItem(reset($shipment_items), $type);
  }

  /**
   * Determine whether a shipment item requires dry ice shipping or not.
   *
   * @param \Drupal\commerce_shipping\ShipmentItem $shipment_item
   *   The shipment item.
   * @param string $type
   *   The type, 'domestic' or 'intl' depending on the shipping distance.
   *
   * @throws \Exception
   *   If the provided $type is not of type 'string'.
   *
   * @return bool
   *   true if the shipment item requires dry ice shipping.
   */
  protected function isDryIceItem(ShipmentItem $shipment_item, $type) {
    if (!is_string($type)) {
      $varType = gettype($type);
      throw new \Exception("Shipment type must be string. {$varType} given.");
    }
    $storage = \Drupal::entityTypeManager()
      ->getStorage('commerce_order_item');

    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $storage
      ->load($shipment_item
      ->getOrderItemId());
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    return $purchased_entity
      ->hasField('fedex_dry_ice_' . $type) && !$purchased_entity
      ->get('fedex_dry_ice_' . $type)
      ->isEmpty() && $purchased_entity
      ->get('fedex_dry_ice_' . $type)
      ->first()
      ->getValue()['value'] == 1;
  }

  /**
   * Determine if this is a domestic or international shipment.
   *
   * @param \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment
   *   The shipment to check.
   *
   * @return string
   *   either 'domestic' or 'intl'
   */
  protected function getType(ShipmentInterface $shipment) {

    /* @var  AddressItem $shipping_address */
    $shipping_address = $shipment
      ->getShippingProfile()
      ->get('address')
      ->first();
    $domestic = $shipping_address instanceof AddressItem ? $shipment
      ->getOrder()
      ->getStore()
      ->getAddress()
      ->getCountryCode() == $shipping_address
      ->getCountryCode() : FALSE;
    return $domestic ? 'domestic' : 'intl';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DryIcePlugin::$packageTypeManager protected property The Package Type Manager.
DryIcePlugin::adjustPackage public function Adjust a package based on the items, shipment and profile. Overrides FedExPluginBase::adjustPackage
DryIcePlugin::buildConfigurationForm public function Form constructor. Overrides FedExPluginBase::buildConfigurationForm
DryIcePlugin::create public static function Creates an instance of the plugin. Overrides FedExPluginBase::create
DryIcePlugin::defaultConfiguration public function Provide the plugin default configuration. Overrides FedExPluginBase::defaultConfiguration
DryIcePlugin::DRY_ICE constant
DryIcePlugin::getType protected function Determine if this is a domestic or international shipment.
DryIcePlugin::isDryIceBox protected function Determine whether a droup of shipment items requires dry ice shipping.
DryIcePlugin::isDryIceItem protected function Determine whether a shipment item requires dry ice shipping or not.
DryIcePlugin::NOT_DRY_ICE constant
DryIcePlugin::splitPackage public function Function splitPackage. Overrides FedExPluginBase::splitPackage
DryIcePlugin::submitConfigurationForm public function Form submission handler. Overrides FedExPluginBase::submitConfigurationForm
DryIcePlugin::verifyPackage protected function Verified a package has all dry ic eor non-dry ice items.
DryIcePlugin::__construct public function Creates a new Dry Ice FedEx Plugin object. Overrides FedExPluginBase::__construct
FedExPluginBase::calculateDependencies public function
FedExPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FedExPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FedExPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.