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

RemoveAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 63
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A __invoke() 14 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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