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

SetDefaultAddress   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 70
loc 70
rs 10
c 1
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\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