Пример #1
0
 public function getPrice(Package $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 ViolationException('Can not calculate shipping for this weight.');
     }
     return $price;
 }
Пример #2
0
 /**
  * @param Package $package
  */
 public function validateDimensions(Package $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 ViolationException('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 ViolationException('Girth limit is exceeded.');
     }
 }
Пример #3
0
 /**
  * @param Package $package
  */
 public function validateDimensions(Package $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 ViolationException('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 ViolationException('Dimensions limit is exceeded.');
     }
     $maxWidth = $converter->convert($maxDimensions->getWidth(), $this->get('dimensions_unit'), $dimensions->getUnit());
     if ($math->greaterThan($dimensions->getWidth(), $maxWidth)) {
         throw new ViolationException('Dimensions limit is exceeded.');
     }
     $maxHeight = $converter->convert($maxDimensions->getHeight(), $this->get('dimensions_unit'), $dimensions->getUnit());
     if ($math->greaterThan($dimensions->getHeight(), $maxHeight)) {
         throw new ViolationException('Dimensions limit is exceeded.');
     }
 }