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\Generator\AffiliateGeneratorInterface; |
9
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateRepositoryInterface; |
10
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
11
|
|
|
use Sylius\Component\Customer\Context\CustomerContextInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
15
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
use Twig\Environment; |
18
|
|
|
|
19
|
|
|
final class CreateFromUserAffiliateAction |
20
|
|
|
{ |
21
|
|
|
private CustomerContextInterface $customerContext; |
22
|
|
|
private AffiliateGeneratorInterface $affiliateGenerator; |
23
|
|
|
private AffiliateRepositoryInterface $affiliateRepository; |
24
|
|
|
private RouterInterface $router; |
25
|
|
|
private Environment $twig; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
CustomerContextInterface $customerContext, |
29
|
|
|
AffiliateGeneratorInterface $affiliateGenerator, |
30
|
|
|
AffiliateRepositoryInterface $affiliateRepository, |
31
|
|
|
RouterInterface $router, |
32
|
|
|
Environment $twig |
33
|
|
|
) { |
34
|
|
|
$this->customerContext = $customerContext; |
35
|
|
|
$this->affiliateGenerator = $affiliateGenerator; |
36
|
|
|
$this->affiliateRepository = $affiliateRepository; |
37
|
|
|
$this->router = $router; |
38
|
|
|
$this->twig = $twig; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function __invoke(Request $request): Response |
42
|
|
|
{ |
43
|
|
|
/** @var CustomerInterface|null $customer */ |
44
|
|
|
$customer = $this->customerContext->getCustomer(); |
45
|
|
|
if (null === $customer) { |
46
|
|
|
throw new HttpException(Response::HTTP_UNAUTHORIZED); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$affiliate = $this->affiliateRepository->findOneByCustomerNotExpired($customer); |
50
|
|
|
if ($affiliate === null) { |
51
|
|
|
$affiliate = $this->affiliateGenerator->generate($customer); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$link = $this->generateLink($affiliate); |
55
|
|
|
|
56
|
|
|
$data = [ |
57
|
|
|
'link' => $link, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$content = $this->twig->render( |
61
|
|
|
'@OdiseoSyliusReferralsPlugin/Shop/Account/Affiliate/Index/Link/_template.html.twig', |
62
|
|
|
$data |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return new Response($content); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function generateLink(AffiliateInterface $affiliate): string |
69
|
|
|
{ |
70
|
|
|
return $this->router->generate( |
71
|
|
|
'sylius_shop_homepage', |
72
|
|
|
[ |
73
|
|
|
AffiliateInterface::TOKEN_PARAM_NAME => $affiliate->getTokenValue() |
74
|
|
|
], |
75
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|