1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; |
6
|
|
|
|
7
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveProductVariantFromWishlist; |
8
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
9
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantNotFoundException; |
10
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException; |
11
|
|
|
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; |
12
|
|
|
use Doctrine\Persistence\ObjectManager; |
13
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
14
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
15
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
16
|
|
|
|
17
|
|
|
final class RemoveProductVariantFromWishlistHandler implements MessageHandlerInterface |
18
|
|
|
{ |
19
|
|
|
private WishlistRepositoryInterface $wishlistRepository; |
20
|
|
|
|
21
|
|
|
private ProductVariantRepositoryInterface $productVariantRepository; |
22
|
|
|
|
23
|
|
|
private RepositoryInterface $wishlistProductRepository; |
24
|
|
|
|
25
|
|
|
private ObjectManager $wishlistManager; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
29
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
30
|
|
|
RepositoryInterface $wishlistProductRepository, |
31
|
|
|
ObjectManager $wishlistManager |
32
|
|
|
) { |
33
|
|
|
$this->wishlistRepository = $wishlistRepository; |
34
|
|
|
$this->productVariantRepository = $productVariantRepository; |
35
|
|
|
$this->wishlistProductRepository = $wishlistProductRepository; |
36
|
|
|
$this->wishlistManager = $wishlistManager; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __invoke(RemoveProductVariantFromWishlist $removeProductVariantFromWishlist): WishlistInterface |
40
|
|
|
{ |
41
|
|
|
$variantId = $removeProductVariantFromWishlist->getProductVariantIdValue(); |
42
|
|
|
$token = $removeProductVariantFromWishlist->getWishlistTokenValue(); |
43
|
|
|
|
44
|
|
|
$variant = $this->productVariantRepository->find($variantId); |
45
|
|
|
$wishlistProduct = $this->wishlistProductRepository->findOneBy(['variant' => $variant]); |
46
|
|
|
|
47
|
|
|
$wishlist = $this->wishlistRepository->findByToken($token); |
48
|
|
|
|
49
|
|
|
if (null === $variant || null === $wishlistProduct) { |
50
|
|
|
throw new ProductVariantNotFoundException( |
51
|
|
|
sprintf('The Product %s does not exist', $variantId) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (null === $wishlist) { |
56
|
|
|
throw new WishlistNotFoundException( |
57
|
|
|
sprintf('The Wishlist %s does not exist', $token) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$wishlist->removeProductVariant($variant); |
62
|
|
|
$this->wishlistManager->flush(); |
63
|
|
|
|
64
|
|
|
return $wishlist; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|