Completed
Push — master ( 10a92d...cbdd73 )
by Łukasz
25:09 queued 19:28
created

RemoveCouponAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

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