Skip to content

Commit

Permalink
use "sylius.shipping_methods_resolver" to get the default shipping me…
Browse files Browse the repository at this point in the history
…thod in "sylius.shipping_method_resolver.default"
  • Loading branch information
vvasiloi committed May 17, 2020
1 parent 4601734 commit b733893
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

<service id="sylius.shipping_method_resolver.default" class="Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolver">
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.shipping_methods_resolver" />
</service>
<service id="Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface" alias="sylius.shipping_method_resolver.default" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface as CoreShipmentInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Shipping\Checker\ShippingMethodEligibilityCheckerInterface;
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException;
use Sylius\Component\Shipping\Model\ShipmentInterface;
use Sylius\Component\Shipping\Model\ShippingMethodInterface;
use Sylius\Component\Shipping\Resolver\DefaultShippingMethodResolverInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
use Webmozart\Assert\Assert;

final class EligibleDefaultShippingMethodResolver implements DefaultShippingMethodResolverInterface
Expand All @@ -38,30 +38,71 @@ final class EligibleDefaultShippingMethodResolver implements DefaultShippingMeth
/** @var ZoneMatcherInterface */
private $zoneMatcher;

/** @var ShippingMethodsResolverInterface|null */
private $shippingMethodsResolver;

public function __construct(
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShippingMethodEligibilityCheckerInterface $shippingMethodEligibilityChecker,
ZoneMatcherInterface $zoneMatcher
ZoneMatcherInterface $zoneMatcher,
?ShippingMethodsResolverInterface $shippingMethodsResolver = null
) {
$this->shippingMethodRepository = $shippingMethodRepository;
$this->shippingMethodEligibilityChecker = $shippingMethodEligibilityChecker;
$this->zoneMatcher = $zoneMatcher;

if (null === $shippingMethodsResolver) {
@trigger_error(
sprintf(
'Not passing an $shippingMethodsResolver to "%s" constructor is deprecated since Sylius 1.8 and will be impossible in Sylius 2.0.',
self::class
),
\E_USER_DEPRECATED
);
}

$this->shippingMethodsResolver = $shippingMethodsResolver;
}

/**
* {@inheritdoc}
* @param ShipmentInterface|CoreShipmentInterface $shipment
*
* @throws UnresolvedDefaultShippingMethodException
*/
public function getDefaultShippingMethod(ShipmentInterface $shipment): ShippingMethodInterface
{
/** @var CoreShipmentInterface $shipment */
Assert::isInstanceOf($shipment, CoreShipmentInterface::class);

/** @var OrderInterface $order */
$order = $shipment->getOrder();
/** @var ChannelInterface $channel */
$channel = $order->getChannel();
if (null !== $this->shippingMethodsResolver) {
return $this->getFromResolver($shipment);
}

return $this->getFromRepository($shipment);
}

/**
* @throws UnresolvedDefaultShippingMethodException
*/
private function getFromResolver(CoreShipmentInterface $shipment): ShippingMethodInterface
{
$shippingMethods = $this->shippingMethodsResolver->getSupportedMethods($shipment);

if (empty($shippingMethods)) {
throw new UnresolvedDefaultShippingMethodException();
}

return $shippingMethods[0];
}

$shippingMethods = $this->getShippingMethods($channel, $order->getShippingAddress());
/**
* @deprecated
*
* @throws UnresolvedDefaultShippingMethodException
*/
private function getFromRepository(CoreShipmentInterface $shipment): ShippingMethodInterface
{
$order = $shipment->getOrder();
$shippingMethods = $this->getShippingMethods($order->getChannel(), $order->getShippingAddress());

foreach ($shippingMethods as $shippingMethod) {
if ($this->shippingMethodEligibilityChecker->isEligible($shipment, $shippingMethod)) {
Expand All @@ -73,6 +114,8 @@ public function getDefaultShippingMethod(ShipmentInterface $shipment): ShippingM
}

/**
* @deprecated
*
* @return array|ShippingMethodInterface[]
*/
private function getShippingMethods(ChannelInterface $channel, ?AddressInterface $address): array
Expand Down

0 comments on commit b733893

Please sign in to comment.