|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusReferralsPlugin\Controller\Shop; |
|
6
|
|
|
|
|
7
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateInterface; |
|
8
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateReferralInterface; |
|
9
|
|
|
use Odiseo\SyliusReferralsPlugin\Generator\AffiliateReferralGeneratorInterface; |
|
10
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateReferralRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
|
12
|
|
|
use Sylius\Component\Customer\Context\CustomerContextInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
17
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
19
|
|
|
use Twig\Environment; |
|
20
|
|
|
|
|
21
|
|
|
final class CreateFromUserAffiliateReferralAction |
|
22
|
|
|
{ |
|
23
|
|
|
private CustomerContextInterface $customerContext; |
|
24
|
|
|
private AffiliateReferralGeneratorInterface $affiliateReferralGenerator; |
|
25
|
|
|
private AffiliateReferralRepositoryInterface $affiliateReferralRepository; |
|
26
|
|
|
private RouterInterface $router; |
|
27
|
|
|
private Environment $twig; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
CustomerContextInterface $customerContext, |
|
31
|
|
|
AffiliateReferralGeneratorInterface $affiliateReferralGenerator, |
|
32
|
|
|
AffiliateReferralRepositoryInterface $affiliateReferralRepository, |
|
33
|
|
|
RouterInterface $router, |
|
34
|
|
|
Environment $twig |
|
35
|
|
|
) { |
|
36
|
|
|
$this->customerContext = $customerContext; |
|
37
|
|
|
$this->affiliateReferralGenerator = $affiliateReferralGenerator; |
|
38
|
|
|
$this->affiliateReferralRepository = $affiliateReferralRepository; |
|
39
|
|
|
$this->router = $router; |
|
40
|
|
|
$this->twig = $twig; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __invoke(Request $request): Response |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var CustomerInterface|null $customer */ |
|
46
|
|
|
$customer = $this->customerContext->getCustomer(); |
|
47
|
|
|
if (null === $customer) { |
|
48
|
|
|
throw new HttpException(Response::HTTP_UNAUTHORIZED); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!$customer instanceof AffiliateInterface) { |
|
52
|
|
|
throw new NotFoundHttpException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$affiliateReferral = $this->affiliateReferralRepository->findOneByAffiliateNotExpired($customer); |
|
56
|
|
|
if ($affiliateReferral === null) { |
|
57
|
|
|
$affiliateReferral = $this->affiliateReferralGenerator->generate($customer); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$link = $this->generateLink($affiliateReferral); |
|
61
|
|
|
|
|
62
|
|
|
$data = [ |
|
63
|
|
|
'link' => $link, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
$content = $this->twig->render( |
|
67
|
|
|
'@OdiseoSyliusReferralsPlugin/Shop/Account/AffiliateReferral/Index/Link/_template.html.twig', |
|
68
|
|
|
$data |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return new Response($content); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function generateLink(AffiliateReferralInterface $affiliateReferral): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->router->generate( |
|
77
|
|
|
'sylius_shop_homepage', |
|
78
|
|
|
[ |
|
79
|
|
|
AffiliateReferralInterface::TOKEN_PARAM_NAME => $affiliateReferral->getTokenValue() |
|
80
|
|
|
], |
|
81
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|