|
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\Repository\WishlistRepositoryInterface; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductToSelectedWishlist; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface; |
|
17
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface; |
|
18
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
final class AddProductToSelectedWishlistHandler implements MessageHandlerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
private WishlistProductFactoryInterface $wishlistProductFactory; |
|
24
|
|
|
|
|
25
|
|
|
private WishlistRepositoryInterface $wishlistRepository; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
WishlistProductFactoryInterface $wishlistProductFactory, |
|
29
|
|
|
WishlistRepositoryInterface $wishlistRepository |
|
30
|
|
|
) { |
|
31
|
|
|
$this->wishlistProductFactory = $wishlistProductFactory; |
|
32
|
|
|
$this->wishlistRepository = $wishlistRepository; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function __invoke(AddProductToSelectedWishlist $addProductToSelectedWishlist) |
|
36
|
|
|
{ |
|
37
|
|
|
$product = $addProductToSelectedWishlist->getProduct(); |
|
38
|
|
|
$wishlist = $addProductToSelectedWishlist->getWishlist(); |
|
39
|
|
|
|
|
40
|
|
|
if (null === $product) { |
|
41
|
|
|
throw new NotFoundHttpException(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @var WishlistProductInterface $wishlistProduct */ |
|
45
|
|
|
$wishlistProduct = $this->wishlistProductFactory->createForWishlistAndProduct($wishlist, $product); |
|
46
|
|
|
|
|
47
|
|
|
$wishlist->addWishlistProduct($wishlistProduct); |
|
48
|
|
|
$this->wishlistRepository->add($wishlist); |
|
49
|
|
|
} |
|
50
|
|
|
} |