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\RemoveProductVariantFromWishlist; |
14
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
15
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface; |
16
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantNotFoundException; |
17
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException; |
18
|
|
|
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; |
19
|
|
|
use Doctrine\Persistence\ObjectManager; |
20
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
21
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
22
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
23
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
24
|
|
|
|
25
|
|
|
final class RemoveProductVariantFromWishlistHandler implements MessageHandlerInterface |
26
|
|
|
{ |
27
|
|
|
private WishlistRepositoryInterface $wishlistRepository; |
28
|
|
|
|
29
|
|
|
private ProductVariantRepositoryInterface $productVariantRepository; |
30
|
|
|
|
31
|
|
|
private RepositoryInterface $wishlistProductRepository; |
32
|
|
|
|
33
|
|
|
private ObjectManager $wishlistManager; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
37
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
38
|
|
|
RepositoryInterface $wishlistProductRepository, |
39
|
|
|
ObjectManager $wishlistManager |
40
|
|
|
) { |
41
|
|
|
$this->wishlistRepository = $wishlistRepository; |
42
|
|
|
$this->productVariantRepository = $productVariantRepository; |
43
|
|
|
$this->wishlistProductRepository = $wishlistProductRepository; |
44
|
|
|
$this->wishlistManager = $wishlistManager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function __invoke(RemoveProductVariantFromWishlist $removeProductVariantFromWishlist): WishlistInterface |
48
|
|
|
{ |
49
|
|
|
$variantId = $removeProductVariantFromWishlist->getProductVariantIdValue(); |
50
|
|
|
$token = $removeProductVariantFromWishlist->getWishlistTokenValue(); |
51
|
|
|
|
52
|
|
|
/** @var ?ProductVariantInterface $variant */ |
53
|
|
|
$variant = $this->productVariantRepository->find($variantId); |
54
|
|
|
/** @var ?WishlistProductInterface $wishlistProduct */ |
55
|
|
|
$wishlistProduct = $this->wishlistProductRepository->findOneBy(['variant' => $variant]); |
56
|
|
|
/** @var ?WishlistInterface $wishlist */ |
57
|
|
|
$wishlist = $this->wishlistRepository->findByToken($token); |
58
|
|
|
|
59
|
|
|
if (null === $variant || null === $wishlistProduct) { |
60
|
|
|
throw new ProductVariantNotFoundException( |
61
|
|
|
sprintf('The Product %s does not exist', $variantId) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null === $wishlist) { |
66
|
|
|
throw new WishlistNotFoundException( |
67
|
|
|
sprintf('The Wishlist %s does not exist', $token) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$wishlist->removeProductVariant($variant); |
72
|
|
|
$this->wishlistManager->flush(); |
73
|
|
|
|
74
|
|
|
return $wishlist; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|