1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Locastic\SyliusComparerPlugin\Controller\Action; |
6
|
|
|
|
7
|
|
|
use Locastic\SyliusComparerPlugin\Entity\ComparerProductInterface; |
8
|
|
|
use Locastic\SyliusComparerPlugin\Factory\ComparerCookieFactoryInterface; |
9
|
|
|
use Locastic\SyliusComparerPlugin\Factory\ComparerProductFactoryInterface; |
10
|
|
|
use Locastic\SyliusComparerPlugin\Factory\ComparerRequestFactoryInterface; |
11
|
|
|
use Locastic\SyliusComparerPlugin\Repository\ComparerRepositoryInterface; |
12
|
|
|
use Locastic\SyliusComparerPlugin\Request\ComparerRequestInterface; |
13
|
|
|
use Locastic\SyliusComparerPlugin\Utils\ComparerCapacityCheckerInterface; |
14
|
|
|
use Locastic\SyliusComparerPlugin\Utils\MessageRedirectHelperInterface; |
15
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
16
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
21
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
22
|
|
|
|
23
|
|
|
final class AddProductToComparerAction |
24
|
|
|
{ |
25
|
|
|
/** @var ProductRepositoryInterface */ |
26
|
|
|
private $productRepository; |
27
|
|
|
|
28
|
|
|
/** @var ComparerRepositoryInterface */ |
29
|
|
|
private $comparerRepository; |
30
|
|
|
|
31
|
|
|
/** @var MessageRedirectHelperInterface */ |
32
|
|
|
private $redirectHelper; |
33
|
|
|
|
34
|
|
|
/** @var ComparerProductFactoryInterface */ |
35
|
|
|
private $comparerProductFactory; |
36
|
|
|
|
37
|
|
|
/** @var ComparerCapacityCheckerInterface */ |
38
|
|
|
private $comparerItemChecker; |
39
|
|
|
|
40
|
|
|
/** @var ComparerCookieFactoryInterface */ |
41
|
|
|
private $comparerCookieFactory; |
42
|
|
|
|
43
|
|
|
/** @var ValidatorInterface */ |
44
|
|
|
private $validator; |
45
|
|
|
|
46
|
|
|
/** @var ComparerRequestFactoryInterface */ |
47
|
|
|
private $comparerRequestFactory; |
48
|
|
|
|
49
|
|
|
public function __construct( |
50
|
|
|
ProductRepositoryInterface $productRepository, |
51
|
|
|
ComparerRepositoryInterface $comparerRepository, |
52
|
|
|
MessageRedirectHelperInterface $redirectHelper, |
53
|
|
|
ComparerProductFactoryInterface $comparerProductFactory, |
54
|
|
|
ComparerCapacityCheckerInterface $comparerItemChecker, |
55
|
|
|
ComparerCookieFactoryInterface $comparerCookieFactory, |
56
|
|
|
ValidatorInterface $validator, |
57
|
|
|
ComparerRequestFactoryInterface $comparerRequestFactory |
58
|
|
|
) { |
59
|
|
|
$this->productRepository = $productRepository; |
60
|
|
|
$this->comparerRepository = $comparerRepository; |
61
|
|
|
$this->redirectHelper = $redirectHelper; |
62
|
|
|
$this->comparerProductFactory = $comparerProductFactory; |
63
|
|
|
$this->comparerItemChecker = $comparerItemChecker; |
64
|
|
|
$this->comparerCookieFactory = $comparerCookieFactory; |
65
|
|
|
$this->validator = $validator; |
66
|
|
|
$this->comparerRequestFactory = $comparerRequestFactory; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function __invoke(Request $request): Response |
70
|
|
|
{ |
71
|
|
|
/** @var ComparerRequestInterface */ |
72
|
|
|
$addProductToComparerRequest = $this->comparerRequestFactory->create($request); |
73
|
|
|
|
74
|
|
|
/** @var ConstraintViolationListInterface $errors */ |
75
|
|
|
$errors = $this->validator->validate($addProductToComparerRequest); |
76
|
|
|
|
77
|
|
|
if (count($errors)) { |
78
|
|
|
return $this->redirectHelper->warningRedirect($errors->get(0)->getMessage(), 'locastic_sylius_comparer_list_products'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$comparer = $addProductToComparerRequest->getComparer(); |
82
|
|
|
|
83
|
|
|
/** @var ProductInterface $product */ |
84
|
|
|
$product = $this->productRepository->find($request->get('productId')); |
85
|
|
|
|
86
|
|
|
/** @var ComparerProductInterface $comparerProduct */ |
87
|
|
|
$comparerProduct = $this->comparerProductFactory->createForComparerAndProduct($comparer, $product); |
88
|
|
|
|
89
|
|
|
$comparer->addComparerProduct($comparerProduct); |
90
|
|
|
$this->comparerRepository->add($comparer); |
91
|
|
|
|
92
|
|
|
return $this->createResponseWithCookie($comparer->getToken()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function createResponseWithCookie(string $token): RedirectResponse |
96
|
|
|
{ |
97
|
|
|
$response = $this->redirectHelper->confirmationRedirect('locastic_sylius_comparer_plugin.ui.added_to_comparer', 'locastic_sylius_comparer_list_products'); |
98
|
|
|
$response->headers->setCookie($this->comparerCookieFactory->createNew($token)); |
99
|
|
|
|
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|