1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Controller\Cart; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface; |
16
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface; |
17
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface; |
18
|
|
|
use FOS\RestBundle\View\View; |
19
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
23
|
|
|
|
24
|
|
|
final class DeleteCartAction |
25
|
|
|
{ |
26
|
|
|
/** @var RequestProcessorInterface */ |
27
|
|
|
private $deleteCartRequestProcessor; |
28
|
|
|
|
29
|
|
|
/** @var MessageBusInterface */ |
30
|
|
|
private $bus; |
31
|
|
|
|
32
|
|
|
/** @var ViewHandlerInterface */ |
33
|
|
|
private $viewHandler; |
34
|
|
|
|
35
|
|
|
/** @var ValidationErrorViewFactoryInterface */ |
36
|
|
|
private $validationErrorViewFactory; |
37
|
|
|
|
38
|
|
|
/** @var GenericSuccessViewFactoryInterface */ |
39
|
|
|
private $genericSuccessViewFactory; |
40
|
|
|
|
41
|
|
|
public function __construct( |
42
|
|
|
RequestProcessorInterface $deleteCartRequestProcessor, |
43
|
|
|
MessageBusInterface $bus, |
44
|
|
|
ViewHandlerInterface $viewHandler, |
45
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
46
|
|
|
GenericSuccessViewFactoryInterface $genericSuccessViewFactory |
47
|
|
|
) { |
48
|
|
|
$this->deleteCartRequestProcessor = $deleteCartRequestProcessor; |
49
|
|
|
$this->bus = $bus; |
50
|
|
|
$this->viewHandler = $viewHandler; |
51
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
52
|
|
|
$this->genericSuccessViewFactory = $genericSuccessViewFactory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function __invoke(Request $request): Response |
56
|
|
|
{ |
57
|
|
|
$validationResults = $this->deleteCartRequestProcessor->validate($request); |
58
|
|
|
|
59
|
|
|
if (0 !== count($validationResults)) { |
60
|
|
|
return $this->viewHandler->handle(View::create( |
61
|
|
|
$this->validationErrorViewFactory->create($validationResults), |
62
|
|
|
Response::HTTP_BAD_REQUEST |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->bus->dispatch($this->deleteCartRequestProcessor->getCommand($request)); |
67
|
|
|
|
68
|
|
|
return $this->viewHandler->handle(View::create( |
69
|
|
|
$this->genericSuccessViewFactory->create(true) |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|