Skip to content
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

[Core][Shipping] Fix shipping method choice type #5216

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Context/Setup/ShippingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ private function createShippingMethod(

$this->shippingMethodRepository->add($shippingMethod);
$this->sharedStorage->set('shipping_method', $shippingMethod);

if ($this->sharedStorage->has('channel')) {
$channel = $this->sharedStorage->get('channel');
$channel->addShippingMethod($shippingMethod);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if should be before $this->shippingMethodRepository->add($shippingMethod);. Right now the relation is not flushed in this step.

}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,6 @@ sylius_shipping:
model: Sylius\Component\Core\Model\ShippingMethod
form:
default: Sylius\Bundle\CoreBundle\Form\Type\ShippingMethodType
choice: Sylius\Bundle\CoreBundle\Form\Type\Shipping\ShippingMethodChoiceType

sylius_taxation:
resources:
Expand Down
8 changes: 8 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<parameter key="sylius.integer_distributor.class">Sylius\Component\Core\Distributor\IntegerDistributor</parameter>
<parameter key="sylius.proportional_integer_distributor.class">Sylius\Component\Core\Distributor\ProportionalIntegerDistributor</parameter>

<parameter key="sylius.shipping_methods_resolver.by_zones_and_channel.class">Sylius\Component\Core\Resolver\ShippingMethodsByZonesAndChannelResolver</parameter>

<!-- Data fetchers -->
<parameter key="sylius.form.type.data_fetcher.number_of_orders.class">Sylius\Bundle\CoreBundle\Form\Type\DataFetcher\NumberOfOrdersType</parameter>
<parameter key="sylius.form.type.data_fetcher.sales_total.class">Sylius\Bundle\CoreBundle\Form\Type\DataFetcher\SalesTotalType</parameter>
Expand Down Expand Up @@ -622,6 +624,12 @@
<argument type="service" id="sylius.sql_logger.purger" />
</service>

<service id="sylius.shipping_methods_resolver.by_zones_and_channel" class="%sylius.shipping_methods_resolver.by_zones_and_channel.class%">
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.zone_matcher" />
<tag name="sylius.shipping_method_resolver" type="by_zones_and_channel" label="By zones and channel" priority="1" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Label should be a translation key: sylius.shipping_methods_resolver.by_zones_and_channel.

</service>

<!-- Data fetchers -->
<service id="sylius.form.type.data_fetcher.user_registration" class="%sylius.form.type.data_fetcher.user_registration.class%">
<tag name="form.type" alias="sylius_data_fetcher_user_registration" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
class RegisterShippingMethodsResolverPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('sylius.registry.shipping_methods_resolver')) {
return;
}

$registry = $container->findDefinition('sylius.registry.shipping_methods_resolver');
$resolvers = [];

foreach ($container->findTaggedServiceIds('sylius.shipping_method_resolver') as $id => $attributes) {
if (!isset($attributes[0]['type']) || !isset($attributes[0]['label'])) {
throw new \InvalidArgumentException('Tagged shipping methods resolvers need to have `type` and `label` attributes.');
}

$priority = isset($attributes[0]['priority']) ? (int) $attributes[0]['priority'] : 0;

$resolvers[$attributes[0]['type']] = $attributes[0]['label'];

$registry->addMethodCall('register', [new Reference($id), $priority]);
}

