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

UpdateAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 88
Duplicated Lines 19.32 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 4
dl 17
loc 88
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 23 2
A __construct() 17 17 1

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\Component\Core\Model\AddressInterface;
11
use Sylius\Component\Core\Model\ShopUserInterface;
12
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
13
use Sylius\ShopApiPlugin\Command\UpdateAddress;
14
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface;
15
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
16
use Sylius\ShopApiPlugin\Model\Address;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
20
use Symfony\Component\Validator\Validator\ValidatorInterface;
21
22
final class UpdateAddressAction
23
{
24
    /**
25
     * @var ViewHandlerInterface
26
     */
27
    private $viewHandler;
28
29
    /**
30
     * @var ValidatorInterface
31
     */
32
    private $validator;
33
34
    /**
35
     * @var CommandBus
36
     */
37
    private $bus;
38
39
    /**
40
     * @var ValidationErrorViewFactoryInterface
41
     */
42
    private $validationErrorViewFactory;
43
44
    /**
45
     * @var AddressBookViewFactoryInterface
46
     */
47
    private $addressBookViewFactory;
48
49
    /**
50
     * @var AddressRepositoryInterface
51
     */
52
    private $addressRepository;
53
54
    /**
55
     * @var TokenStorageInterface
56
     */
57
    private $tokenStorage;
58
59
    /**
60
     * @param ViewHandlerInterface $viewHandler
61
     * @param ValidatorInterface $validator
62
     * @param CommandBus $bus
63
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
64
     * @param AddressBookViewFactoryInterface $addressViewFactory
65
     * @param AddressRepositoryInterface $addressRepository
66
     * @param TokenStorageInterface $tokenStorage
67
     */
68 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
69
        ViewHandlerInterface $viewHandler,
70
        ValidatorInterface $validator,
71
        CommandBus $bus,
72
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
73
        AddressBookViewFactoryInterface $addressViewFactory,
74
        AddressRepositoryInterface $addressRepository,
75
        TokenStorageInterface $tokenStorage
76
    ) {
77
        $this->viewHandler = $viewHandler;
78
        $this->validator = $validator;
79
        $this->bus = $bus;
80
        $this->validationErrorViewFactory = $validationErrorViewFactory;
81
        $this->addressBookViewFactory = $addressViewFactory;
82
        $this->addressRepository = $addressRepository;
83
        $this->tokenStorage = $tokenStorage;
84
    }
85
86
    public function __invoke(Request $request, $id): Response
87
    {
88
        $addressModel = Address::createFromRequest($request);
89
90
        $validationResults = $this->validator->validate($addressModel);
91
92
        if (0 !== count($validationResults)) {
93
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
94
        }
95
96
        /** @var ShopUserInterface $customer */
97
        $shopUser = $this->tokenStorage->getToken()->getUser();
98
99
        $this->bus->handle(new UpdateAddress($addressModel, $shopUser->getEmail(), $id));
100
101
        /** @var AddressInterface $updatedAddress */
102
        $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]);
103
104
        return $this->viewHandler->handle(View::create(
105
            $this->addressBookViewFactory->create($updatedAddress, $shopUser->getCustomer()),
106
            Response::HTTP_OK)
107
        );
108
    }
109
}
110