[Core] Remove shipping methods collection from channel

This commit is contained in:
Mateusz Zalewski 2016-11-15 14:22:42 +01:00
parent eecf6b6824
commit 0f9e0087a9
No known key found for this signature in database
GPG key ID: CFC1E4176165876B
13 changed files with 79 additions and 100 deletions

View file

@ -17,6 +17,7 @@ use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Addressing\Repository\ZoneRepositoryInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
@ -255,8 +256,9 @@ final class ShippingContext implements Context
$shippingMethod->setEnabled($enabled);
if ($addForCurrentChannel && $this->sharedStorage->has('channel')) {
/** @var ChannelInterface $channel */
$channel = $this->sharedStorage->get('channel');
$channel->addShippingMethod($shippingMethod);
$shippingMethod->addChannel($channel);
}
$this->shippingMethodRepository->add($shippingMethod);

View file

@ -11,6 +11,7 @@
namespace Sylius\Bundle\CoreBundle\Doctrine\ORM;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ShippingBundle\Doctrine\ORM\ShippingMethodRepository as BaseShippingMethodRepository;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
@ -38,11 +39,9 @@ class ShippingMethodRepository extends BaseShippingMethodRepository implements S
*/
public function findEnabledForZonesAndChannel(array $zones, ChannelInterface $channel)
{
return $this->createQueryBuilder('o')
->where('o.enabled = true')
->andWhere('o IN (:channelShippingMethods)')
return $this
->getEnabledForChannelQueryBuilder($channel)
->andWhere('o.zone IN (:zones)')
->setParameter('channelShippingMethods', $channel->getShippingMethods()->toArray())
->setParameter('zones', $zones)
->orderBy('o.position', 'asc')
->getQuery()
@ -55,12 +54,31 @@ class ShippingMethodRepository extends BaseShippingMethodRepository implements S
*/
public function findEnabledForChannel(ChannelInterface $channel)
{
return $this->createQueryBuilder('o')
->where('o.enabled = true')
->andWhere('o IN (:channelShippingMethods)')
->setParameter('channelShippingMethods', $channel->getShippingMethods()->toArray())
return $this
->getEnabledForChannelQueryBuilder($channel)
->getQuery()
->getResult()
;
}
/**
* @param ChannelInterface $channel
*
* @return QueryBuilder
*/
private function getEnabledForChannelQueryBuilder(ChannelInterface $channel)
{
$queryBuilder = $this
->createQueryBuilder('o')
->where('o.enabled = true')
;
$queryBuilder
->innerJoin($this->getPropertyName('channels'), 'channel')
->andWhere($queryBuilder->expr()->eq('channel', ':channel'))
->setParameter('channel', $channel)
;
return $queryBuilder;
}
}

View file

@ -43,7 +43,6 @@ final class ChannelFixture extends AbstractResourceFixture
->arrayNode('locales')->prototype('scalar')->end()->end()
->scalarNode('default_currency')->cannotBeEmpty()->end()
->arrayNode('currencies')->prototype('scalar')->end()->end()
->arrayNode('shipping_methods')->prototype('scalar')->end()->end()
->scalarNode('theme_name')->end()
;
}

View file

@ -45,15 +45,11 @@ final class ChannelExampleFactory implements ExampleFactoryInterface
* @param ChannelFactoryInterface $channelFactory
* @param RepositoryInterface $localeRepository
* @param RepositoryInterface $currencyRepository
* @param RepositoryInterface $paymentMethodRepository
* @param RepositoryInterface $shippingMethodRepository
*/
public function __construct(
ChannelFactoryInterface $channelFactory,
RepositoryInterface $localeRepository,
RepositoryInterface $currencyRepository,
RepositoryInterface $paymentMethodRepository,
RepositoryInterface $shippingMethodRepository
RepositoryInterface $currencyRepository
) {
$this->channelFactory = $channelFactory;
@ -94,9 +90,6 @@ final class ChannelExampleFactory implements ExampleFactoryInterface
->setDefault('currencies', LazyOption::all($currencyRepository))
->setAllowedTypes('currencies', 'array')
->setNormalizer('currencies', LazyOption::findBy($currencyRepository, 'code'))
->setDefault('shipping_methods', LazyOption::all($shippingMethodRepository))
->setAllowedTypes('shipping_methods', 'array')
->setNormalizer('shipping_methods', LazyOption::findBy($shippingMethodRepository, 'code'))
->setDefault('theme_name', null)
;
}
@ -127,10 +120,6 @@ final class ChannelExampleFactory implements ExampleFactoryInterface
$channel->addCurrency($currency);
}
foreach ($options['shipping_methods'] as $shippingMethod) {
$channel->addShippingMethod($shippingMethod);
}
return $channel;
}
}

