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

CreateFromProductAffiliateReferralAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A __invoke() 0 37 5
A generateLink() 0 12 1
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
    private CustomerContextInterface $customerContext;
26
    private ProductRepositoryInterface $productRepository;
27
    private AffiliateReferralGeneratorInterface $affiliateReferralGenerator;
28
    private AffiliateReferralRepositoryInterface $affiliateReferralRepository;
29
    private RouterInterface $router;
30
31
    public function __construct(
32
        CustomerContextInterface $customerContext,
33
        ProductRepositoryInterface $productRepository,
34
        AffiliateReferralGeneratorInterface $affiliateReferralGenerator,
35
        AffiliateReferralRepositoryInterface $affiliateReferralRepository,
36
        RouterInterface $router
37
    ) {
38
        $this->customerContext = $customerContext;
39
        $this->productRepository = $productRepository;
40
        $this->affiliateReferralGenerator = $affiliateReferralGenerator;
41
        $this->affiliateReferralRepository = $affiliateReferralRepository;
42
        $this->router = $router;
43
    }
44
45
    public function __invoke(Request $request): Response
46
    {
47
        /** @var CustomerInterface|null $customer */
48
        $customer = $this->customerContext->getCustomer();
49
        if (null === $customer) {
50
            throw new HttpException(Response::HTTP_UNAUTHORIZED);
51
        }
52
53
        if (!$customer instanceof AffiliateInterface) {
54
            throw new NotFoundHttpException();
55
        }
56
57
        $productId = $request->attributes->get('id');
58
59
        /** @var ProductInterface|null $product */
60
        $product = $this->productRepository->find($productId);
61
        if (null === $product) {
62
            throw new NotFoundHttpException();
63
        }
64
65
        $affiliateReferral = $this->affiliateReferralRepository->findOneByAffiliateAndProductNotExpired(
66
            $customer,
67
            $product
68
        );
69
70
        if ($affiliateReferral === null) {
71
            $affiliateReferral = $this->affiliateReferralGenerator->generate($customer, $product);
72
        }
73
74
        $link = $this->generateLink($affiliateReferral);
75
76
        $data = [
77
            'link' => $link,
78
            'responseURL' => $request->getUri(),
79
        ];
80
81
        return new JsonResponse($data);
82
    }
83
84
    private function generateLink(AffiliateReferralInterface $affiliateReferral): string
85
    {
86
        /** @var ProductInterface $product */
87
        $product = $affiliateReferral->getProduct();
88
89
        return $this->router->generate(
90
            'sylius_shop_product_show',
91
            [
92
                'slug' => $product->getSlug(),
93
                AffiliateReferralInterface::TOKEN_PARAM_NAME => $affiliateReferral->getTokenValue()
94
            ],
95
            UrlGeneratorInterface::ABSOLUTE_URL
96
        );
97
    }
98
}
99