|
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\Persistence\ObjectManager; |
|
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 ObjectManager */ |
|
32
|
|
|
private $wishlistManager; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $wishlistCookieToken; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
|
39
|
|
|
WishlistFactoryInterface $wishlistFactory, |
|
40
|
|
|
ObjectManager $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
|
|
|
|
|
53
|
|
|
if (!$user instanceof ShopUserInterface) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->resolveWishlist($interactiveLoginEvent->getRequest(), $user); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function resolveWishlist(Request $request, ShopUserInterface $shopUser): void |
|
61
|
|
|
{ |
|
62
|
|
|
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken, ''); |
|
63
|
|
|
|
|
64
|
|
|
/** @var WishlistInterface|null $cookieWishlist */ |
|
65
|
|
|
$cookieWishlist = $this->wishlistRepository->findByToken($cookieWishlistToken); |
|
66
|
|
|
|
|
67
|
|
|
if (null === $cookieWishlist) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$userWishlist = $this->wishlistRepository->findOneByShopUser($shopUser); |
|
72
|
|
|
|
|
73
|
|
|
if (null !== $userWishlist) { |
|
74
|
|
|
foreach ($cookieWishlist->getWishlistProducts() as $wishlistProduct) { |
|
75
|
|
|
$userWishlist->addWishlistProduct($wishlistProduct); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (null === $userWishlist) { |
|
80
|
|
|
$cookieWishlist->setShopUser($shopUser); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->wishlistManager->flush(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|