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

RemoveAddressAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
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\Component\Core\Model\ShopUserInterface;
11
use Sylius\ShopApiPlugin\Command\RemoveAddress;
12
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactory;
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
     * @param ViewHandlerInterface $viewHandler
48
     * @param ValidatorInterface $validator
49
     * @param ValidationErrorViewFactory $validationErrorViewFactory
50
     * @param CommandBus $bus
51
     * @param TokenStorageInterface $tokenStorage
52
     */
53
    public function __construct(
54
        ViewHandlerInterface $viewHandler,
55
        ValidatorInterface $validator,
56
        ValidationErrorViewFactory $validationErrorViewFactory,
57
        CommandBus $bus,
58
        TokenStorageInterface $tokenStorage
59
    ) {
60
        $this->viewHandler = $viewHandler;
61
        $this->validator = $validator;
62
        $this->validationErrorViewFactory = $validationErrorViewFactory;
63
        $this->bus = $bus;
64
        $this->tokenStorage = $tokenStorage;
65
    }
66
67
    public function __invoke(Request $request): Response
68
    {
69
        $removeAddressRequest = new RemoveAddressRequest($request);
70
71
        $validationResults = $this->validator->validate($removeAddressRequest);
72
73
        if (0 !== count($validationResults)) {
74
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
75
        }
76
77
        /** @var ShopUserInterface $shopUser */
78
        $shopUser = $this->tokenStorage->getToken()->getUser();
79
80
        $this->bus->handle(new RemoveAddress(
81
            $request->attributes->get('id'),
82
            $shopUser->getEmail()
83
        ));
84
85
        return $this->viewHandler->handle(View::create('', Response::HTTP_NO_CONTENT));
86
    }
87
}
88