|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Sylius\Modifier; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Sylius\Entity\Order\OrderItemInterface; |
|
16
|
|
|
use Doctrine\Persistence\ObjectManager; |
|
17
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
18
|
|
|
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface; |
|
19
|
|
|
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
|
20
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
|
21
|
|
|
use Webmozart\Assert\Assert; |
|
22
|
|
|
|
|
23
|
|
|
final class OrderModifier implements OrderModifierInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var OrderItemQuantityModifierInterface */ |
|
26
|
|
|
private $orderItemQuantityModifier; |
|
27
|
|
|
|
|
28
|
|
|
/** @var OrderProcessorInterface */ |
|
29
|
|
|
private $orderProcessor; |
|
30
|
|
|
|
|
31
|
|
|
/** @var ObjectManager */ |
|
32
|
|
|
private $orderManager; |
|
33
|
|
|
|
|
34
|
|
|
/** @var AvailabilityCheckerInterface */ |
|
35
|
|
|
private $availabilityChecker; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
OrderItemQuantityModifierInterface $orderItemQuantityModifier, |
|
39
|
|
|
OrderProcessorInterface $orderProcessor, |
|
40
|
|
|
ObjectManager $orderManager, |
|
41
|
|
|
AvailabilityCheckerInterface $availabilityChecker |
|
42
|
|
|
) { |
|
43
|
|
|
$this->orderItemQuantityModifier = $orderItemQuantityModifier; |
|
44
|
|
|
$this->orderProcessor = $orderProcessor; |
|
45
|
|
|
$this->orderManager = $orderManager; |
|
46
|
|
|
$this->availabilityChecker = $availabilityChecker; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function modify( |
|
50
|
|
|
OrderInterface $order, |
|
51
|
|
|
OrderItemInterface $cartItem, |
|
52
|
|
|
int $newQuantity, |
|
53
|
|
|
string $uuid |
|
54
|
|
|
): void { |
|
55
|
|
|
Assert::true( |
|
56
|
|
|
$this->availabilityChecker->isStockSufficient($cartItem->getVariant(), $newQuantity), |
|
57
|
|
|
sprintf('We don\'t have as many "%s" as you requested.', $cartItem->getProductName()) |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->orderItemQuantityModifier->modify($cartItem, $newQuantity); |
|
61
|
|
|
|
|
62
|
|
|
$cartItem->setUuid($uuid); |
|
63
|
|
|
|
|
64
|
|
|
$this->orderProcessor->process($order); |
|
65
|
|
|
|
|
66
|
|
|
$this->orderManager->persist($order); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|