Completed
Push — master ( b6acfe...4d9bc8 )
by Paweł
03:29
created

DropCartAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 14 2
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\DropCartRequest;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Validator\Validator\ValidatorInterface;
13
14
final class DropCartAction
15
{
16
    /**
17
     * @var ViewHandlerInterface
18
     */
19
    private $viewHandler;
20
21
    /**
22
     * @var CommandBus
23
     */
24
    private $bus;
25
26
    /**
27
     * @var ValidatorInterface
28
     */
29
    private $validator;
30
31
    /**
32
     * @var ValidationErrorViewFactoryInterface
33
     */
34
    private $validationErrorViewFactory;
35
36
    /**
37
     * @param ViewHandlerInterface $viewHandler
38
     * @param CommandBus $bus
39
     * @param ValidatorInterface $validator
40
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
41
     */
42
    public function __construct(
43
        ViewHandlerInterface $viewHandler,
44
        CommandBus $bus,
45
        ValidatorInterface $validator,
46
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
47
    ) {
48
        $this->viewHandler = $viewHandler;
49
        $this->bus = $bus;
50
        $this->validator = $validator;
51
        $this->validationErrorViewFactory = $validationErrorViewFactory;
52
    }
53
54
    /**
55
     * @param Request $request
56
     *
57
     * @return Response
58
     */
59
    public function __invoke(Request $request)
60
    {
61
        $pickupRequest = new DropCartRequest($request);
62
63
        $validationResults = $this->validator->validate($pickupRequest);
64
65
        if (0 === count($validationResults)) {
66
            $this->bus->handle($pickupRequest->getCommand());
67
68
            return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
69
        }
70
71
        return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
72
    }
73
}
74