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

SetDefaultAddress::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
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\Factory\ValidationErrorViewFactoryInterface;
11
use Sylius\ShopApiPlugin\Request\SetDefaultAddressRequest;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
final class SetDefaultAddress
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
     * CreateAddressAction constructor.
40
     *
41
     * @param ViewHandlerInterface $viewHandler
42
     * @param CommandBus $bus
43
     * @param ValidatorInterface $validator
44
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
45
     */
46
    public function __construct(
47
        ViewHandlerInterface $viewHandler,
48
        CommandBus $bus,
49
        ValidatorInterface $validator,
50
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
51
    ) {
52
        $this->viewHandler = $viewHandler;
53
        $this->bus = $bus;
54
        $this->validator = $validator;
55
        $this->validationErrorViewFactory = $validationErrorViewFactory;
56
    }
57
58
    /**
59
     * @param Request $request
60
     *
61
     * @return Response
62
     */
63
    public function __invoke(Request $request)
64
    {
65
        $setDefaultAddressRequest = new SetDefaultAddressRequest($request);
66
67
        $validationResults = $this->validator->validate($setDefaultAddressRequest);
68
69
        if (0 !== count($validationResults)) {
70
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
71
        }
72
73
        $this->bus->handle($setDefaultAddressRequest->getCommand());
74
75
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
76
    }
77
}
78