1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusWishlistPlugin\EventListener; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
16
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; |
17
|
|
|
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
22
|
|
|
|
23
|
|
|
final class MergeUserWishlistItemsListener |
24
|
|
|
{ |
25
|
|
|
/** @var WishlistRepositoryInterface */ |
26
|
|
|
private $wishlistRepository; |
27
|
|
|
|
28
|
|
|
/** @var WishlistFactoryInterface */ |
29
|
|
|
private $wishlistFactory; |
30
|
|
|
|
31
|
|
|
/** @var EntityManagerInterface */ |
32
|
|
|
private $wishlistManager; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $wishlistCookieToken; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
39
|
|
|
WishlistFactoryInterface $wishlistFactory, |
40
|
|
|
EntityManagerInterface $wishlistManager, |
41
|
|
|
string $wishlistCookieToken |
42
|
|
|
) { |
43
|
|
|
$this->wishlistRepository = $wishlistRepository; |
44
|
|
|
$this->wishlistFactory = $wishlistFactory; |
45
|
|
|
$this->wishlistManager = $wishlistManager; |
46
|
|
|
$this->wishlistCookieToken = $wishlistCookieToken; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void |
50
|
|
|
{ |
51
|
|
|
$user = $interactiveLoginEvent->getAuthenticationToken()->getUser(); |
52
|
|
|
if (!$user instanceof ShopUserInterface) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->resolveWishlist($interactiveLoginEvent->getRequest(), $user); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function resolveWishlist(Request $request, ShopUserInterface $shopUser): void |
60
|
|
|
{ |
61
|
|
|
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken, ''); |
62
|
|
|
|
63
|
|
|
$cookieWishlist = $this->wishlistRepository->findByToken($cookieWishlistToken); |
64
|
|
|
$userWishlist = $this->wishlistRepository->findByShopUser($shopUser); |
65
|
|
|
|
66
|
|
|
if (null === $cookieWishlist) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (null !== $userWishlist) { |
71
|
|
|
foreach ($cookieWishlist->getWishlistProducts() as $wishlistProduct) { |
72
|
|
|
$userWishlist->addWishlistProduct($wishlistProduct); |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
$cookieWishlist->setShopUser($shopUser); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->wishlistManager->flush(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|