Passed
Push — master ( 843515...db168e )
by Diego
04:15
created

SetSessionFromAffiliateReferralLink   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setSession() 0 64 11
A __construct() 0 8 1
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
    private TokenStorageInterface $tokenStorage;
22
    private AffiliateReferralRepositoryInterface $affiliateReferralRepository;
23
    private AffiliateReferralViewRepositoryInterface $affiliateReferralViewRepository;
24
25
    public function __construct(
26
        TokenStorageInterface $tokenStorage,
27
        AffiliateReferralRepositoryInterface $affiliateReferralRepository,
28
        AffiliateReferralViewRepositoryInterface $affiliateReferralViewRepository
29
    ) {
30
        $this->tokenStorage = $tokenStorage;
31
        $this->affiliateReferralRepository = $affiliateReferralRepository;
32
        $this->affiliateReferralViewRepository = $affiliateReferralViewRepository;
33
    }
34
35
    public function setSession(RequestEvent $event): void
36
    {
37
        if ($event->getRequestType() !== HttpKernelInterface::MAIN_REQUEST) {
38
            return;
39
        }
40
41
        $request = $event->getRequest();
42
43
        /** @var Session $session */
44
        $session = $request->getSession();
45
46
        if (!$request->query->has(AffiliateReferralInterface::TOKEN_PARAM_NAME)) {
47
            return;
48
        }
49
50
        $tokenValue = $request->query->get(AffiliateReferralInterface::TOKEN_PARAM_NAME);
51
        if (null === $tokenValue) {
52
            return;
53
        }
54
55
        if ($session->get(AffiliateReferralInterface::TOKEN_PARAM_NAME) === $tokenValue) {
56
            return;
57
        }
58
59
        /** @var AffiliateReferralInterface|null $affiliateReferral */
60
        $affiliateReferral = $this->affiliateReferralRepository->findOneBy([
61
            'tokenValue' => $tokenValue
62
        ]);
63
64
        if (null === $affiliateReferral) {
65
            $session->getFlashBag()->add('error', 'The referral link is invalid!');
66
67
            return;
68
        }
69
70
        if ($affiliateReferral->isExpired()) {
71
            $session->getFlashBag()->add('info', 'The link you followed has expired!');
72
73
            return;
74
        }
75
76
        $token = $this->tokenStorage->getToken();
77
        if (!$token instanceof TokenInterface) {
78
            return;
79
        }
80
81
        $affiliateReferralView = new AffiliateReferralView();
82
        $affiliateReferralView->setAffiliateReferral($affiliateReferral);
83
        $affiliateReferralView->setIp($request->getClientIp());
84
85
        $shopUser = $token->getUser();
86
87
        if ($shopUser instanceof ShopUserInterface) {
88
            $customer = $shopUser->getCustomer();
89
            if ($customer instanceof AffiliateInterface) {
90
                if ($customer === $affiliateReferral->getAffiliate()) {
91
                    return;
92
                }
93
            }
94
        }
95
96
        $this->affiliateReferralViewRepository->add($affiliateReferralView);
97
98
        $session->set(AffiliateReferralInterface::TOKEN_PARAM_NAME, $tokenValue);
99
    }
100
}
101