Passed
Push — master ( a403c4...843515 )
by Diego
07:39 queued 01:51
created

CreateFromProductAffiliateAction::generateTokenValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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