You are here

class ShippingOrderManager in Commerce Shipping 8.2

Hierarchy

Expanded class hierarchy of ShippingOrderManager

1 string reference to 'ShippingOrderManager'
commerce_shipping.services.yml in ./commerce_shipping.services.yml
commerce_shipping.services.yml
1 service uses ShippingOrderManager
commerce_shipping.order_manager in ./commerce_shipping.services.yml
Drupal\commerce_shipping\ShippingOrderManager

File

src/ShippingOrderManager.php, line 10

Namespace

Drupal\commerce_shipping
View source
class ShippingOrderManager implements ShippingOrderManagerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The packer manager.
   *
   * @var \Drupal\commerce_shipping\PackerManagerInterface
   */
  protected $packerManager;

  /**
   * Constructs a new ShippingOrderManager object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info service.
   * @param \Drupal\commerce_shipping\PackerManagerInterface $packer_manager
   *   The packer manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, PackerManagerInterface $packer_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->packerManager = $packer_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function createProfile(OrderInterface $order, array $values = []) {
    $values += [
      'type' => 'customer',
      'uid' => 0,
    ];

    // Check whether the order type has another profile type ID specified.
    $order_type_id = $order
      ->bundle();
    $order_bundle_info = $this->entityTypeBundleInfo
      ->getBundleInfo('commerce_order');
    if (!empty($order_bundle_info[$order_type_id]['shipping_profile_type'])) {
      $values['type'] = $order_bundle_info[$order_type_id]['shipping_profile_type'];
    }

    /** @var \Drupal\profile\ProfileStorageInterface $profile_storage */
    $profile_storage = $this->entityTypeManager
      ->getStorage('profile');
    return $profile_storage
      ->create($values);
  }

  /**
   * {@inheritdoc}
   */
  public function getProfile(OrderInterface $order) {
    $profiles = $order
      ->collectProfiles();
    return isset($profiles['shipping']) ? $profiles['shipping'] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function hasShipments(OrderInterface $order) {
    return $order
      ->hasField('shipments') && !$order
      ->get('shipments')
      ->isEmpty();
  }

  /**
   * {@inheritdoc}
   */
  public function isShippable(OrderInterface $order) {
    if (!$order
      ->hasField('shipments')) {
      return FALSE;
    }

    // The order must contain at least one shippable purchasable entity.
    foreach ($order
      ->getItems() as $order_item) {
      $purchased_entity = $order_item
        ->getPurchasedEntity();
      if ($purchased_entity && $purchased_entity
        ->hasField('weight')) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function pack(OrderInterface $order, ProfileInterface $profile = NULL) {
    if (!$profile) {
      $profile = $this
        ->getProfile($order) ?: $this
        ->createProfile($order);
    }
    $shipments = $order
      ->get('shipments')
      ->referencedEntities();
    list($shipments, $removed_shipments) = $this->packerManager
      ->packToShipments($order, $profile, $shipments);

    // Delete any shipments that are no longer used.
    if (!empty($removed_shipments)) {
      $shipment_storage = $this->entityTypeManager
        ->getStorage('commerce_shipment');
      $shipment_storage
        ->delete($removed_shipments);
    }
    return $shipments;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ShippingOrderManager::$entityTypeBundleInfo protected property The entity type bundle info service.
ShippingOrderManager::$entityTypeManager protected property The entity type manager.
ShippingOrderManager::$packerManager protected property The packer manager.
ShippingOrderManager::createProfile public function Creates a shipping profile for the given order. Overrides ShippingOrderManagerInterface::createProfile
ShippingOrderManager::getProfile public function Gets the shipping profile for the given order. Overrides ShippingOrderManagerInterface::getProfile
ShippingOrderManager::hasShipments public function Checks if the given order has shipments. Overrides ShippingOrderManagerInterface::hasShipments
ShippingOrderManager::isShippable public function Determines whether the order is shippable. Overrides ShippingOrderManagerInterface::isShippable
ShippingOrderManager::pack public function Packs the given order into shipments. Overrides ShippingOrderManagerInterface::pack
ShippingOrderManager::__construct public function Constructs a new ShippingOrderManager object.
ShippingOrderManagerInterface::FORCE_REFRESH constant Data key used to flag an order's shipments for repacking and calculation.