Completed
Push — master ( 6ad5a2...634c9e )
by Łukasz
12s
created

RemoveItemFromCartAction::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 1
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\ChangeItemQuantityRequest;
10
use Sylius\ShopApiPlugin\Request\RemoveItemFromCartRequest;
11
use Sylius\ShopApiPlugin\ViewRepository\CartViewRepositoryInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17 View Code Duplication
final class RemoveItemFromCartAction
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
18
{
19
    /** @var ViewHandlerInterface */
20
    private $viewHandler;
21
22
    /** @var CommandBus */
23
    private $bus;
24
25
    /** @var ValidatorInterface */
26
    private $validator;
27
28
    /** @var ValidationErrorViewFactoryInterface */
29
    private $validationErrorViewFactory;
30
31
    /** @var CartViewRepositoryInterface */
32
    private $cartQuery;
33
34
    public function __construct(
35
        ViewHandlerInterface $viewHandler,
36
        CommandBus $bus,
37
        ValidatorInterface $validator,
38
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
39
        CartViewRepositoryInterface $cartQuery
40
    ) {
41
        $this->viewHandler = $viewHandler;
42
        $this->bus = $bus;
43
        $this->validator = $validator;
44
        $this->validationErrorViewFactory = $validationErrorViewFactory;
45
        $this->cartQuery = $cartQuery;
46
    }
47
48
    public function __invoke(Request $request): Response
49
    {
50
        $removeItemFromCartRequest = new RemoveItemFromCartRequest($request);
51
52
        $validationResults = $this->validator->validate($removeItemFromCartRequest);
53
54
        if (0 !== count($validationResults)) {
55
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
56
        }
57
58
        $removeItemFromCartCommand = $removeItemFromCartRequest->getCommand();
59
60
        $this->bus->handle($removeItemFromCartCommand);
61
62
        try {
63
            return $this->viewHandler->handle(
64
                View::create($this->cartQuery->getOneByToken($removeItemFromCartCommand->orderToken()), Response::HTTP_OK)
65
            );
66
        } catch (\InvalidArgumentException $exception) {
67
            throw new BadRequestHttpException($exception->getMessage());
68
        }
69
    }
70
}
71