__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
    public function __construct(
24
        private CustomerContextInterface $customerContext,
25
        private AffiliateReferralGeneratorInterface $affiliateReferralGenerator,
26
        private AffiliateReferralRepositoryInterface $affiliateReferralRepository,
27
        private RouterInterface $router,
28
        private Environment $twig,
29
    ) {
30
    }
31
32
    public function __invoke(Request $request): Response
33
    {
34
        /** @var CustomerInterface|null $customer */
35
        $customer = $this->customerContext->getCustomer();
36
        if (null === $customer) {
37
            throw new HttpException(Response::HTTP_UNAUTHORIZED);
38
        }
39
40
        if (!$customer instanceof AffiliateInterface) {
41
            throw new NotFoundHttpException();
42
        }
43
44
        $affiliateReferral = $this->affiliateReferralRepository->findOneByAffiliateNotExpired($customer);
45
        if ($affiliateReferral === null) {
46
            $affiliateReferral = $this->affiliateReferralGenerator->generate($customer);
47
        }
48
49
        $link = $this->generateLink($affiliateReferral);
50
51
        $data = [
52
            'link' => $link,
53
        ];
54
55
        $content = $this->twig->render(
56
            '@OdiseoSyliusReferralsPlugin/Shop/Account/AffiliateReferral/Index/Link/_template.html.twig',
57
            $data,
58
        );
59
60
        return new Response($content);
61
    }
62
63
    private function generateLink(AffiliateReferralInterface $affiliateReferral): string
64
    {
65
        return $this->router->generate(
66
            'sylius_shop_homepage',
67
            [
68
                AffiliateReferralInterface::TOKEN_PARAM_NAME => $affiliateReferral->getTokenValue(),
69
            ],
70
            UrlGeneratorInterface::ABSOLUTE_URL,
71
        );
72
    }
73
}
74