/** * {@inheritdoc} */ public function getSupportedMethods(ShippingSubjectInterface $subject) { /** @var ShipmentInterface $subject */ Assert::true($this->supports($subject)); /** @var OrderInterface $order */ $order = $subject->getOrder(); $zones = $this->zoneMatcher->matchAll($order->getShippingAddress()); if (empty($zones)) { return []; } return $this->shippingMethodRepository->findEnabledForZonesAndChannel($zones, $order->getChannel()); }
/** * @param OrderInterface $order * * @return array */ private function getZonesIdsForAddress(OrderInterface $order) { $matchedZones = $this->zoneMatcher->matchAll($order->getShippingAddress()); if (empty($matchedZones)) { return []; } $zones = []; foreach ($matchedZones as $zone) { $zones[] = $zone->getId(); } return $zones; }
function it_returns_empty_array_if_zone_matcher_could_not_match_any_zone(AddressInterface $address, OrderInterface $order, ShipmentInterface $shipment, ZoneMatcherInterface $zoneMatcher) { $shipment->getOrder()->willReturn($order); $order->getShippingAddress()->willReturn($address); $zoneMatcher->matchAll($address)->willReturn([]); $this->getSupportedMethods($shipment)->shouldReturn([]); }
/** * {@inheritdoc} */ public function getSupportedMethods(ShippingSubjectInterface $subject) { /** @var ShipmentInterface $subject */ Assert::true($this->supports($subject)); /** @var OrderInterface $order */ $order = $subject->getOrder(); $zones = $this->zoneMatcher->matchAll($order->getShippingAddress()); if (empty($zones)) { return []; } $methods = []; $shippingMethods = $this->shippingMethodRepository->findEnabledForZonesAndChannel($zones, $order->getChannel()); foreach ($shippingMethods as $shippingMethod) { if ($this->eligibilityChecker->isEligible($subject, $shippingMethod)) { $methods[] = $shippingMethod; } } return $methods; }
function it_returns_only_shipping_methods_that_are_eligible(ShippingMethodEligibilityCheckerInterface $eligibilityChecker, AddressInterface $address, ChannelInterface $channel, OrderInterface $order, ShipmentInterface $shipment, ShippingMethodInterface $firstShippingMethod, ShippingMethodInterface $secondShippingMethod, ShippingMethodRepositoryInterface $shippingMethodRepository, ZoneInterface $firstZone, ZoneInterface $secondZone, ZoneMatcherInterface $zoneMatcher) { $shipment->getOrder()->willReturn($order); $order->getShippingAddress()->willReturn($address); $order->getChannel()->willReturn($channel); $zoneMatcher->matchAll($address)->willReturn([$firstZone, $secondZone]); $eligibilityChecker->isEligible($shipment, $firstShippingMethod)->willReturn(false); $eligibilityChecker->isEligible($shipment, $secondShippingMethod)->willReturn(true); $shippingMethodRepository->findEnabledForZonesAndChannel([$firstZone, $secondZone], $channel)->willReturn([$firstShippingMethod, $secondShippingMethod]); $this->getSupportedMethods($shipment)->shouldReturn([$secondShippingMethod]); }