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

RemoveItemFromCartAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
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