public function validate(Result $result, Package $package)
 {
     try {
         $this->validateSenderAddress($package->getSenderAddress());
     } catch (ViolationException $e) {
         $result->addViolation(new Violation($e->getMessage()));
     }
     try {
         $this->validateRecipientAddress($package->getRecipientAddress());
     } catch (ViolationException $e) {
         $result->addViolation(new Violation($e->getMessage()));
     }
     try {
         $this->validateMaximumDimension($package);
     } catch (ViolationException $e) {
         $result->addViolation(new Violation($e->getMessage()));
     }
     try {
         $this->validateMaximumPerimeter($package);
     } catch (ViolationException $e) {
         $result->addViolation(new Violation($e->getMessage()));
     }
     try {
         $this->validateWeight($package);
     } catch (ViolationException $e) {
         $result->addViolation(new Violation($e->getMessage()));
     }
 }
示例#2
0
 /**
  * @param Result $result
  * @param Package $package
  * @return mixed
  */
 public function calculate(Result $result, 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;
     }
     $zoneCalculator = $this->getZoneCalculator($package);
     $total = $zoneCalculator->calculate($weight);
     $result->set('shipping_cost', $total);
 }