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, Customer, Shipping, Cart] make shipping calculation less dependant on CartInterface #783

Merged
merged 3 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ services:
tags:
- { name: coreshop.shipping_rule.action, type: discountAmount, form-type: CoreShop\Bundle\CoreBundle\Form\Type\Shipping\Rule\Action\DiscountAmountActionConfigurationType }


coreshop.carrier.price_calculator.taxed:
class: CoreShop\Component\Core\Shipping\Calculator\TaxedCarrierPriceRuleCalculator
arguments:
Expand All @@ -81,5 +80,6 @@ services:
class: CoreShop\Component\Core\Shipping\Discover\StoreBasedShippableCarriersDiscovery
decorates: coreshop.carrier.resolver
arguments:
- '@coreshop.carrier.cart.store_based_resolver.inner'
- '@coreshop.repository.carrier'
- '@coreshop.shipping.carrier.validator'
- '@coreshop.shipping.carrier.validator'
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CarrierInterface as CoreCarrierInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Core\Taxation\TaxApplicatorInterface;
use CoreShop\Component\Core\Taxation\TaxCalculatorFactoryInterface;
use CoreShop\Component\Shipping\Calculator\CarrierPriceCalculatorInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface as BaseCarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Store\Model\StoreAwareInterface;
use CoreShop\Component\Taxation\Calculator\TaxCalculatorInterface;
use CoreShop\Component\Taxation\Model\TaxRuleGroupInterface;
use Webmozart\Assert\Assert;

