/** * @param PackageInterface $package */ public function validateDimensions(PackageInterface $package) { $math = $this->getMath(); $converter = $this->getLengthConverter(); $girthCalculator = $this->getGirthCalculator(); $dimensions = $this->getDimensionsNormalizer()->normalize($package->getDimensions()); $maximumDimension = $converter->convert($this->get('maximum_dimension'), $this->get('dimensions_unit'), $dimensions->getUnit()); if ($math->greaterThan($dimensions->getLength(), $maximumDimension)) { throw new InvalidDimensionsException('Side length limit is exceeded.'); } $girth = $girthCalculator->calculate($dimensions); $maxGirth = $converter->convert($this->get('maximum_girth'), $this->get('dimensions_unit'), $dimensions->getUnit()); if ($math->greaterThan($girth->getValue(), $maxGirth)) { throw new InvalidDimensionsException('Girth limit is exceeded.'); } }
/** * @param PackageInterface $package */ public function validateDimensions(PackageInterface $package) { $math = $this->getMath(); $converter = $this->getLengthConverter(); $dimensions = $package->getDimensions(); $invalidDimensions = $math->lessOrEqualThan($dimensions->getHeight(), 0) || $math->lessOrEqualThan($dimensions->getLength(), 0) || $math->lessOrEqualThan($dimensions->getWidth(), 0); if ($invalidDimensions) { throw new InvalidDimensionsException('Dimensions must be greater than zero.'); } $maxDimensions = $this->getDimensionsNormalizer()->normalize($this->get('maximum_dimensions')); $dimensions = $this->getDimensionsNormalizer()->normalize($package->getDimensions()); $maxLength = $converter->convert($maxDimensions->getLength(), $this->get('dimensions_unit'), $dimensions->getUnit()); if ($math->greaterThan($dimensions->getLength(), $maxLength)) { throw new InvalidDimensionsException('Dimensions limit is exceeded.'); } $maxWidth = $converter->convert($maxDimensions->getWidth(), $this->get('dimensions_unit'), $dimensions->getUnit()); if ($math->greaterThan($dimensions->getWidth(), $maxWidth)) { throw new InvalidDimensionsException('Dimensions limit is exceeded.'); } $maxHeight = $converter->convert($maxDimensions->getHeight(), $this->get('dimensions_unit'), $dimensions->getUnit()); if ($math->greaterThan($dimensions->getHeight(), $maxHeight)) { throw new InvalidDimensionsException('Dimensions 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; }