ShipmentManager.php in Commerce Shipping 8.2
File
src/ShipmentManager.php
View source
<?php
namespace Drupal\commerce_shipping;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\Event\ShippingEvents;
use Drupal\commerce_shipping\Event\ShippingRatesEvent;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ShipmentManager implements ShipmentManagerInterface {
protected $entityTypeManager;
protected $entityRepository;
protected $eventDispatcher;
protected $logger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, EventDispatcherInterface $event_dispatcher, LoggerInterface $logger) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
$this->eventDispatcher = $event_dispatcher;
$this->logger = $logger;
}
public function applyRate(ShipmentInterface $shipment, ShippingRate $rate) {
$shipping_method_storage = $this->entityTypeManager
->getStorage('commerce_shipping_method');
$shipping_method = $shipping_method_storage
->load($rate
->getShippingMethodId());
$shipping_method_plugin = $shipping_method
->getPlugin();
if (empty($shipment
->getPackageType())) {
$shipment
->setPackageType($shipping_method_plugin
->getDefaultPackageType());
}
$shipping_method_plugin
->selectRate($shipment, $rate);
}
public function calculateRates(ShipmentInterface $shipment) {
$all_rates = [];
$shipping_method_storage = $this->entityTypeManager
->getStorage('commerce_shipping_method');
$shipping_methods = $shipping_method_storage
->loadMultipleForShipment($shipment);
foreach ($shipping_methods as $shipping_method) {
$shipping_method = $this->entityRepository
->getTranslationFromContext($shipping_method);
$shipping_method_plugin = $shipping_method
->getPlugin();
try {
$rates = $shipping_method_plugin
->calculateRates($shipment);
} catch (\Exception $exception) {
$this->logger
->error('Exception occurred when calculating rates for @name: @message', [
'@name' => $shipping_method
->getName(),
'@message' => $exception
->getMessage(),
]);
continue;
}
$event = new ShippingRatesEvent($rates, $shipping_method, $shipment);
$this->eventDispatcher
->dispatch(ShippingEvents::SHIPPING_RATES, $event);
$rates = $event
->getRates();
$rates = $this
->sortRates($rates);
foreach ($rates as $rate) {
$all_rates[$rate
->getId()] = $rate;
}
}
return $all_rates;
}
public function selectDefaultRate(ShipmentInterface $shipment, array $rates) {
$default_rate = reset($rates);
if ($shipment
->getShippingMethodId() && $shipment
->getShippingService()) {
foreach ($rates as $rate) {
if ($shipment
->getShippingMethodId() != $rate
->getShippingMethodId()) {
continue;
}
if ($shipment
->getShippingService() != $rate
->getService()
->getId()) {
continue;
}
$default_rate = $rate;
break;
}
}
return $default_rate;
}
protected function sortRates(array $rates) {
uasort($rates, function (ShippingRate $first_rate, ShippingRate $second_rate) {
return $first_rate
->getOriginalAmount()
->compareTo($second_rate
->getOriginalAmount());
});
return $rates;
}
}