View file

@ -13,6 +13,7 @@ namespace Sylius\Bundle\CoreBundle\Fixture\Factory;
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
@ -38,6 +39,11 @@ final class ShippingMethodExampleFactory implements ExampleFactoryInterface
*/
private $localeRepository;
/**
* @var ChannelRepositoryInterface
*/
private $channelRepository;
/**
* @var \Faker\Generator
*/
@ -53,15 +59,18 @@ final class ShippingMethodExampleFactory implements ExampleFactoryInterface
* @param RepositoryInterface $zoneRepository
* @param RepositoryInterface $shippingCategoryRepository
* @param RepositoryInterface $localeRepository
* @param ChannelRepositoryInterface $channelRepository
*/
public function __construct(
FactoryInterface $shippingMethodFactory,
RepositoryInterface $zoneRepository,
RepositoryInterface $shippingCategoryRepository,
RepositoryInterface $localeRepository
RepositoryInterface $localeRepository,
ChannelRepositoryInterface $channelRepository
) {
$this->shippingMethodFactory = $shippingMethodFactory;
$this->localeRepository = $localeRepository;
$this->channelRepository = $channelRepository;
$this->faker = \Faker\Factory::create();
$this->optionsResolver =
@ -88,6 +97,9 @@ final class ShippingMethodExampleFactory implements ExampleFactoryInterface
->setDefault('calculator', function (Options $options) {
return ['type' => DefaultCalculators::FLAT_RATE, 'configuration' => ['amount' => $this->faker->randomNumber(4)]];
})
->setDefault('channels', LazyOption::all($channelRepository))
->setAllowedTypes('channels', 'array')
->setNormalizer('channels', LazyOption::findBy($channelRepository, 'name'))
;
}
@ -118,6 +130,10 @@ final class ShippingMethodExampleFactory implements ExampleFactoryInterface
$shippingMethod->setDescription($options['description']);
}
foreach ($options['channels'] as $channel) {
$shippingMethod->addChannel($channel);
}
return $shippingMethod;
}

View file

