|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
8
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
|
9
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
10
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
11
|
|
|
use Sylius\ShopApiPlugin\Command\PutVariantBasedConfigurableItemToCart; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Modifier\OrderModifierInterface; |
|
13
|
|
|
use Webmozart\Assert\Assert; |
|
14
|
|
|
|
|
15
|
|
|
final class PutVariantBasedConfigurableItemToCartHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var OrderRepositoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cartRepository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ProductVariantRepositoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $productVariantRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var OrderModifierInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $orderModifier; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
OrderRepositoryInterface $cartRepository, |
|
34
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
35
|
|
|
OrderModifierInterface $orderModifier |
|
36
|
|
|
) { |
|
37
|
|
|
$this->cartRepository = $cartRepository; |
|
38
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
39
|
|
|
$this->orderModifier = $orderModifier; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
public function handle(PutVariantBasedConfigurableItemToCart $putConfigurableItemToCart) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
/** @var OrderInterface $cart */ |
|
45
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $putConfigurableItemToCart->orderToken()]); |
|
46
|
|
|
|
|
47
|
|
|
Assert::notNull($cart, 'Cart has not been found'); |
|
48
|
|
|
|
|
49
|
|
|
/** @var ProductVariantInterface $productVariant */ |
|
50
|
|
|
$productVariant = $this->productVariantRepository->findOneByCodeAndProductCode($putConfigurableItemToCart->productVariant(), $putConfigurableItemToCart->product()); |
|
51
|
|
|
|
|
52
|
|
|
Assert::notNull($productVariant, 'Product variant has not been found'); |
|
53
|
|
|
|
|
54
|
|
|
$this->orderModifier->modify($cart, $productVariant, $putConfigurableItemToCart->quantity()); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.