|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file was created by developers working at BitBag |
|
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
|
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductsToWishlist; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
17
|
|
|
use Sylius\Component\Order\Model\OrderItemInterface; |
|
18
|
|
|
use Sylius\Component\Order\Modifier\OrderModifierInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
20
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
21
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class AddProductsToWishlistHandler implements MessageHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
private FlashBagInterface $flashBag; |
|
26
|
|
|
|
|
27
|
|
|
private TranslatorInterface $translator; |
|
28
|
|
|
|
|
29
|
|
|
private OrderModifierInterface $orderModifier; |
|
30
|
|
|
|
|
31
|
|
|
private OrderRepositoryInterface $orderRepository; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
FlashBagInterface $flashBag, |
|
35
|
|
|
TranslatorInterface $translator, |
|
36
|
|
|
OrderModifierInterface $orderModifier, |
|
37
|
|
|
OrderRepositoryInterface $orderRepository |
|
38
|
|
|
) { |
|
39
|
|
|
$this->flashBag = $flashBag; |
|
40
|
|
|
$this->translator = $translator; |
|
41
|
|
|
$this->orderModifier = $orderModifier; |
|
42
|
|
|
$this->orderRepository = $orderRepository; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function __invoke(AddProductsToWishlist $addProductsToWishlistCommand): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->addProductsToWishlist($addProductsToWishlistCommand->getWishlistProducts()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function addProductsToWishlist(Collection $wishlistProducts): void |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var AddWishlistProduct $wishlistProduct */ |
|
53
|
|
|
foreach ($wishlistProducts as $wishlistProduct) { |
|
54
|
|
|
if ($this->productCanBeProcessed($wishlistProduct)) { |
|
55
|
|
|
$this->addProductToWishlist($wishlistProduct); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function productCanBeProcessed(AddWishlistProduct $wishlistProduct): bool |
|
61
|
|
|
{ |
|
62
|
|
|
$cartItem = $wishlistProduct->getCartItem()->getCartItem(); |
|
63
|
|
|
|
|
64
|
|
|
return $this->productIsInStock($cartItem) && $this->productHasPositiveQuantity($cartItem); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function productIsInStock(OrderItemInterface $product): bool |
|
68
|
|
|
{ |
|
69
|
|
|
if ($product->getVariant()->isInStock()) { |
|
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
$message = sprintf('%s does not have sufficient stock.', $product->getProductName()); |
|
|
|
|
|
|
73
|
|
|
$this->flashBag->add('error', $this->translator->trans($message)); |
|
74
|
|
|
|
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function productHasPositiveQuantity(OrderItemInterface $product): bool |
|
79
|
|
|
{ |
|
80
|
|
|
if (0 < $product->getQuantity()) { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
$this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.increase_quantity')); |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function addProductToWishlist(AddWishlistProduct $wishlistProduct): void |
|
89
|
|
|
{ |
|
90
|
|
|
$cart = $wishlistProduct->getCartItem()->getCart(); |
|
91
|
|
|
$cartItem = $wishlistProduct->getCartItem()->getCartItem(); |
|
92
|
|
|
|
|
93
|
|
|
$this->orderModifier->addToOrder($cart, $cartItem); |
|
94
|
|
|
$this->orderRepository->add($cart); |
|
95
|
|
|
|
|
96
|
|
|
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart')); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|