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

RemoveAddressAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
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\ValidationErrorViewFactory;
11
use Sylius\ShopApiPlugin\Request\RemoveAddressRequest;
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
class RemoveAddressAction
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 ValidatorInterface
26
     */
27
    private $validator;
28
29
    /**
30
     * @var ValidationErrorViewFactory
31
     */
32
    private $validationErrorViewFactory;
33
34
    /**
35
     * @var CommandBus
36
     */
37
    private $bus;
38
39
    /**
40
     * @var TokenStorageInterface
41
     */
42
    private $tokenStorage;
43
44
    /**
45
     * @param ViewHandlerInterface $viewHandler
46
     * @param ValidatorInterface $validator
47
     * @param ValidationErrorViewFactory $validationErrorViewFactory
48
     * @param CommandBus $bus
49
     * @param TokenStorageInterface $tokenStorage
50
     */
51
    public function __construct(
52
        ViewHandlerInterface $viewHandler,
53
        ValidatorInterface $validator,
54
        ValidationErrorViewFactory $validationErrorViewFactory,
55
        CommandBus $bus,
56
        TokenStorageInterface $tokenStorage
57
    ) {
58
        $this->viewHandler = $viewHandler;
59
        $this->validator = $validator;
60
        $this->validationErrorViewFactory = $validationErrorViewFactory;
61
        $this->bus = $bus;
62
        $this->tokenStorage = $tokenStorage;
63
    }
64
65
    public function __invoke(Request $request): Response
66
    {
67
        $removeAddressRequest = new RemoveAddressRequest($request, $this->tokenStorage->getToken()->getUser());
68
69
        $validationResults = $this->validator->validate($removeAddressRequest);
70
71
        if (0 !== count($validationResults)) {
72
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
73
        }
74
75
        $this->bus->handle($removeAddressRequest->getCommand());
76
77
        return $this->viewHandler->handle(View::create('', Response::HTTP_NO_CONTENT));
78
    }
79
}
80