|
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\CreateNewWishlist; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
19
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class CreateNewWishlistHandler implements MessageHandlerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private WishlistRepositoryInterface $wishlistRepository; |
|
25
|
|
|
|
|
26
|
|
|
private TokenStorageInterface $tokenStorage; |
|
27
|
|
|
|
|
28
|
|
|
private WishlistFactoryInterface $wishlistFactory; |
|
29
|
|
|
|
|
30
|
|
|
private RequestStack $requestStack; |
|
31
|
|
|
|
|
32
|
|
|
private string $wishlistCookieToken; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
|
36
|
|
|
TokenStorageInterface $tokenStorage, |
|
37
|
|
|
WishlistFactoryInterface $wishlistFactory, |
|
38
|
|
|
RequestStack $requestStack, |
|
39
|
|
|
string $wishlistCookieToken |
|
40
|
|
|
) { |
|
41
|
|
|
$this->wishlistRepository = $wishlistRepository; |
|
42
|
|
|
$this->tokenStorage = $tokenStorage; |
|
43
|
|
|
$this->wishlistFactory = $wishlistFactory; |
|
44
|
|
|
$this->requestStack = $requestStack; |
|
45
|
|
|
$this->wishlistCookieToken = $wishlistCookieToken; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function __invoke(CreateNewWishlist $createNewWishlist): WishlistInterface |
|
49
|
|
|
{ |
|
50
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
|
51
|
|
|
|
|
52
|
|
|
if ($user instanceof ShopUserInterface) { |
|
53
|
|
|
$wishlist = $this->wishlistFactory->createForUser($user); |
|
54
|
|
|
} else { |
|
55
|
|
|
$wishlist = $this->wishlistFactory->createNew(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$mainRequest = $this->requestStack->getMasterRequest(); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if ($mainRequest->cookies->get($this->wishlistCookieToken)) { |
|
61
|
|
|
$wishlist->setToken($mainRequest->cookies->get($this->wishlistCookieToken)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$wishlist->setName($createNewWishlist->getName()); |
|
65
|
|
|
$this->wishlistRepository->add($wishlist); |
|
66
|
|
|
|
|
67
|
|
|
return $wishlist; |
|
68
|
|
|
} |
|
69
|
|
|
} |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.