class PackerManager in Commerce Shipping 8.2
Hierarchy
- class \Drupal\commerce_shipping\PackerManager implements PackerManagerInterface
Expanded class hierarchy of PackerManager
1 file declares its use of PackerManager
- PackerManagerTest.php in tests/
src/ Kernel/ PackerManagerTest.php
1 string reference to 'PackerManager'
1 service uses PackerManager
File
- src/
PackerManager.php, line 10
Namespace
Drupal\commerce_shippingView source
class PackerManager implements PackerManagerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The packers.
*
* @var \Drupal\commerce_shipping\Packer\PackerInterface[]
*/
protected $packers = [];
/**
* Constructs a new PackerManager object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function addPacker(PackerInterface $packer) {
$this->packers[] = $packer;
}
/**
* {@inheritdoc}
*/
public function getPackers() {
return $this->packers;
}
/**
* {@inheritdoc}
*/
public function pack(OrderInterface $order, ProfileInterface $shipping_profile) {
$proposed_shipments = [];
foreach ($this->packers as $packer) {
if ($packer
->applies($order, $shipping_profile)) {
$proposed_shipments = $packer
->pack($order, $shipping_profile);
if (!is_null($proposed_shipments)) {
break;
}
}
}
return $proposed_shipments;
}
/**
* {@inheritdoc}
*/
public function packToShipments(OrderInterface $order, ProfileInterface $shipping_profile, array $shipments) {
$shipment_storage = $this->entityTypeManager
->getStorage('commerce_shipment');
$proposed_shipments = $this
->pack($order, $shipping_profile);
$populated_shipments = [];
foreach ($proposed_shipments as $index => $proposed_shipment) {
$shipment = NULL;
// Take the first existing shipment of the matching type.
foreach ($shipments as $existing_index => $existing_shipment) {
if ($existing_shipment
->bundle() == $proposed_shipment
->getType()) {
$shipment = $existing_shipment;
unset($shipments[$existing_index]);
break;
}
}
if (!$shipment) {
$shipment = $shipment_storage
->create([
'type' => $proposed_shipment
->getType(),
]);
}
$shipment
->populateFromProposedShipment($proposed_shipment);
$shipment
->setData('owned_by_packer', TRUE);
$populated_shipments[$index] = $shipment;
}
$removed_shipments = array_filter($shipments, function ($shipment) {
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
return !$shipment
->isNew();
});
return [
$populated_shipments,
(array) $removed_shipments,
];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PackerManager:: |
protected | property | The entity type manager. | |
PackerManager:: |
protected | property | The packers. | |
PackerManager:: |
public | function |
Adds a packer. Overrides PackerManagerInterface:: |
|
PackerManager:: |
public | function |
Gets all added packers. Overrides PackerManagerInterface:: |
|
PackerManager:: |
public | function |
Packs the given order. Overrides PackerManagerInterface:: |
|
PackerManager:: |
public | function |
Packs the given order and populates the given shipments. Overrides PackerManagerInterface:: |
|
PackerManager:: |
public | function | Constructs a new PackerManager object. |