final class TaxedCarrierPriceRuleCalculator implements TaxedShippingCalculatorInterface
{
Expand Down Expand Up @@ -61,11 +60,6 @@ public function __construct(
*/
public function getPrice(BaseCarrierInterface $carrier, ShippableInterface $shippable, AddressInterface $address, $withTax = true)
{
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);

$price = $this->carrierPriceCalculator->getPrice($carrier, $shippable, $address);

if (!$carrier instanceof CoreCarrierInterface) {
Expand All @@ -74,7 +68,7 @@ public function getPrice(BaseCarrierInterface $carrier, ShippableInterface $ship

$taxCalculator = $this->getTaxCalculator($carrier, $address);

if ($taxCalculator instanceof TaxCalculatorInterface) {
if ($taxCalculator instanceof TaxCalculatorInterface && $shippable instanceof StoreAwareInterface) {
return $this->taxApplicator->applyTax($price, ['store' => $shippable->getStore()], $taxCalculator, $withTax);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
namespace CoreShop\Component\Core\Shipping\Discover;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Core\Repository\CarrierRepositoryInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Resolver\CarriersResolverInterface;
use CoreShop\Component\Shipping\Validator\ShippableCarrierValidatorInterface;
use Webmozart\Assert\Assert;
use CoreShop\Component\Store\Model\StoreAwareInterface;

final class StoreBasedShippableCarriersDiscovery implements CarriersResolverInterface
{
/**
* @var CarriersResolverInterface
*/
private $inner;

/**
* @var CarrierRepositoryInterface
*/
Expand All @@ -33,13 +37,16 @@ final class StoreBasedShippableCarriersDiscovery implements CarriersResolverInte
private $shippableCarrierValidator;

/**
* @param CarriersResolverInterface $inner
* @param CarrierRepositoryInterface $carrierRepository
* @param ShippableCarrierValidatorInterface $shippableCarrierValidator
*/
public function __construct(
CarriersResolverInterface $inner,
CarrierRepositoryInterface $carrierRepository,
ShippableCarrierValidatorInterface $shippableCarrierValidator
) {
$this->inner = $inner;
$this->carrierRepository = $carrierRepository;
$this->shippableCarrierValidator = $shippableCarrierValidator;
}
Expand All @@ -49,20 +56,19 @@ public function __construct(
*/
public function resolveCarriers(ShippableInterface $shippable, AddressInterface $address)
{
/**
* @var CartInterface $shippable
*/
Assert::isInstanceOf($shippable, CartInterface::class);
if ($shippable instanceof StoreAwareInterface) {
$carriers = $this->carrierRepository->findForStore($shippable->getStore());
$availableCarriers = [];

$carriers = $this->carrierRepository->findForStore($shippable->getStore());
$availableCarriers = [];

foreach ($carriers as $carrier) {
if ($this->shippableCarrierValidator->isCarrierValid($carrier, $shippable, $address)) {
$availableCarriers[] = $carrier;
foreach ($carriers as $carrier) {
if ($this->shippableCarrierValidator->isCarrierValid($carrier, $shippable, $address)) {
$availableCarriers[] = $carrier;
}
}

return $availableCarriers;
}

return $availableCarriers;
return $this->inner->resolveCarriers($shippable, $address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
namespace CoreShop\Component\Core\Shipping\Rule\Action;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Currency\Converter\CurrencyConverterInterface;
use CoreShop\Component\Currency\Model\CurrencyAwareInterface;
use CoreShop\Component\Currency\Model\CurrencyInterface;
use CoreShop\Component\Currency\Repository\CurrencyRepositoryInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
Expand Down Expand Up @@ -57,16 +57,16 @@ public function getPrice(CarrierInterface $carrier, ShippableInterface $shippabl
*/
public function getModification(CarrierInterface $carrier, ShippableInterface $shippable, AddressInterface $address, $price, array $configuration)
{
/**
* @var CartInterface $shippable
*/
Assert::isInstanceOf($shippable, CartInterface::class);

$amount = $configuration['amount'];
$currency = $this->currencyRepository->find($configuration['currency']);

Assert::isInstanceOf($currency, CurrencyInterface::class);
if ($shippable instanceof CurrencyAwareInterface) {
$currency = $this->currencyRepository->find($configuration['currency']);

Assert::isInstanceOf($currency, CurrencyInterface::class);

return $this->moneyConverter->convert($amount, $currency->getIsoCode(), $shippable->getCurrency()->getIsoCode());
}

return $this->moneyConverter->convert($amount, $currency->getIsoCode(), $shippable->getCurrency()->getIsoCode());
return $amount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
namespace CoreShop\Component\Core\Shipping\Rule\Action;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Currency\Converter\CurrencyConverterInterface;
use CoreShop\Component\Currency\Model\CurrencyAwareInterface;
use CoreShop\Component\Currency\Model\CurrencyInterface;
use CoreShop\Component\Currency\Repository\CurrencyRepositoryInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
Expand Down Expand Up @@ -57,16 +57,16 @@ public function getPrice(CarrierInterface $carrier, ShippableInterface $shippabl
*/
public function getModification(CarrierInterface $carrier, ShippableInterface $shippable, AddressInterface $address, $price, array $configuration)
{
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);

$amount = $configuration['amount'];
$currency = $this->currencyRepository->find($configuration['currency']);

Assert::isInstanceOf($currency, CurrencyInterface::class);
if ($shippable instanceof CurrencyAwareInterface) {
$currency = $this->currencyRepository->find($configuration['currency']);

Assert::isInstanceOf($currency, CurrencyInterface::class);

return -1 * $this->moneyConverter->convert($amount, $currency->getIsoCode(), $shippable->getCurrency()->getIsoCode());
}

return -1 * $this->moneyConverter->convert($amount, $currency->getIsoCode(), $shippable->getCurrency()->getIsoCode());
return $amount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
namespace CoreShop\Component\Core\Shipping\Rule\Action;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Currency\Converter\CurrencyConverterInterface;
use CoreShop\Component\Currency\Model\CurrencyAwareInterface;
use CoreShop\Component\Currency\Model\CurrencyInterface;
use CoreShop\Component\Currency\Repository\CurrencyRepositoryInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
Expand Down Expand Up @@ -49,17 +49,17 @@ public function __construct(CurrencyRepositoryInterface $currencyRepository, Cur
*/
public function getPrice(CarrierInterface $carrier, ShippableInterface $shippable, AddressInterface $address, array $configuration)
{
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);

$price = $configuration['price'];
$currency = $this->currencyRepository->find($configuration['currency']);

Assert::isInstanceOf($currency, CurrencyInterface::class);
if ($shippable instanceof CurrencyAwareInterface) {
$currency = $this->currencyRepository->find($configuration['currency']);

Assert::isInstanceOf($currency, CurrencyInterface::class);

return $this->moneyConverter->convert($price, $currency->getIsoCode(), $shippable->getCurrency()->getIsoCode());
}

return $this->moneyConverter->convert($price, $currency->getIsoCode(), $shippable->getCurrency()->getIsoCode());
return $price;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Rule\Condition\AbstractConditionChecker;
use Webmozart\Assert\Assert;

final class CategoriesConditionChecker extends AbstractConditionChecker
{
Expand All @@ -42,10 +41,9 @@ public function __construct(CategoryRepositoryInterface $categoryRepository)
*/
public function isShippingRuleValid(CarrierInterface $carrier, ShippableInterface $shippable, AddressInterface $address, array $configuration)
{
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);
if (!$shippable instanceof CartInterface) {
return false;
}

$cartItems = $shippable->getItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
namespace CoreShop\Component\Core\Shipping\Rule\Condition;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Currency\Model\CurrencyAwareInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Rule\Condition\AbstractConditionChecker;
use Webmozart\Assert\Assert;

final class CurrenciesConditionChecker extends AbstractConditionChecker
{
Expand All @@ -30,10 +29,9 @@ public function isShippingRuleValid(
AddressInterface $address,
array $configuration
) {
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);
if (!$shippable instanceof CurrencyAwareInterface) {
return false;
}

return in_array($shippable->getCurrency()->getId(), $configuration['currencies']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
namespace CoreShop\Component\Core\Shipping\Rule\Condition;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Customer\Model\CustomerAwareInterface;
use CoreShop\Component\Customer\Model\CustomerInterface;
use CoreShop\Component\Resource\Model\ResourceInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Rule\Condition\AbstractConditionChecker;
use Webmozart\Assert\Assert;

final class CustomerGroupsConditionChecker extends AbstractConditionChecker
{
Expand All @@ -32,10 +31,9 @@ public function isShippingRuleValid(
AddressInterface $address,
array $configuration
) {
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);
if (!$shippable instanceof CustomerAwareInterface) {
return false;
}

if (!$shippable->getCustomer() instanceof CustomerInterface) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
namespace CoreShop\Component\Core\Shipping\Rule\Condition;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Customer\Model\CustomerAwareInterface;
use CoreShop\Component\Customer\Model\CustomerInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Rule\Condition\AbstractConditionChecker;
use Webmozart\Assert\Assert;

final class CustomersConditionChecker extends AbstractConditionChecker
{
Expand All @@ -31,10 +30,9 @@ public function isShippingRuleValid(
AddressInterface $address,
array $configuration
) {
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);
if (!$shippable instanceof CustomerAwareInterface) {
return false;
}

if (!$shippable->getCustomer() instanceof CustomerInterface) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
namespace CoreShop\Component\Core\Shipping\Rule\Condition;

use CoreShop\Component\Address\Model\AddressInterface;
use CoreShop\Component\Core\Model\CartInterface;
use CoreShop\Component\Shipping\Model\CarrierInterface;
use CoreShop\Component\Shipping\Model\ShippableInterface;
use CoreShop\Component\Shipping\Rule\Condition\AbstractConditionChecker;
use Webmozart\Assert\Assert;
use CoreShop\Component\Store\Model\StoreAwareInterface;

final class StoresConditionChecker extends AbstractConditionChecker
{
Expand All @@ -30,10 +29,9 @@ public function isShippingRuleValid(
AddressInterface $address,
array $configuration
) {
/**
* @var $shippable CartInterface
*/
Assert::isInstanceOf($shippable, CartInterface::class);
if (!$shippable instanceof StoreAwareInterface) {
return false;
}

return in_array($shippable->getStore()->getId(), $configuration['stores']);
}
Expand Down
26 changes: 26 additions & 0 deletions src/CoreShop/Component/Customer/Model/CustomerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Component\Customer\Model;

interface CustomerAwareInterface
{
/**
* @return CustomerInterface|null
*/
public function getCustomer();

/**
* @param CustomerInterface|null $customer
*/
public function setCustomer($customer);
}
Loading