1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace SyliusCart\CartBundle\Controller; |
6
|
|
|
|
7
|
|
|
use Broadway\CommandHandling\CommandBusInterface; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
use SyliusCart\Domain\Command\AddProductToCart; |
10
|
|
|
use SyliusCart\Domain\Command\ChangeProductQuantity; |
11
|
|
|
use SyliusCart\Domain\Command\ClearCart; |
12
|
|
|
use SyliusCart\Domain\Command\InitializeCart; |
13
|
|
|
use SyliusCart\Domain\Command\RemoveProductFromCart; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class CartController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var CommandBusInterface |
25
|
|
|
*/ |
26
|
|
|
private $commandBus; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param CommandBusInterface $commandBus |
30
|
|
|
*/ |
31
|
|
|
public function __construct(CommandBusInterface $commandBus) |
32
|
|
|
{ |
33
|
|
|
$this->commandBus = $commandBus; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Request $request |
38
|
|
|
* |
39
|
|
|
* @return Response |
40
|
|
|
* |
41
|
|
|
* @throws HttpException |
42
|
|
|
*/ |
43
|
|
|
public function initializeAction(Request $request): Response |
44
|
|
|
{ |
45
|
|
|
$cartId = Uuid::uuid4(); |
46
|
|
|
|
47
|
|
|
$this->tryToHandleCommand(InitializeCart::create($cartId, $request->request->get('currencyCode'))); |
48
|
|
|
|
49
|
|
|
return new Response($cartId); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Request $request |
54
|
|
|
* |
55
|
|
|
* @return Response |
56
|
|
|
* |
57
|
|
|
* @throws HttpException |
58
|
|
|
*/ |
59
|
|
|
public function addProductAction(Request $request): Response |
60
|
|
|
{ |
61
|
|
|
$this->tryToHandleCommand(AddProductToCart::create( |
62
|
|
|
$request->request->get('cartId'), |
63
|
|
|
$request->request->get('productCode'), |
64
|
|
|
(int) $request->request->get('quantity'), |
65
|
|
|
(int) $request->request->get('price'), |
66
|
|
|
$request->request->get('productCurrencyCode') |
67
|
|
|
)); |
68
|
|
|
|
69
|
|
|
return new Response(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Request $request |
74
|
|
|
* |
75
|
|
|
* @return Response |
76
|
|
|
* |
77
|
|
|
* @throws HttpException |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function changeProductQuantityAction(Request $request): Response |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this->tryToHandleCommand(ChangeProductQuantity::create( |
82
|
|
|
$request->request->get('cartId'), |
83
|
|
|
$request->request->get('productCode'), |
84
|
|
|
(int) $request->request->get('quantity') |
85
|
|
|
)); |
86
|
|
|
|
87
|
|
|
return new Response(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Request $request |
92
|
|
|
* |
93
|
|
|
* @return Response |
94
|
|
|
* |
95
|
|
|
* @throws HttpException |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function removeProductAction(Request $request): Response |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->tryToHandleCommand(RemoveProductFromCart::create( |
100
|
|
|
$request->request->get('cartId'), |
101
|
|
|
$request->request->get('productCode') |
102
|
|
|
)); |
103
|
|
|
|
104
|
|
|
return new Response(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Request $request |
109
|
|
|
* |
110
|
|
|
* @return Response |
111
|
|
|
* |
112
|
|
|
* @throws HttpException |
113
|
|
|
*/ |
114
|
|
|
public function clearAction(Request $request): Response |
115
|
|
|
{ |
116
|
|
|
$this->tryToHandleCommand(ClearCart::create($request->request->get('cartId'))); |
117
|
|
|
|
118
|
|
|
return new Response(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $command |
123
|
|
|
* |
124
|
|
|
* @throws HttpException |
125
|
|
|
*/ |
126
|
|
|
private function tryToHandleCommand($command) |
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
$this->commandBus->dispatch($command); |
130
|
|
|
} catch (\DomainException $exception) { |
131
|
|
|
throw new HttpException(Response::HTTP_BAD_REQUEST, $exception->getMessage(), $exception); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.