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( |
|
|
|
|
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
|
|
|
|
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.