|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sylius\SyliusShopApiPlugin\Controller\Cart; |
|
6
|
|
|
|
|
7
|
|
|
use FOS\RestBundle\View\View; |
|
8
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
9
|
|
|
use League\Tactician\CommandBus; |
|
10
|
|
|
use Sylius\SyliusShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; |
|
11
|
|
|
use Sylius\SyliusShopApiPlugin\Request\PutOptionBasedConfigurableItemToCartRequest; |
|
12
|
|
|
use Sylius\SyliusShopApiPlugin\Request\PutSimpleItemToCartRequest; |
|
13
|
|
|
use Sylius\SyliusShopApiPlugin\Request\PutVariantBasedConfigurableItemToCartRequest; |
|
14
|
|
|
use Sylius\SyliusShopApiPlugin\ViewRepository\CartViewRepositoryInterface; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
19
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class PutItemToCartAction |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var ViewHandlerInterface */ |
|
24
|
|
|
private $viewHandler; |
|
25
|
|
|
|
|
26
|
|
|
/** @var CommandBus */ |
|
27
|
|
|
private $bus; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ValidatorInterface */ |
|
30
|
|
|
private $validator; |
|
31
|
|
|
|
|
32
|
|
|
/** @var ValidationErrorViewFactoryInterface */ |
|
33
|
|
|
private $validationErrorViewFactory; |
|
34
|
|
|
|
|
35
|
|
|
/** @var CartViewRepositoryInterface */ |
|
36
|
|
|
private $cartQuery; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
ViewHandlerInterface $viewHandler, |
|
40
|
|
|
CommandBus $bus, |
|
41
|
|
|
ValidatorInterface $validator, |
|
42
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
43
|
|
|
CartViewRepositoryInterface $cartQuery |
|
44
|
|
|
) { |
|
45
|
|
|
$this->viewHandler = $viewHandler; |
|
46
|
|
|
$this->bus = $bus; |
|
47
|
|
|
$this->validator = $validator; |
|
48
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
|
49
|
|
|
$this->cartQuery = $cartQuery; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function __invoke(Request $request): Response |
|
53
|
|
|
{ |
|
54
|
|
|
$commandRequest = $this->provideCommandRequest($request); |
|
55
|
|
|
|
|
56
|
|
|
$validationResults = $this->validator->validate($commandRequest); |
|
57
|
|
|
|
|
58
|
|
|
if (0 !== count($validationResults)) { |
|
59
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$command = $commandRequest->getCommand(); |
|
63
|
|
|
$this->bus->handle($command); |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
return $this->viewHandler->handle( |
|
67
|
|
|
View::create($this->cartQuery->getOneByToken($command->orderToken()), Response::HTTP_CREATED) |
|
68
|
|
|
); |
|
69
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
70
|
|
|
throw new BadRequestHttpException($exception->getMessage()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param Request $request |
|
76
|
|
|
* |
|
77
|
|
|
* @return PutOptionBasedConfigurableItemToCartRequest|PutSimpleItemToCartRequest|PutVariantBasedConfigurableItemToCartRequest |
|
78
|
|
|
*/ |
|
79
|
|
|
private function provideCommandRequest(Request $request) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$request->request->has('variantCode') && !$request->request->has('options')) { |
|
82
|
|
|
return PutSimpleItemToCartRequest::fromRequest($request); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($request->request->has('variantCode') && !$request->request->has('options')) { |
|
86
|
|
|
return PutVariantBasedConfigurableItemToCartRequest::fromRequest($request); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!$request->request->has('variantCode') && $request->request->has('options')) { |
|
90
|
|
|
return PutOptionBasedConfigurableItemToCartRequest::fromRequest($request); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
throw new NotFoundHttpException('Variant not found for given configuration'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|