DangerousGoodsPlugin.php in Commerce FedEx 8
File
modules/dangerous/src/Plugin/Commerce/FedEx/DangerousGoodsPlugin.php
View source
<?php
namespace Drupal\commerce_fedex_dangerous\Plugin\Commerce\FedEx;
use Drupal\commerce_fedex\Plugin\Commerce\FedEx\FedExPluginBase;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\Core\Form\FormStateInterface;
use NicholasCreativeMedia\FedExPHP\Enums\PackageSpecialServiceType;
use NicholasCreativeMedia\FedExPHP\Structs\DangerousGoodsDetail;
use NicholasCreativeMedia\FedExPHP\Structs\PackageSpecialServicesRequested;
use NicholasCreativeMedia\FedExPHP\Structs\RequestedPackageLineItem;
class DangerousGoodsPlugin extends FedExPluginBase {
const NOT_DANGEROUS = 0;
public function defaultConfiguration() {
return [
'contact_number' => '',
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['contact_number'] = [
'#type' => 'textfield',
'#title' => $this
->t("Contact Number"),
'#description' => $this
->t('Enter the Phone number of your dangerous goods/hazardous materials contact person'),
'#default_value' => $this->configuration['contact_number'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['contact_number'] = $form_state
->getValue('contact_number');
}
public function adjustPackage(RequestedPackageLineItem $package, array $shipment_items, ShipmentInterface $shipment) {
$status = $this
->getDangerousStatus(reset($shipment_items));
if ($status === static::NOT_DANGEROUS) {
return $package;
}
$special_services_requested = $package
->getSpecialServicesRequested();
if (empty($special_services_requested)) {
$special_services_requested = new PackageSpecialServicesRequested();
}
$special_services_requested
->addToSpecialServiceTypes(PackageSpecialServiceType::VALUE_DANGEROUS_GOODS);
$dangerous_goods_detail = $special_services_requested
->getDangerousGoodsDetail();
if (empty($dangerous_goods_detail)) {
$dangerous_goods_detail = new DangerousGoodsDetail();
}
$dangerous_goods_detail
->setAccessibility($status);
$special_services_requested
->setDangerousGoodsDetail($dangerous_goods_detail);
$package
->setSpecialServicesRequested($special_services_requested);
return $package;
}
public function splitPackage(array $shipment_items, ShipmentInterface $shipment) {
$packages = [];
foreach ($shipment_items as $shipment_item) {
$packages[$this
->getDangerousStatus($shipment_item)][] = $shipment_item;
}
return array_values($packages);
}
protected function getDangerousStatus(ShipmentItem $shipment_item) {
$storage = \Drupal::entityTypeManager()
->getStorage('commerce_order_item');
$order_item = $storage
->load($shipment_item
->getOrderItemId());
$purchased_entity = $order_item
->getPurchasedEntity();
if (!$purchased_entity
->hasField('fedex_dangerous_accessibility') || $purchased_entity
->get('fedex_dangerous_accessibility')
->isEmpty()) {
return static::NOT_DANGEROUS;
}
return $purchased_entity
->get('fedex_dangerous_accessibility')->value;
}
}