@ -18,6 +18,7 @@ use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
@ -74,6 +75,11 @@ final class OrderFixture extends AbstractFixture
*/
private $paymentMethodRepository;
/**
* @var ShippingMethodRepositoryInterface
*/
private $shippingMethodRepository;
/**
* @var FactoryInterface
*/
@ -99,6 +105,7 @@ final class OrderFixture extends AbstractFixture
* @param RepositoryInterface $productRepository
* @param RepositoryInterface $countryRepository
* @param PaymentMethodRepositoryInterface $paymentMethodRepository
* @param ShippingMethodRepositoryInterface $shippingMethodRepository
* @param FactoryInterface $addressFactory
* @param StateMachineFactoryInterface $stateMachineFactory
*/
@ -112,6 +119,7 @@ final class OrderFixture extends AbstractFixture
RepositoryInterface $productRepository,
RepositoryInterface $countryRepository,
PaymentMethodRepositoryInterface $paymentMethodRepository,
ShippingMethodRepositoryInterface $shippingMethodRepository,
FactoryInterface $addressFactory,
StateMachineFactoryInterface $stateMachineFactory
) {
@ -124,6 +132,7 @@ final class OrderFixture extends AbstractFixture
$this->productRepository = $productRepository;
$this->countryRepository = $countryRepository;
$this->paymentMethodRepository = $paymentMethodRepository;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->addressFactory = $addressFactory;
$this->stateMachineFactory = $stateMachineFactory;
@ -236,7 +245,7 @@ final class OrderFixture extends AbstractFixture
*/
private function selectShipping(OrderInterface $order)
{
$shippingMethod = $this->faker->randomElement($order->getChannel()->getShippingMethods()->toArray());
$shippingMethod = $this->faker->randomElement($this->shippingMethodRepository->findEnabledForChannel($order->getChannel()));
Assert::notNull($shippingMethod);

View file

@ -39,6 +39,7 @@ final class ShippingMethodFixture extends AbstractResourceFixture
->scalarNode('zone')->cannotBeEmpty()->end()
->booleanNode('enabled')->end()
->scalarNode('category')->end()
->arrayNode('channels')->prototype('scalar')->end()->end()
->arrayNode('calculator')
->children()
->scalarNode('type')->isRequired()->cannotBeEmpty()->end()

View file

@ -22,20 +22,6 @@ sylius_fixtures:
countries:
- "US"
shipping_method:
options:
custom:
ups:
code: "ups"
name: "UPS"
enabled: true
dhl_express:
code: "dhl_express"
name: "DHL Express"
fedex:
code: "fedex"
name: "FedEx"
channel:
options:
custom:
@ -45,10 +31,6 @@ sylius_fixtures:
- "%locale%"
currencies:
- "%currency%"
shipping_methods:
- "ups"
- "dhl_express"
- "fedex"
enabled: true
payment_method:
@ -66,6 +48,24 @@ sylius_fixtures:
- "US Web Store"
enabled: true
shipping_method:
options:
custom:
ups:
code: "ups"
name: "UPS"
enabled: true
dhl_express:
code: "dhl_express"
name: "DHL Express"
channels:
- "US Web Store"
fedex:
code: "fedex"
name: "FedEx"
channels:
- "US Web Store"
regular_user:
name: "shop_user"
options:

View file

@ -49,16 +49,6 @@
</inverse-join-columns>
</join-table>
</many-to-many>
<many-to-many field="shippingMethods" target-entity="Sylius\Component\Shipping\Model\ShippingMethodInterface">
<join-table name="sylius_channel_shipping_methods">
<join-columns>
<join-column name="channel_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
</join-columns>
<inverse-join-columns>
<join-column name="shipping_method_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
</inverse-join-columns>
</join-table>
</many-to-many>
</mapped-superclass>
</doctrine-mapping>

View file

@ -151,6 +151,7 @@
<argument type="service" id="sylius.repository.product" />
<argument type="service" id="sylius.repository.country" />
<argument type="service" id="sylius.repository.payment_method" />
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.factory.address" />
<argument type="service" id="sm.factory" />
<tag name="sylius_fixtures.fixture" />

View file

@ -28,14 +28,13 @@
<argument type="service" id="sylius.repository.zone" />
<argument type="service" id="sylius.repository.shipping_category" />
<argument type="service" id="sylius.repository.locale" />
<argument type="service" id="sylius.repository.channel" />
</service>
<service id="sylius.fixture.example_factory.channel" class="Sylius\Bundle\CoreBundle\Fixture\Factory\ChannelExampleFactory">
<argument type="service" id="sylius.factory.channel" />
<argument type="service" id="sylius.repository.locale" />
<argument type="service" id="sylius.repository.currency" />
<argument type="service" id="sylius.repository.payment_method" />
<argument type="service" id="sylius.repository.shipping_method" />
</service>
<service id="sylius.fixture.example_factory.shop_user" class="Sylius\Bundle\CoreBundle\Fixture\Factory\ShopUserExampleFactory">

View file

@ -55,11 +55,6 @@ class Channel extends BaseChannel implements ChannelInterface
*/
protected $locales;
/**
* @var BaseShippingMethodInterface[]|Collection
*/
protected $shippingMethods;
/**
* @var string
*/
@ -71,8 +66,6 @@ class Channel extends BaseChannel implements ChannelInterface
$this->currencies = new ArrayCollection();
$this->locales = new ArrayCollection();
$this->paymentMethods = new ArrayCollection();
$this->shippingMethods = new ArrayCollection();
}
/**
@ -211,42 +204,6 @@ class Channel extends BaseChannel implements ChannelInterface
return $this->locales->contains($locale);
}
/**
* {@inheritdoc}
*/
public function getShippingMethods()
{
return $this->shippingMethods;
}
/**
* {@inheritdoc}
*/
public function addShippingMethod(BaseShippingMethodInterface $shippingMethod)
{
if (!$this->hasShippingMethod($shippingMethod)) {
$this->shippingMethods->add($shippingMethod);
}
}
/**
* {@inheritdoc}
*/
public function removeShippingMethod(BaseShippingMethodInterface $shippingMethod)
{
if ($this->hasShippingMethod($shippingMethod)) {
$this->shippingMethods->removeElement($shippingMethod);
}
}
/**
* {@inheritdoc}
*/
public function hasShippingMethod(BaseShippingMethodInterface $shippingMethod)
{
return $this->shippingMethods->contains($shippingMethod);
}
/**
* {@inheritdoc}
*/

View file

@ -17,7 +17,6 @@ use Sylius\Component\Currency\Model\CurrenciesAwareInterface;
use Sylius\Component\Currency\Model\CurrencyInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Locale\Model\LocalesAwareInterface;
use Sylius\Component\Shipping\Model\ShippingMethodsAwareInterface;
/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
@ -25,8 +24,7 @@ use Sylius\Component\Shipping\Model\ShippingMethodsAwareInterface;
interface ChannelInterface extends
BaseChannelInterface,
CurrenciesAwareInterface,
LocalesAwareInterface,
ShippingMethodsAwareInterface
LocalesAwareInterface
{
/**
* @return LocaleInterface