Skip to content

Commit

Permalink
[Core][Shipping] Use resolvers registry in ShippingMethodChoiceType
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 9, 2016
1 parent 0126812 commit 445de00
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 98 deletions.

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
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;
}
}
}

return new ObjectChoiceList($methods, null, [], null, 'id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<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
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,12 @@ function it_returns_all_methods_eligible_for_given_subject(
ShippingMethodInterface $method3
) {
$methods = [$method1, $method2, $method3];
$methodRepository->findBy([])->shouldBeCalled()->willReturn($methods);
$methodRepository->findBy(['enabled' => true])->shouldBeCalled()->willReturn($methods);

$eligibilityChecker->isEligible($subject, $method1)->shouldBeCalled()->willReturn(true);
$eligibilityChecker->isEligible($subject, $method2)->shouldBeCalled()->willReturn(true);
$eligibilityChecker->isEligible($subject, $method3)->shouldBeCalled()->willReturn(false);

$this->getSupportedMethods($subject)->shouldReturn([$method1, $method2]);
}

function it_filters_the_methods_pool_by_given_criteria(
$methodRepository,
$eligibilityChecker,
ShippingSubjectInterface $subject,
ShippingMethodInterface $method1,
ShippingMethodInterface $method2,
ShippingMethodInterface $method3
) {
$methods = [$method1, $method3];
$methodRepository->findBy(['enabled' => true])->shouldBeCalled()->willReturn($methods);

$eligibilityChecker->isEligible($subject, $method1)->shouldBeCalled()->willReturn(false);
$eligibilityChecker->isEligible($subject, $method2)->shouldNotBeCalled();
$eligibilityChecker->isEligible($subject, $method3)->shouldBeCalled()->willReturn(true);

$this->getSupportedMethods($subject, ['enabled' => true])->shouldReturn([$method3]);
}
}

0 comments on commit 445de00

Please sign in to comment.