__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\Core\Model\ProductInterface;
13
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
14
use Sylius\Component\Customer\Context\CustomerContextInterface;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Exception\HttpException;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
use Symfony\Component\Routing\RouterInterface;
22
23
final class CreateFromProductAffiliateReferralAction
24
{
25
    public function __construct(
26
        private CustomerContextInterface $customerContext,
27
        private ProductRepositoryInterface $productRepository,
28
        private AffiliateReferralGeneratorInterface $affiliateReferralGenerator,
29
        private AffiliateReferralRepositoryInterface $affiliateReferralRepository,
30
        private RouterInterface $router,
31
    ) {
32
    }
33
34
    public function __invoke(Request $request): Response
35
    {
36
        /** @var CustomerInterface|null $customer */
37
        $customer = $this->customerContext->getCustomer();
38
        if (null === $customer) {
39
            throw new HttpException(Response::HTTP_UNAUTHORIZED);
40
        }
41
42
        if (!$customer instanceof AffiliateInterface) {
43
            throw new NotFoundHttpException();
44
        }
45
46
        $productId = $request->attributes->get('id');
47
48
        /** @var ProductInterface|null $product */
49
        $product = $this->productRepository->find($productId);
50
        if (null === $product) {
51
            throw new NotFoundHttpException();
52
        }
53
54
        $affiliateReferral = $this->affiliateReferralRepository->findOneByAffiliateAndProductNotExpired(
55
            $customer,
56
            $product,
57
        );
58
59
        if ($affiliateReferral === null) {
60
            $affiliateReferral = $this->affiliateReferralGenerator->generate($customer, $product);
61
        }
62
63
        $link = $this->generateLink($affiliateReferral);
64
65
        $data = [
66
            'link' => $link,
67
            'responseURL' => $request->getUri(),
68
        ];
69
70
        return new JsonResponse($data);
71
    }
72
73
    private function generateLink(AffiliateReferralInterface $affiliateReferral): string
74
    {
75
        /** @var ProductInterface $product */
76
        $product = $affiliateReferral->getProduct();
77
78
        return $this->router->generate(
79
            'sylius_shop_product_show',
80
            [
81
                'slug' => $product->getSlug(),
82
                AffiliateReferralInterface::TOKEN_PARAM_NAME => $affiliateReferral->getTokenValue(),
83
            ],
84
            UrlGeneratorInterface::ABSOLUTE_URL,
85
        );
86
    }
87
}
88