You are here

abstract class ShippingMethodBase in Commerce Shipping 8.2

Provides the base class for shipping methods.

Hierarchy

Expanded class hierarchy of ShippingMethodBase

2 files declare their use of ShippingMethodBase
ExceptionThrower.php in tests/modules/commerce_shipping_test/src/Plugin/Commerce/ShippingMethod/ExceptionThrower.php
Test.php in tests/modules/commerce_shipping_test/src/Plugin/Commerce/ShippingMethod/Test.php

File

src/Plugin/Commerce/ShippingMethod/ShippingMethodBase.php, line 20

Namespace

Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod
View source
abstract class ShippingMethodBase extends PluginBase implements ContainerFactoryPluginInterface, ShippingMethodInterface {

  /**
   * The package type manager.
   *
   * @var \Drupal\commerce_shipping\PackageTypeManagerInterface
   */
  protected $packageTypeManager;

  /**
   * The workflow manager.
   *
   * @var \Drupal\state_machine\WorkflowManagerInterface
   */
  protected $workflowManager;

  /**
   * The shipping services.
   *
   * @var \Drupal\commerce_shipping\ShippingService[]
   */
  protected $services = [];

  /**
   * The parent config entity.
   *
   * Not available while the plugin is being configured.
   *
   * @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface
   */
  protected $parentEntity;

  /**
   * Constructs a new ShippingMethodBase 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.
   * @param \Drupal\state_machine\WorkflowManagerInterface $workflow_manager
   *   The workflow manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, PackageTypeManagerInterface $package_type_manager, WorkflowManagerInterface $workflow_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->packageTypeManager = $package_type_manager;
    $this->workflowManager = $workflow_manager;
    foreach ($this->pluginDefinition['services'] as $id => $label) {
      $this->services[$id] = new ShippingService($id, (string) $label);
    }
    $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'), $container
      ->get('plugin.manager.workflow'));
  }

  /**
   * {@inheritdoc}
   */
  public function setParentEntity(EntityInterface $parent_entity) {
    $this->parentEntity = $parent_entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return (string) $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultPackageType() {
    $package_type_id = $this->configuration['default_package_type'];
    return $this->packageTypeManager
      ->createInstance($package_type_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getServices() {

    // Filter out shipping services disabled by the merchant.
    return array_intersect_key($this->services, array_flip($this->configuration['services']));
  }

  /**
   * {@inheritdoc}
   */
  public function getWorkflowId() {
    return $this->configuration['workflow'];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'default_package_type' => 'custom_box',
      'services' => [],
      'workflow' => $this->pluginDefinition['workflow'],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $package_types = $this->packageTypeManager
      ->getDefinitionsByShippingMethod($this->pluginId);
    $package_types = array_map(function ($package_type) {
      return $package_type['label'];
    }, $package_types);
    $services = array_map(function ($service) {
      return $service
        ->getLabel();
    }, $this->services);

    // Select all services by default.
    if (empty($this->configuration['services'])) {
      $service_ids = array_keys($services);
      $this->configuration['services'] = array_combine($service_ids, $service_ids);
    }
    $workflows = $this->workflowManager
      ->getGroupedLabels('commerce_shipment');
    $workflows = reset($workflows);
    $form['default_package_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default package type'),
      '#options' => $package_types,
      '#default_value' => $this->configuration['default_package_type'],
      '#required' => TRUE,
      '#access' => count($package_types) > 1,
    ];
    $form['services'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Shipping services'),
      '#options' => $services,
      '#default_value' => $this->configuration['services'],
      '#required' => TRUE,
      '#access' => count($services) > 1,
    ];
    $form['workflow'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Shipment workflow'),
      '#options' => $workflows,
      '#default_value' => $this->configuration['workflow'],
      '#required' => TRUE,
      '#access' => count($workflows) > 1,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue($form['#parents']);

    /** @var \Drupal\state_machine\Plugin\Workflow\WorkflowInterface $workflow */
    $workflow = $this->workflowManager
      ->createInstance($values['workflow']);

    // Verify "Finalize" transition.
    if (!$workflow
      ->getTransition('finalize')) {
      $form_state
        ->setError($form['workflow'], $this
        ->t('The @workflow workflow does not have a "Finalize" transition.', [
        '@workflow' => $workflow
          ->getLabel(),
      ]));
    }

    // Verify "Cancel" transition.
    if (!$workflow
      ->getTransition('cancel')) {
      $form_state
        ->setError($form['workflow'], $this
        ->t('The @workflow workflow does not have a "Cancel" transition.', [
        '@workflow' => $workflow
          ->getLabel(),
      ]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getErrors()) {
      $values = $form_state
        ->getValue($form['#parents']);
      if (!empty($values['services'])) {
        $values['services'] = array_filter($values['services']);
        $this->configuration['default_package_type'] = $values['default_package_type'];
        $this->configuration['services'] = array_keys($values['services']);
      }
      $this->configuration['workflow'] = $values['workflow'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function selectRate(ShipmentInterface $shipment, ShippingRate $rate) {

    // Plugins can override this method to store additional information
    // on the shipment when the rate is selected (for example, the rate ID).
    $shipment
      ->setShippingMethodId($rate
      ->getShippingMethodId());
    $shipment
      ->setShippingService($rate
      ->getService()
      ->getId());
    $shipment
      ->setOriginalAmount($rate
      ->getOriginalAmount());
    $shipment
      ->setAmount($rate
      ->getAmount());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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.
ShippingMethodBase::$packageTypeManager protected property The package type manager.
ShippingMethodBase::$parentEntity protected property The parent config entity.
ShippingMethodBase::$services protected property The shipping services.
ShippingMethodBase::$workflowManager protected property The workflow manager.
ShippingMethodBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 1
ShippingMethodBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ShippingMethodBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ShippingMethodBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
ShippingMethodBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ShippingMethodBase::getDefaultPackageType public function Gets the default package type. Overrides ShippingMethodInterface::getDefaultPackageType
ShippingMethodBase::getLabel public function Gets the shipping method label. Overrides ShippingMethodInterface::getLabel
ShippingMethodBase::getServices public function Gets the shipping services. Overrides ShippingMethodInterface::getServices
ShippingMethodBase::getWorkflowId public function Gets the shipment workflow ID. Overrides ShippingMethodInterface::getWorkflowId
ShippingMethodBase::selectRate public function Selects the given shipping rate for the given shipment. Overrides ShippingMethodInterface::selectRate
ShippingMethodBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ShippingMethodBase::setParentEntity public function Sets the parent entity. Overrides ParentEntityAwareInterface::setParentEntity
ShippingMethodBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
ShippingMethodBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
ShippingMethodBase::__construct public function Constructs a new ShippingMethodBase object. Overrides PluginBase::__construct 1
ShippingMethodInterface::calculateRates public function Calculates rates for the given shipment. 3
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.