View source
<?php
namespace Drupal\commerce_shipping;
use Drupal\commerce_shipping\Plugin\Commerce\PackageType\PackageType;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\physical\WeightUnit;
class PackageTypeManager extends DefaultPluginManager implements PackageTypeManagerInterface {
protected $defaults = [
'id' => '',
'remote_id' => '',
'label' => '',
'dimensions' => [],
'weight' => NULL,
'shipping_method' => NULL,
'class' => PackageType::class,
];
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->moduleHandler = $module_handler;
$this
->setCacheBackend($cache_backend, 'commerce_package_type_plugins');
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('commerce_package_types', $this->moduleHandler
->getModuleDirectories());
$this->discovery
->addTranslatableProperty('label', 'label_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
$definition['id'] = $plugin_id;
foreach ([
'remote_id',
'label',
'dimensions',
] as $required_property) {
if (empty($definition[$required_property])) {
throw new PluginException(sprintf('The package_type "%s" must define the "%s" property.', $plugin_id, $required_property));
}
}
foreach ([
'length',
'width',
'height',
'unit',
] as $dimension_property) {
if (empty($definition['dimensions'][$dimension_property])) {
throw new PluginException(sprintf('The package type "%s" property "dimensions" must have a "%s" key.', $plugin_id, $dimension_property));
}
}
if (isset($definition['weight'])) {
foreach ([
'number',
'unit',
] as $weight_property) {
if (!isset($definition['weight'][$weight_property])) {
throw new PluginException(sprintf('The package type "%s" property "weight" must have a "%s" key.', $plugin_id, $weight_property));
}
}
}
else {
$definition['weight'] = [
'number' => 0,
'unit' => WeightUnit::GRAM,
];
}
}
public function getDefinitionsByShippingMethod($shipping_method) {
$definitions = $this
->getDefinitions();
foreach ($definitions as $id => $definition) {
if (!empty($definition['shipping_method']) && $definition['shipping_method'] != $shipping_method) {
unset($definitions[$id]);
}
}
uasort($definitions, function ($a, $b) {
return strnatcasecmp($a['label'], $b['label']);
});
return $definitions;
}
}