Failed Conditions
Pull Request — master (#229)
by
unknown
04:40 queued 02:07
created

CreateAddressAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
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
     * @param ViewHandlerInterface $viewHandler
41
     * @param CommandBus $bus
42
     * @param ValidatorInterface $validator
43
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
44
     */
45
    public function __construct(
46
        ViewHandlerInterface $viewHandler,
47
        CommandBus $bus,
48
        ValidatorInterface $validator,
49
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
50
    ) {
51
        $this->viewHandler = $viewHandler;
52
        $this->bus = $bus;
53
        $this->validator = $validator;
54
        $this->validationErrorViewFactory = $validationErrorViewFactory;
55
    }
56
57
    /**
58
     * @param Request $request
59
     *
60
     * @return Response
61
     */
62
    public function __invoke(Request $request): Response
63
    {
64
        $addressModel = Address::createFromRequest($request);
65
66
        $validationResults = $this->validator->validate($addressModel, null, 'sylius_address_book_create');
67
68
        if (0 !== count($validationResults)) {
69
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
70
        }
71
72
        $this->bus->handle(new CreateAddress($addressModel));
73
74
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
75
    }
76
}
77