public function validateWeight(PackageInterface $package) { $math = $this->getMath(); $converter = $this->getWeightConverter(); $maxWeight = $converter->convert($this->get('maximum_weight'), $this->get('mass_unit'), $package->getWeight()->getUnit()); if ($math->greaterThan($package->getWeight()->getValue(), $maxWeight)) { throw new InvalidWeightException('Sender country weight limit is exceeded.'); } }
public function getPrice(PackageInterface $package) { $weight = $package->getWeight(); $volumetricWeight = $this->getVolumetricWeightCalculator()->calculate($package->getDimensions()); $weight = $this->getWeightConverter()->convert($weight->getValue(), $weight->getUnit(), $this->get('mass_unit')); $volumetricWeight = $this->getWeightConverter()->convert($volumetricWeight->getValue(), $volumetricWeight->getUnit(), $this->get('mass_unit')); $math = $this->getMath(); if ($math->greaterThan($volumetricWeight, $weight)) { $weight = $volumetricWeight; } $importCountry = $this->detectImportCountry($package->getRecipientAddress()); $zone = $this->get('zones')[$importCountry->getZone()]; $currentWeight = null; $price = null; foreach ($zone['weight_prices'] as $w => $p) { if ($math->lessOrEqualThan($weight, $w) && $math->greaterThan($weight, $currentWeight)) { $currentWeight = $w; $price = $p; } } if (is_null($price)) { throw new InvalidWeightException('Can not calculate shipping for this weight.'); } return $price; }