Completed
Pull Request — master (#290)
by
unknown
36:49 queued 34:03
created

DropCartAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Controller\Cart;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use League\Tactician\CommandBus;
10
use Sylius\SyliusShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
11
use Sylius\SyliusShopApiPlugin\Request\DropCartRequest;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16 View Code Duplication
final class DropCartAction
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...
17
{
18
    /**
19
     * @var ViewHandlerInterface
20
     */
21
    private $viewHandler;
22
23
    /**
24
     * @var CommandBus
25
     */
26
    private $bus;
27
28
    /**
29
     * @var ValidatorInterface
30
     */
31
    private $validator;
32
33
    /**
34
     * @var ValidationErrorViewFactoryInterface
35
     */
36
    private $validationErrorViewFactory;
37
38
    /**
39
     * @param ViewHandlerInterface $viewHandler
40
     * @param CommandBus $bus
41
     * @param ValidatorInterface $validator
42
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
43
     */
44
    public function __construct(
45
        ViewHandlerInterface $viewHandler,
46
        CommandBus $bus,
47
        ValidatorInterface $validator,
48
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
49
    ) {
50
        $this->viewHandler = $viewHandler;
51
        $this->bus = $bus;
52
        $this->validator = $validator;
53
        $this->validationErrorViewFactory = $validationErrorViewFactory;
54
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @return Response
60
     */
61
    public function __invoke(Request $request)
62
    {
63
        $pickupRequest = new DropCartRequest($request);
64
65
        $validationResults = $this->validator->validate($pickupRequest);
66
67
        if (0 === count($validationResults)) {
68
            $this->bus->handle($pickupRequest->getCommand());
69
70
            return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
71
        }
72
73
        return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
74
    }
75
}
76