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

RemoveAddressAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
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\ValidationErrorViewFactory;
11
use Sylius\ShopApiPlugin\Request\RemoveAddressRequest;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
final class RemoveAddressAction
17
{
18
    /**
19
     * @var ViewHandlerInterface
20
     */
21
    private $viewHandler;
22
23
    /**
24
     * @var ValidatorInterface
25
     */
26
    private $validator;
27
28
    /**
29
     * @var ValidationErrorViewFactory
30
     */
31
    private $validationErrorViewFactory;
32
33
    /**
34
     * @var CommandBus
35
     */
36
    private $bus;
37
38
    /**
39
     * @param ViewHandlerInterface $viewHandler
40
     * @param ValidatorInterface $validator
41
     * @param ValidationErrorViewFactory $validationErrorViewFactory
42
     * @param CommandBus $bus
43
     */
44
    public function __construct(
45
        ViewHandlerInterface $viewHandler,
46
        ValidatorInterface $validator,
47
        ValidationErrorViewFactory $validationErrorViewFactory,
48
        CommandBus $bus
49
    ) {
50
        $this->viewHandler = $viewHandler;
51
        $this->validator = $validator;
52
        $this->validationErrorViewFactory = $validationErrorViewFactory;
53
        $this->bus = $bus;
54
    }
55
56
    public function __invoke(Request $request): Response
57
    {
58
        $removeAddressRequest = new RemoveAddressRequest($request);
59
60
        $validationResults = $this->validator->validate($removeAddressRequest);
61
62
        if (0 !== count($validationResults)) {
63
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
64
        }
65
66
        $this->bus->handle($removeAddressRequest->getCommand());
67
68
        return $this->viewHandler->handle(View::create('', Response::HTTP_NO_CONTENT));
69
    }
70
}
71