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

SetDefaultAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A __invoke() 17 17 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\Command\SetDefaultAddress;
11
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
12
use Sylius\ShopApiPlugin\Request\SetDefaultAddressRequest;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18 View Code Duplication
final class SetDefaultAddressAction
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...
19
{
20
    /**
21
     * @var ViewHandlerInterface
22
     */
23
    private $viewHandler;
24
25
    /**
26
     * @var CommandBus
27
     */
28
    private $bus;
29
30
    /**
31
     * @var ValidatorInterface
32
     */
33
    private $validator;
34
35
    /**
36
     * @var ValidationErrorViewFactoryInterface
37
     */
38
    private $validationErrorViewFactory;
39
40
    /**
41
     * @var TokenStorageInterface
42
     */
43
    private $tokenStorage;
44
45
    /**
46
     * @param ViewHandlerInterface $viewHandler
47
     * @param CommandBus $bus
48
     * @param ValidatorInterface $validator
49
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
50
     * @param TokenStorageInterface $tokenStorage
51
     */
52
    public function __construct(
53
        ViewHandlerInterface $viewHandler,
54
        CommandBus $bus,
55
        ValidatorInterface $validator,
56
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
57
        TokenStorageInterface $tokenStorage
58
    ) {
59
        $this->viewHandler = $viewHandler;
60
        $this->bus = $bus;
61
        $this->validator = $validator;
62
        $this->validationErrorViewFactory = $validationErrorViewFactory;
63
        $this->tokenStorage = $tokenStorage;
64
    }
65
66
    /**
67
     * @param Request $request
68
     *
69
     * @return Response
70
     */
71
    public function __invoke(Request $request)
72
    {
73
        $setDefaultAddressRequest = new SetDefaultAddressRequest($request);
74
75
        $validationResults = $this->validator->validate($setDefaultAddressRequest);
76
77
        if (0 !== count($validationResults)) {
78
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
79
        }
80
81
        $this->bus->handle(new SetDefaultAddress(
82
            $request->attributes->get('id'),
83
            $this->tokenStorage->getToken()->getUser()->getEmail()
84
        ));
85
86
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
87
    }
88
}
89