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