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

SetDefaultAddress::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 14
rs 9.4285
c 1
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\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 View Code Duplication
final class SetDefaultAddress
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
     * @var TokenStorageInterface
41
     */
42
    private $tokenStorage;
43
44
    /**
45
     * CreateAddressAction constructor.
46
     *
47
     * @param ViewHandlerInterface $viewHandler
48
     * @param CommandBus $bus
49
     * @param ValidatorInterface $validator
50
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
51
     * @param TokenStorageInterface $tokenStorage
52
     */
53
    public function __construct(
54
        ViewHandlerInterface $viewHandler,
55
        CommandBus $bus,
56
        ValidatorInterface $validator,
57
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
58
        TokenStorageInterface $tokenStorage
59
    ) {
60
        $this->viewHandler = $viewHandler;
61
        $this->bus = $bus;
62
        $this->validator = $validator;
63
        $this->validationErrorViewFactory = $validationErrorViewFactory;
64
        $this->tokenStorage = $tokenStorage;
65
    }
66
67
    /**
68
     * @param Request $request
69
     *
70
     * @return Response
71
     */
72
    public function __invoke(Request $request)
73
    {
74
        $setDefaultAddressRequest = new SetDefaultAddressRequest($request, $this->tokenStorage->getToken()->getUser());
75
76
        $validationResults = $this->validator->validate($setDefaultAddressRequest);
77
78
        if (0 !== count($validationResults)) {
79
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
80
        }
81
82
        $this->bus->handle($setDefaultAddressRequest->getCommand());
83
84
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
85
    }
86
}
87