-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #784 from dpfaffenbauer/cart-item-processor-extract
Cart item processor extract
- Loading branch information
Showing
4 changed files
with
195 additions
and
100 deletions.
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
90 changes: 90 additions & 0 deletions
90
src/CoreShop/Component/Core/Order/Processor/CartItemsProcessor.php
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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\Core\Order\Processor; | ||
|
||
use CoreShop\Component\Core\Model\CartItemInterface; | ||
use CoreShop\Component\Core\Model\StoreInterface; | ||
use CoreShop\Component\Order\Calculator\PurchasableCalculatorInterface; | ||
use CoreShop\Component\Order\Model\CartInterface; | ||
use CoreShop\Component\Order\Processor\CartItemProcessorInterface; | ||
use CoreShop\Component\Order\Processor\CartProcessorInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class CartItemsProcessor implements CartProcessorInterface | ||
{ | ||
/** | ||
* @var PurchasableCalculatorInterface | ||
*/ | ||
private $productPriceCalculator; | ||
|
||
/** | ||
* @var CartItemProcessorInterface | ||
*/ | ||
private $cartItemProcessor; | ||
|
||
/** | ||
* @param PurchasableCalculatorInterface $productPriceCalculator | ||
* @param CartItemProcessorInterface $cartItemProcessor | ||
*/ | ||
public function __construct( | ||
PurchasableCalculatorInterface $productPriceCalculator, | ||
CartItemProcessorInterface $cartItemProcessor | ||
) { | ||
$this->productPriceCalculator = $productPriceCalculator; | ||
$this->cartItemProcessor = $cartItemProcessor; | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(CartInterface $cart) | ||
{ | ||
$store = $cart->getStore(); | ||
|
||
/** | ||
* @var StoreInterface $store | ||
*/ | ||
Assert::isInstanceOf($store, StoreInterface::class); | ||
|
||
$context = [ | ||
'store' => $store, | ||
'customer' => $cart->getCustomer() ?: null, | ||
'currency' => $cart->getCurrency(), | ||
'country' => $store->getBaseCountry(), | ||
'cart' => $cart, | ||
]; | ||
|
||
/** | ||
* @var CartItemInterface $item | ||
*/ | ||
foreach ($cart->getItems() as $item) { | ||
$product = $item->getProduct(); | ||
|
||
$itemPrice = $this->productPriceCalculator->getPrice($product, $context, true); | ||
$itemPriceWithoutDiscount = $this->productPriceCalculator->getPrice($product, $context); | ||
$itemRetailPrice = $this->productPriceCalculator->getRetailPrice($product, $context); | ||
$itemDiscountPrice = $this->productPriceCalculator->getDiscountPrice($product, $context); | ||
$itemDiscount = $this->productPriceCalculator->getDiscount($product, $context, $itemPriceWithoutDiscount); | ||
|
||
$this->cartItemProcessor->processCartItem( | ||
$item, | ||
$itemPrice, | ||
$itemRetailPrice, | ||
$itemDiscountPrice, | ||
$itemDiscount, | ||
$context | ||
); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/CoreShop/Component/Order/Processor/CartItemProcessorInterface.php
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\Order\Processor; | ||
|
||
use CoreShop\Component\Order\Model\CartItemInterface; | ||
|
||
interface CartItemProcessorInterface | ||
{ | ||
/** | ||
* @param CartItemInterface $cartItem | ||
* @param int $itemPrice | ||
* @param int $itemRetailPrice | ||
* @param int $itemDiscountPrice | ||
* @param int $itemDiscount | ||
* @param array $context | ||
*/ | ||
public function processCartItem( | ||
CartItemInterface $cartItem, | ||
int $itemPrice, | ||
int $itemRetailPrice, | ||
int $itemDiscountPrice, | ||
int $itemDiscount, | ||
array $context | ||
); | ||
} |