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

SetDefaultAddress   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 60
rs 10
c 1
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\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
     * @param ViewHandlerInterface $viewHandler
40
     * @param CommandBus $bus
41
     * @param ValidatorInterface $validator
42
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
43
     */
44
    public function __construct(
45
        ViewHandlerInterface $viewHandler,
46
        CommandBus $bus,
47
        ValidatorInterface $validator,
48
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
49
    ) {
50
        $this->viewHandler = $viewHandler;
51
        $this->bus = $bus;
52
        $this->validator = $validator;
53
        $this->validationErrorViewFactory = $validationErrorViewFactory;
54
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @return Response
60
     */
61
    public function __invoke(Request $request)
62
    {
63
        $setDefaultAddressRequest = new SetDefaultAddressRequest($request);
64
65
        $validationResults = $this->validator->validate($setDefaultAddressRequest);
66
67
        if (0 !== count($validationResults)) {
68
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
69
        }
70
71
        $this->bus->handle($setDefaultAddressRequest->getCommand());
72
73
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
74
    }
75
}
76