|
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\Context; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; |
|
16
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
19
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class WishlistContext implements WishlistContextInterface |
|
22
|
|
|
{ |
|
23
|
|
|
private TokenStorageInterface $tokenStorage; |
|
24
|
|
|
|
|
25
|
|
|
private WishlistRepositoryInterface $wishlistRepository; |
|
26
|
|
|
|
|
27
|
|
|
private WishlistFactoryInterface $wishlistFactory; |
|
28
|
|
|
|
|
29
|
|
|
private string $wishlistCookieToken; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
TokenStorageInterface $tokenStorage, |
|
33
|
|
|
WishlistRepositoryInterface $wishlistRepository, |
|
34
|
|
|
WishlistFactoryInterface $wishlistFactory, |
|
35
|
|
|
string $wishlistCookieToken |
|
36
|
|
|
) { |
|
37
|
|
|
$this->tokenStorage = $tokenStorage; |
|
38
|
|
|
$this->wishlistRepository = $wishlistRepository; |
|
39
|
|
|
$this->wishlistFactory = $wishlistFactory; |
|
40
|
|
|
$this->wishlistCookieToken = $wishlistCookieToken; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getWishlist(Request $request): WishlistInterface |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var ?string $cookieWishlistToken */ |
|
46
|
|
|
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken); |
|
47
|
|
|
|
|
48
|
|
|
/** @var ?TokenInterface $token */ |
|
49
|
|
|
$token = $this->tokenStorage->getToken(); |
|
50
|
|
|
|
|
51
|
|
|
/** @var WishlistInterface $wishlist */ |
|
52
|
|
|
$wishlist = $this->wishlistFactory->createNew(); |
|
53
|
|
|
|
|
54
|
|
|
$user = null !== $token ? $token->getUser() : null; |
|
55
|
|
|
|
|
56
|
|
|
if (null === $cookieWishlistToken && null === $user) { |
|
57
|
|
|
return $wishlist; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (null !== $cookieWishlistToken && !$user instanceof ShopUserInterface) { |
|
61
|
|
|
return null !== $this->wishlistRepository->findByToken($cookieWishlistToken) ? |
|
|
|
|
|
|
62
|
|
|
$this->wishlistRepository->findByToken($cookieWishlistToken) : |
|
63
|
|
|
$wishlist |
|
64
|
|
|
; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($user instanceof ShopUserInterface) { |
|
68
|
|
|
return null !== $this->wishlistRepository->findOneByShopUser($user) ? |
|
|
|
|
|
|
69
|
|
|
$this->wishlistRepository->findOneByShopUser($user) : |
|
70
|
|
|
$this->wishlistFactory->createForUser($user) |
|
71
|
|
|
; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $wishlist; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|