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\Provider\CurrentUserProviderInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Request\SetDefaultAddressRequest; |
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 SetDefaultAddressAction |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ViewHandlerInterface |
23
|
|
|
*/ |
24
|
|
|
private $viewHandler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CommandBus |
28
|
|
|
*/ |
29
|
|
|
private $bus; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ValidatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $validator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ValidationErrorViewFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $validationErrorViewFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TokenStorageInterface |
43
|
|
|
*/ |
44
|
|
|
private $tokenStorage; |
45
|
|
|
/** |
46
|
|
|
* @var CurrentUserProviderInterface |
47
|
|
|
*/ |
48
|
|
|
private $currentUserProvider; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ViewHandlerInterface $viewHandler |
52
|
|
|
* @param CommandBus $bus |
53
|
|
|
* @param ValidatorInterface $validator |
54
|
|
|
* @param ValidationErrorViewFactoryInterface $validationErrorViewFactory |
55
|
|
|
* @param TokenStorageInterface $tokenStorage |
56
|
|
|
* @param CurrentUserProviderInterface $currentUserProvider |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
ViewHandlerInterface $viewHandler, |
60
|
|
|
CommandBus $bus, |
61
|
|
|
ValidatorInterface $validator, |
62
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
63
|
|
|
TokenStorageInterface $tokenStorage, |
64
|
|
|
CurrentUserProviderInterface $currentUserProvider |
65
|
|
|
) { |
66
|
|
|
$this->viewHandler = $viewHandler; |
67
|
|
|
$this->bus = $bus; |
68
|
|
|
$this->validator = $validator; |
69
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
70
|
|
|
$this->tokenStorage = $tokenStorage; |
71
|
|
|
$this->currentUserProvider = $currentUserProvider; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Request $request |
76
|
|
|
* |
77
|
|
|
* @return Response |
78
|
|
|
*/ |
79
|
|
|
public function __invoke(Request $request) |
80
|
|
|
{ |
81
|
|
|
$setDefaultAddressRequest = new SetDefaultAddressRequest($request); |
82
|
|
|
|
83
|
|
|
$validationResults = $this->validator->validate($setDefaultAddressRequest); |
84
|
|
|
|
85
|
|
|
if (0 !== count($validationResults)) { |
86
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$user = $this->currentUserProvider->provide(); |
90
|
|
|
|
91
|
|
|
$this->bus->handle(new SetDefaultAddress( |
92
|
|
|
$request->attributes->get('id'), |
93
|
|
|
$user->getEmail() |
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.