$container->setParameter('sylius.shipping_method_resolvers', $resolvers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Bundle\ShippingBundle\Form\Type\RuleType;
use Sylius\Bundle\ShippingBundle\Form\Type\ShipmentType;
use Sylius\Bundle\ShippingBundle\Form\Type\ShippingCategoryType;
use Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodChoiceType;
use Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodTranslationType;
use Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodType;
use Sylius\Component\Resource\Factory\Factory;
Expand Down Expand Up @@ -136,7 +137,7 @@ private function addResourcesSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->children()
->scalarNode('default')->defaultValue(ShippingMethodType::class)->cannotBeEmpty()->end()
->scalarNode('choice')->defaultValue(ResourceChoiceType::class)->cannotBeEmpty()->end()
->scalarNode('choice')->defaultValue(ShippingMethodChoiceType::class)->cannotBeEmpty()->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sylius\Bundle\ShippingBundle\Form\Type;

use Sylius\Component\Registry\PrioritizedServiceRegistryInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Shipping\Model\ShippingMethodInterface;
Expand All @@ -35,11 +36,9 @@
class ShippingMethodChoiceType extends AbstractType
{
/**
* Supported methods resolver.
*
* @var MethodsResolverInterface
* @var PrioritizedServiceRegistryInterface
*/
protected $resolver;
protected $resolversRegistry;

/**
* @var ServiceRegistryInterface
Expand All @@ -52,16 +51,16 @@ class ShippingMethodChoiceType extends AbstractType
protected $repository;

/**
* @param MethodsResolverInterface $resolver
* @param ServiceRegistryInterface $calculators
* @param RepositoryInterface $repository
* @param PrioritizedServiceRegistryInterface $resolversRegistry
* @param ServiceRegistryInterface $calculators
* @param RepositoryInterface $repository
*/
public function __construct(
MethodsResolverInterface $resolver,
PrioritizedServiceRegistryInterface $resolversRegistry,
ServiceRegistryInterface $calculators,
RepositoryInterface $repository
) {
$this->resolver = $resolver;
$this->resolversRegistry = $resolversRegistry;
$this->calculators = $calculators;
$this->repository = $repository;
}
Expand All @@ -82,10 +81,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
public function configureOptions(OptionsResolver $resolver)
{
$choiceList = function (Options $options) {
$methods = $methods = $this->repository->findAll();

if (isset($options['subject'])) {
$methods = $this->resolver->getSupportedMethods($options['subject'], $options['criteria']);
} else {
$methods = $this->repository->findBy($options['criteria']);
/** @var MethodsResolverInterface $resolver */
foreach ($this->resolversRegistry->all() as $resolver) {
if ($resolver->supports($options['subject'])) {
$methods = $resolver->getSupportedMethods($options['subject']);
break;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this logic to the "main" methods resolver and inject it here. It will support every shipment and use exactly this logic to get the right methods. (CompositeMethodsResolver as name, wdyt?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 👍 Less logic in form type is always a good choice 😄

}

return new ObjectChoiceList($methods, null, [], null, 'id');
Expand Down
11 changes: 10 additions & 1 deletion src/Sylius/Bundle/ShippingBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

<parameter key="sylius.registry.shipping_rule_checker.class">Sylius\Component\Registry\ServiceRegistry</parameter>

<parameter key="sylius.registry.shipping_methods_resolver.class">Sylius\Component\Registry\PrioritizedServiceRegistry</parameter>
<parameter key="sylius.shipping_methods_resolver.interface">Sylius\Component\Shipping\Resolver\MethodsResolverInterface</parameter>

<parameter key="sylius.shipping_eligibility_checker.class">Sylius\Component\Shipping\Checker\ShippingMethodEligibilityChecker</parameter>
<parameter key="sylius.shipping_rule_checker.unit_count.class">Sylius\Component\Shipping\Checker\UnitCountRuleChecker</parameter>

Expand Down Expand Up @@ -67,12 +70,18 @@
<argument>shipping rule checker</argument>
</service>

<service id="sylius.registry.shipping_methods_resolver" class="%sylius.registry.shipping_methods_resolver.class%">
<argument>%sylius.shipping_methods_resolver.interface%</argument>
<argument>Shipping methods resolver</argument>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shipping_methods_resolver?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to service implementation $contextparameter should be "human readable".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for me, but then let's lowercase the first letter 😄

Copy link
Member

@pjedrzejewski pjedrzejewski Jun 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalmarcinkowski This is for exception message, so the first uppercase letter is ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I recall that PR :)

</service>

<service id="sylius.shipping_methods_resolver" class="%sylius.shipping_methods_resolver.class%">
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.shipping_eligibility_checker" />
<tag name="sylius.shipping_method_resolver" type="default" label="Default" />
</service>
<service id="sylius.form.type.shipping_method_choice" class="%sylius.form.type.shipping_method_choice.class%">
<argument type="service" id="sylius.shipping_methods_resolver" />
<argument type="service" id="sylius.registry.shipping_methods_resolver" />
<argument type="service" id="sylius.registry.shipping_calculator" />
<argument type="service" id="sylius.repository.shipping_method" />
<tag name="form.type" alias="sylius_shipping_method_choice" />
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/ShippingBundle/SyliusShippingBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler\RegisterCalculatorsPass;
use Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler\RegisterRuleCheckersPass;
use Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler\RegisterShippingMethodsResolverPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
Expand Down Expand Up @@ -47,6 +48,7 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass(new RegisterCalculatorsPass());
$container->addCompilerPass(new RegisterRuleCheckersPass());
$container->addCompilerPass(new RegisterShippingMethodsResolverPass());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually use plural form here: Resolvers.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Registry\PrioritizedServiceRegistryInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;
Expand All @@ -28,11 +29,11 @@
class ShippingMethodChoiceTypeSpec extends ObjectBehavior
{
function let(
MethodsResolverInterface $resolver,
PrioritizedServiceRegistryInterface $resolversRegistry,
ServiceRegistryInterface $calculators,
RepositoryInterface $repository
) {
$this->beConstructedWith($resolver, $calculators, $repository);
$this->beConstructedWith($resolversRegistry, $calculators, $repository);
}

function it_is_initializable()
Expand Down
Loading