Failed Conditions
Pull Request — master (#229)
by
unknown
02:57
created

CreateAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A __invoke() 0 14 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Controller\AddressBook;
4
5
use FOS\RestBundle\View\View;
6
use League\Tactician\CommandBus;
7
use FOS\RestBundle\View\ViewHandlerInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Validator\Validator\ValidatorInterface;
11
use Sylius\ShopApiPlugin\Request\CreateAddressRequest;
12
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
13
14
final class CreateAddressAction
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
     * CreateAddressAction constructor.
38
     * @param ViewHandlerInterface $viewHandler
39
     * @param CommandBus $bus
40
     * @param ValidatorInterface $validator
41
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
42
     */
43
    public function __construct(
44
        ViewHandlerInterface $viewHandler,
45
        CommandBus $bus,
46
        ValidatorInterface $validator,
47
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
48
    )
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
     * @return Response
59
     */
60
    public function __invoke(Request $request): Response
61
    {
62
        $createAddressRequest = new CreateAddressRequest($request);
63
64
        $validationResults = $this->validator->validate($createAddressRequest);
65
66
        if (0 !== count($validationResults)) {
67
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
68
        }
69
70
        $this->bus->handle($createAddressRequest->getCommand());
71
72
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
73
    }
74
}