|
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\EventSubscriber; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
|
23
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
24
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class CreateNewWishlistSubscriber implements EventSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private string $wishlistCookieToken; |
|
29
|
|
|
|
|
30
|
|
|
private WishlistsResolverInterface $wishlistsResolver; |
|
31
|
|
|
|
|
32
|
|
|
private WishlistFactoryInterface $wishlistFactory; |
|
33
|
|
|
|
|
34
|
|
|
private WishlistRepositoryInterface $wishlistRepository; |
|
35
|
|
|
|
|
36
|
|
|
private TokenStorageInterface $tokenStorage; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
string $wishlistCookieToken, |
|
40
|
|
|
WishlistsResolverInterface $wishlistsResolver, |
|
41
|
|
|
WishlistFactoryInterface $wishlistFactory, |
|
42
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
|
43
|
|
|
TokenStorageInterface $tokenStorage |
|
44
|
|
|
) { |
|
45
|
|
|
$this->wishlistCookieToken = $wishlistCookieToken; |
|
46
|
|
|
$this->wishlistsResolver = $wishlistsResolver; |
|
47
|
|
|
$this->wishlistFactory = $wishlistFactory; |
|
48
|
|
|
$this->wishlistRepository = $wishlistRepository; |
|
49
|
|
|
$this->tokenStorage = $tokenStorage; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function getSubscribedEvents(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
KernelEvents::REQUEST => [['onKernelRequest', 1]], |
|
56
|
|
|
KernelEvents::RESPONSE => [['onKernelResponse', 0]], |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function onKernelRequest(RequestEvent $event): void |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$event->isMasterRequest()) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var WishlistInterface[] $wishlists */ |
|
67
|
|
|
$wishlists = $this->wishlistsResolver->resolve(); |
|
68
|
|
|
|
|
69
|
|
|
$wishlistCookieToken = $event->getRequest()->cookies->get($this->wishlistCookieToken); |
|
70
|
|
|
|
|
71
|
|
|
if ($wishlistCookieToken && !empty($wishlists)) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @var WishlistInterface $wishlist */ |
|
76
|
|
|
$wishlist = $this->createNewWishlist($wishlistCookieToken); |
|
77
|
|
|
|
|
78
|
|
|
$event->getRequest()->attributes->set($this->wishlistCookieToken, $wishlist->getToken()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function onKernelResponse(ResponseEvent $event): void |
|
82
|
|
|
{ |
|
83
|
|
|
if (!$event->isMasterRequest()) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($event->getRequest()->cookies->has($this->wishlistCookieToken)) { |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$response = $event->getResponse(); |
|
92
|
|
|
$wishlistCookieToken = $event->getRequest()->attributes->get($this->wishlistCookieToken); |
|
93
|
|
|
|
|
94
|
|
|
if (!$wishlistCookieToken) { |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
$this->setWishlistCookieToken($response, $wishlistCookieToken); |
|
98
|
|
|
|
|
99
|
|
|
$event->getRequest()->attributes->remove($this->wishlistCookieToken); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function createNewWishlist(?string $wishlistCookieToken): WishlistInterface |
|
103
|
|
|
{ |
|
104
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
|
105
|
|
|
|
|
106
|
|
|
$wishlist = $this->wishlistFactory->createNew(); |
|
107
|
|
|
|
|
108
|
|
|
if ($user instanceof ShopUserInterface) { |
|
109
|
|
|
$wishlist = $this->wishlistFactory->createForUser($user); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($wishlistCookieToken) { |
|
113
|
|
|
$wishlist->setToken($wishlistCookieToken); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$wishlist->setName('Wishlist'); |
|
117
|
|
|
$this->wishlistRepository->add($wishlist); |
|
118
|
|
|
|
|
119
|
|
|
return $wishlist; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function setWishlistCookieToken(Response $response, string $wishlistCookieToken): void |
|
123
|
|
|
{ |
|
124
|
|
|
$cookie = new Cookie($this->wishlistCookieToken, $wishlistCookieToken, strtotime('+1 year')); |
|
125
|
|
|
|
|
126
|
|
|
$response->headers->setCookie($cookie); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|