-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Default shipping and payment method resolver #11482
Open
vvasiloi
wants to merge
13
commits into
Sylius:1.12
Choose a base branch
from
vvasiloi:default_resolvers
base: 1.12
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−27
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2b6448c
use "sylius.shipping_methods_resolver" to get the default shipping me…
vvasiloi b5e1a7d
use "sylius.payment_methods_resolver" to get the default payment meth…
vvasiloi 53b42be
remove 'deprecated' tags from private methods
vvasiloi ce2233e
add spec
vvasiloi 06d4cb2
update deprecation version
vvasiloi 0753f9a
use the methods resolver by default
vvasiloi 5be0e09
make repository optional and add a deprecation notice when passing it
vvasiloi 18d4127
add a check that at least the repository or the resolver is passed
vvasiloi 91f5e3a
replace properties type annotations with built-in type hints (contrib…
vvasiloi 60aa36e
replace properties type annotations with built-in type hints (#12891)
vvasiloi 2ba6417
add spec for DefaultPaymentMethodResolver with $paymentMethodsResolver
vvasiloi a373948
make $paymentMethodRepository optional and add a deprecation notice w…
vvasiloi ad5692f
add a check that at least the repository or the resolver is passed
vvasiloi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,48 +17,104 @@ | |||||
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\Eligibility\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 | ||||||
{ | ||||||
/** @var ShippingMethodRepositoryInterface */ | ||||||
private $shippingMethodRepository; | ||||||
private ?ShippingMethodRepositoryInterface $shippingMethodRepository; | ||||||
|
||||||
/** @var ShippingMethodEligibilityCheckerInterface */ | ||||||
private $shippingMethodEligibilityChecker; | ||||||
private ShippingMethodEligibilityCheckerInterface $shippingMethodEligibilityChecker; | ||||||
|
||||||
/** @var ZoneMatcherInterface */ | ||||||
private $zoneMatcher; | ||||||
private ZoneMatcherInterface $zoneMatcher; | ||||||
|
||||||
private ?ShippingMethodsResolverInterface $shippingMethodsResolver; | ||||||
|
||||||
public function __construct( | ||||||
ShippingMethodRepositoryInterface $shippingMethodRepository, | ||||||
?ShippingMethodRepositoryInterface $shippingMethodRepository = null, | ||||||
ShippingMethodEligibilityCheckerInterface $shippingMethodEligibilityChecker, | ||||||
ZoneMatcherInterface $zoneMatcher | ||||||
ZoneMatcherInterface $zoneMatcher, | ||||||
?ShippingMethodsResolverInterface $shippingMethodsResolver = null | ||||||
) { | ||||||
$this->shippingMethodRepository = $shippingMethodRepository; | ||||||
$this->shippingMethodEligibilityChecker = $shippingMethodEligibilityChecker; | ||||||
$this->zoneMatcher = $zoneMatcher; | ||||||
|
||||||
Assert::false( | ||||||
null === $shippingMethodRepository && null === $shippingMethodsResolver, | ||||||
sprintf( | ||||||
'You must pass to "%s" constructor either a $shippingMethodRepository, or a $shippingMethodsResolver, or both.', | ||||||
__CLASS__, | ||||||
) | ||||||
); | ||||||
|
||||||
if (null !== $shippingMethodRepository) { | ||||||
@trigger_error( | ||||||
sprintf( | ||||||
'Passing an $shippingMethodRepository to "%s" constructor is deprecated since Sylius 1.9 and the argument will be removed in Sylius 2.0.', | ||||||
self::class | ||||||
), | ||||||
\E_USER_DEPRECATED | ||||||
); | ||||||
} | ||||||
|
||||||
if (null === $shippingMethodsResolver) { | ||||||
@trigger_error( | ||||||
sprintf( | ||||||
'Not passing a $shippingMethodsResolver to "%s" constructor is deprecated since Sylius 1.9 and the argument will be required starting with Sylius 2.0.', | ||||||
__CLASS__ | ||||||
), | ||||||
\E_USER_DEPRECATED | ||||||
); | ||||||
} | ||||||
|
||||||
$this->shippingMethodsResolver = $shippingMethodsResolver; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @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); | ||||||
} | ||||||
|
||||||
$shippingMethods = $this->getShippingMethods($channel, $order->getShippingAddress()); | ||||||
/** | ||||||
* @throws UnresolvedDefaultShippingMethodException | ||||||
*/ | ||||||
private function getFromResolver(CoreShipmentInterface $shipment): ShippingMethodInterface | ||||||
{ | ||||||
$shippingMethods = $this->shippingMethodsResolver->getSupportedMethods($shipment); | ||||||
|
||||||
if (empty($shippingMethods)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
throw new UnresolvedDefaultShippingMethodException(); | ||||||
} | ||||||
|
||||||
return $shippingMethods[0]; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @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)) { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
count($paymentMethods) === 0
?Or use
reset($paymentMethods)
and check if it'sfalse
instead of relying on the numeric index ($paymentMethods[0]
).