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

CreateAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Controller\AddressBook;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use League\Tactician\CommandBus;
10
use Sylius\ShopApiPlugin\Command\CreateAddress;
11
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
12
use Sylius\ShopApiPlugin\Model\Address;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17
final class CreateAddressAction
18
{
19
    /**
20
     * @var ViewHandlerInterface
21
     */
22
    private $viewHandler;
23
24
    /**
25
     * @var CommandBus
26
     */
27
    private $bus;
28
29
    /**
30
     * @var ValidatorInterface
31
     */
32
    private $validator;
33
34
    /**
35
     * @var ValidationErrorViewFactoryInterface
36
     */
37
    private $validationErrorViewFactory;
38
39
    /**
40
     * CreateAddressAction constructor.
41
     *
42
     * @param ViewHandlerInterface $viewHandler
43
     * @param CommandBus $bus
44
     * @param ValidatorInterface $validator
45
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
46
     */
47
    public function __construct(
48
        ViewHandlerInterface $viewHandler,
49
        CommandBus $bus,
50
        ValidatorInterface $validator,
51
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
52
    ) {
53
        $this->viewHandler = $viewHandler;
54
        $this->bus = $bus;
55
        $this->validator = $validator;
56
        $this->validationErrorViewFactory = $validationErrorViewFactory;
57
    }
58
59
    /**
60
     * @param Request $request
61
     *
62
     * @return Response
63
     */
64
    public function __invoke(Request $request): Response
65
    {
66
        $addressModel = Address::createFromRequest($request);
67
68
        $validationResults = $this->validator->validate($addressModel, null, 'sylius_address_book_create');
69
70
        if (0 !== count($validationResults)) {
71
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
72
        }
73
74
        $this->bus->handle(new CreateAddress($addressModel));
75
76
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
77
    }
78
}
79