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\CreateWishlist; |
14
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
15
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; |
16
|
|
|
use BitBag\SyliusWishlistPlugin\Resolver\ShopUserWishlistResolverInterface; |
17
|
|
|
use Doctrine\Persistence\ObjectManager; |
18
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
19
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
22
|
|
|
|
23
|
|
|
final class CreateWishlistHandler implements MessageHandlerInterface |
24
|
|
|
{ |
25
|
|
|
private TokenStorageInterface $tokenStorage; |
26
|
|
|
|
27
|
|
|
private WishlistFactoryInterface $wishlistFactory; |
28
|
|
|
|
29
|
|
|
private ShopUserWishlistResolverInterface $shopUserWishlistResolver; |
30
|
|
|
|
31
|
|
|
private ObjectManager $wishlistManager; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
TokenStorageInterface $tokenStorage, |
35
|
|
|
WishlistFactoryInterface $wishlistFactory, |
36
|
|
|
ShopUserWishlistResolverInterface $shopUserWishlistResolver, |
37
|
|
|
ObjectManager $wishlistManager |
38
|
|
|
) { |
39
|
|
|
$this->tokenStorage = $tokenStorage; |
40
|
|
|
$this->wishlistFactory = $wishlistFactory; |
41
|
|
|
$this->shopUserWishlistResolver = $shopUserWishlistResolver; |
42
|
|
|
$this->wishlistManager = $wishlistManager; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __invoke(CreateWishlist $createWishlist): WishlistInterface |
46
|
|
|
{ |
47
|
|
|
/** @var ?TokenInterface $token */ |
48
|
|
|
$token = $this->tokenStorage->getToken(); |
49
|
|
|
|
50
|
|
|
$user = null !== $token ? $token->getUser() : null; |
51
|
|
|
|
52
|
|
|
/** @var WishlistInterface $wishlist */ |
53
|
|
|
$wishlist = $this->wishlistFactory->createNew(); |
54
|
|
|
|
55
|
|
|
if ($user instanceof ShopUserInterface) { |
56
|
|
|
$wishlist = $this->shopUserWishlistResolver->resolve($user); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->wishlistManager->persist($wishlist); |
60
|
|
|
$this->wishlistManager->flush(); |
61
|
|
|
|
62
|
|
|
return $wishlist; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|