|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusReferralsPlugin\EventListener; |
|
6
|
|
|
|
|
7
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateInterface; |
|
8
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateReferralInterface; |
|
9
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateReferralView; |
|
10
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateReferralRepositoryInterface; |
|
11
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateReferralViewRepositoryInterface; |
|
12
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
15
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
18
|
|
|
|
|
19
|
|
|
class SetSessionFromAffiliateReferralLink |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
private TokenStorageInterface $tokenStorage, |
|
23
|
|
|
private AffiliateReferralRepositoryInterface $affiliateReferralRepository, |
|
24
|
|
|
private AffiliateReferralViewRepositoryInterface $affiliateReferralViewRepository, |
|
25
|
|
|
) { |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setSession(RequestEvent $event): void |
|
29
|
|
|
{ |
|
30
|
|
|
if ($event->getRequestType() !== HttpKernelInterface::MAIN_REQUEST) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$request = $event->getRequest(); |
|
35
|
|
|
|
|
36
|
|
|
/** @var Session $session */ |
|
37
|
|
|
$session = $request->getSession(); |
|
38
|
|
|
|
|
39
|
|
|
if (!$request->query->has(AffiliateReferralInterface::TOKEN_PARAM_NAME)) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$tokenValue = $request->query->get(AffiliateReferralInterface::TOKEN_PARAM_NAME); |
|
44
|
|
|
if (null === $tokenValue) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($session->get(AffiliateReferralInterface::TOKEN_PARAM_NAME) === $tokenValue) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @var AffiliateReferralInterface|null $affiliateReferral */ |
|
53
|
|
|
$affiliateReferral = $this->affiliateReferralRepository->findOneBy([ |
|
54
|
|
|
'tokenValue' => $tokenValue, |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
if (null === $affiliateReferral) { |
|
58
|
|
|
$session->getFlashBag()->add('error', 'The referral link is invalid!'); |
|
59
|
|
|
|
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($affiliateReferral->isExpired()) { |
|
64
|
|
|
$session->getFlashBag()->add('info', 'The link you followed has expired!'); |
|
65
|
|
|
|
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$token = $this->tokenStorage->getToken(); |
|
70
|
|
|
if (!$token instanceof TokenInterface) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$affiliateReferralView = new AffiliateReferralView(); |
|
75
|
|
|
$affiliateReferralView->setAffiliateReferral($affiliateReferral); |
|
76
|
|
|
$affiliateReferralView->setIp($request->getClientIp()); |
|
77
|
|
|
|
|
78
|
|
|
$shopUser = $token->getUser(); |
|
79
|
|
|
|
|
80
|
|
|
if ($shopUser instanceof ShopUserInterface) { |
|
81
|
|
|
$customer = $shopUser->getCustomer(); |
|
82
|
|
|
if ($customer instanceof AffiliateInterface) { |
|
83
|
|
|
if ($customer === $affiliateReferral->getAffiliate()) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->affiliateReferralViewRepository->add($affiliateReferralView); |
|
90
|
|
|
|
|
91
|
|
|
$session->set(AffiliateReferralInterface::TOKEN_PARAM_NAME, $tokenValue); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|