1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Controller\Cart; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
6
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
7
|
|
|
use League\Tactician\CommandBus; |
8
|
|
|
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; |
9
|
|
|
use Sylius\ShopApiPlugin\Request\DropCartRequest; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
13
|
|
|
|
14
|
|
|
final class DropCartAction |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ViewHandlerInterface |
18
|
|
|
*/ |
19
|
|
|
private $viewHandler; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var CommandBus |
23
|
|
|
*/ |
24
|
|
|
private $bus; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ValidatorInterface |
28
|
|
|
*/ |
29
|
|
|
private $validator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ValidationErrorViewFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $validationErrorViewFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ViewHandlerInterface $viewHandler |
38
|
|
|
* @param CommandBus $bus |
39
|
|
|
* @param ValidatorInterface $validator |
40
|
|
|
* @param ValidationErrorViewFactoryInterface $validationErrorViewFactory |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
ViewHandlerInterface $viewHandler, |
44
|
|
|
CommandBus $bus, |
45
|
|
|
ValidatorInterface $validator, |
46
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory |
47
|
|
|
) { |
48
|
|
|
$this->viewHandler = $viewHandler; |
49
|
|
|
$this->bus = $bus; |
50
|
|
|
$this->validator = $validator; |
51
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* |
57
|
|
|
* @return Response |
58
|
|
|
*/ |
59
|
|
|
public function __invoke(Request $request) |
60
|
|
|
{ |
61
|
|
|
$pickupRequest = new DropCartRequest($request); |
62
|
|
|
|
63
|
|
|
$validationResults = $this->validator->validate($pickupRequest); |
64
|
|
|
|
65
|
|
|
if (0 === count($validationResults)) { |
66
|
|
|
$this->bus->handle($pickupRequest->getCommand()); |
67
|
|
|
|
68
|
|
|
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|