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

SetDefaultAddress::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
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\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17
final class SetDefaultAddress
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)
65
    {
66
        $setDefaultAddressRequest = new SetDefaultAddressRequest($request);
67
68
        $validationResults = $this->validator->validate($setDefaultAddressRequest);
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($setDefaultAddressRequest->getCommand());
75
76
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
77
    }
78
}
79