Completed
Pull Request — master (#229)
by
unknown
02:44
created

RemoveAddressAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
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\Command\RemoveAddress;
11
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactory;
12
use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface;
13
use Sylius\ShopApiPlugin\Request\RemoveAddressRequest;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Validator\Validator\ValidatorInterface;
18
19 View Code Duplication
final 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...
20
{
21
    /**
22
     * @var ViewHandlerInterface
23
     */
24
    private $viewHandler;
25
26
    /**
27
     * @var ValidatorInterface
28
     */
29
    private $validator;
30
31
    /**
32
     * @var ValidationErrorViewFactory
33
     */
34
    private $validationErrorViewFactory;
35
36
    /**
37
     * @var CommandBus
38
     */
39
    private $bus;
40
41
    /**
42
     * @var TokenStorageInterface
43
     */
44
    private $tokenStorage;
45
46
    /**
47
     * @var CurrentUserProviderInterface
48
     */
49
    private $currentUserProvider;
50
51
    /**
52
     * @param ViewHandlerInterface $viewHandler
53
     * @param ValidatorInterface $validator
54
     * @param ValidationErrorViewFactory $validationErrorViewFactory
55
     * @param CommandBus $bus
56
     * @param TokenStorageInterface $tokenStorage
57
     * @param CurrentUserProviderInterface $currentUserProvider
58
     */
59
    public function __construct(
60
        ViewHandlerInterface $viewHandler,
61
        ValidatorInterface $validator,
62
        ValidationErrorViewFactory $validationErrorViewFactory,
63
        CommandBus $bus,
64
        TokenStorageInterface $tokenStorage,
65
        CurrentUserProviderInterface $currentUserProvider
66
    ) {
67
        $this->viewHandler = $viewHandler;
68
        $this->validator = $validator;
69
        $this->validationErrorViewFactory = $validationErrorViewFactory;
70
        $this->bus = $bus;
71
        $this->tokenStorage = $tokenStorage;
72
        $this->currentUserProvider = $currentUserProvider;
73
    }
74
75
    public function __invoke(Request $request): Response
76
    {
77
        $removeAddressRequest = new RemoveAddressRequest($request);
78
79
        $validationResults = $this->validator->validate($removeAddressRequest);
80
81
        if (0 !== count($validationResults)) {
82
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
83
        }
84
85
        $user = $this->currentUserProvider->provide();
86
87
        $this->bus->handle(new RemoveAddress(
88
            $request->attributes->get('id'),
89
            $user->getEmail()
90
        ));
91
92
        return $this->viewHandler->handle(View::create('', Response::HTTP_NO_CONTENT));
93
    }
94
}
95