1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Locastic\SyliusComparerPlugin\Controller\Action; |
6
|
|
|
|
7
|
|
|
use Locastic\SyliusComparerPlugin\Entity\ComparerInterface; |
8
|
|
|
use Locastic\SyliusComparerPlugin\Factory\ComparerRequestFactoryInterface; |
9
|
|
|
use Locastic\SyliusComparerPlugin\Request\ComparerRequestInterface; |
10
|
|
|
use Locastic\SyliusComparerPlugin\Utils\MessageRedirectHelperInterface; |
11
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
12
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
13
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
16
|
|
|
|
17
|
|
|
final class RemoveProductFromComparerAction |
18
|
|
|
{ |
19
|
|
|
/** @var RepositoryInterface */ |
20
|
|
|
private $comparerProductRepository; |
21
|
|
|
|
22
|
|
|
/** @var MessageRedirectHelperInterface */ |
23
|
|
|
private $redirectHelper; |
24
|
|
|
|
25
|
|
|
/** @var ProductRepositoryInterface */ |
26
|
|
|
private $productRepository; |
27
|
|
|
|
28
|
|
|
/** @var ValidatorInterface */ |
29
|
|
|
private $validator; |
30
|
|
|
|
31
|
|
|
/** @var ComparerRequestFactoryInterface */ |
32
|
|
|
private $comparerRequestFactory; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
RepositoryInterface $comparerProductRepository, |
36
|
|
|
MessageRedirectHelperInterface $redirectHelper, |
37
|
|
|
ProductRepositoryInterface $productRepository, |
38
|
|
|
ValidatorInterface $validator, |
39
|
|
|
ComparerRequestFactoryInterface $comparerRequestFactory |
40
|
|
|
) { |
41
|
|
|
$this->comparerProductRepository = $comparerProductRepository; |
42
|
|
|
$this->redirectHelper = $redirectHelper; |
43
|
|
|
$this->productRepository = $productRepository; |
44
|
|
|
$this->validator = $validator; |
45
|
|
|
$this->comparerRequestFactory = $comparerRequestFactory; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function __invoke(Request $request) |
49
|
|
|
{ |
50
|
|
|
/** @var ComparerRequestInterface */ |
51
|
|
|
$removeRequest = $this->comparerRequestFactory->create($request); |
52
|
|
|
|
53
|
|
|
$errors = $this->validator->validate($removeRequest); |
54
|
|
|
|
55
|
|
|
if (count($errors)) { |
56
|
|
|
return $this->redirectHelper->warningRedirect($errors->get(0)->getMessage(), 'locastic_sylius_comparer_list_products'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @var ComparerInterface */ |
60
|
|
|
$comparer = $removeRequest->getComparer(); |
61
|
|
|
|
62
|
|
|
/** @var ProductInterface $product */ |
63
|
|
|
$product = $this->productRepository->find($request->get('productId')); |
64
|
|
|
|
65
|
|
|
$this->comparerProductRepository->remove($comparer->getComparerProduct($product)); |
|
|
|
|
66
|
|
|
|
67
|
|
|
return $this->redirectHelper->confirmationRedirect('locastic_sylius_comparer_plugin.ui.removed_from_comparer', 'locastic_sylius_comparer_list_products'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|