|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Controller\Customer; |
|
6
|
|
|
|
|
7
|
|
|
use FOS\RestBundle\View\View; |
|
8
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
9
|
|
|
use League\Tactician\CommandBus; |
|
10
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
11
|
|
|
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Factory\CustomerViewFactoryInterface; |
|
13
|
|
|
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; |
|
14
|
|
|
use Sylius\ShopApiPlugin\Request\UpdateCustomerRequest; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
18
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
19
|
|
|
use Webmozart\Assert\Assert; |
|
20
|
|
|
|
|
21
|
|
|
final class UpdateCustomerAction |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ViewHandlerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $viewHandler; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ValidatorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $validator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CommandBus |
|
35
|
|
|
*/ |
|
36
|
|
|
private $bus; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ValidationErrorViewFactoryInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $validationErrorViewFactory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var CustomerViewFactoryInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $customerViewFactory; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var CustomerRepositoryInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $customerRepository; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var TokenStorageInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $tokenStorage; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param ViewHandlerInterface $viewHandler |
|
60
|
|
|
* @param ValidatorInterface $validator |
|
61
|
|
|
* @param CommandBus $bus |
|
62
|
|
|
* @param ValidationErrorViewFactoryInterface $validationErrorViewFactory |
|
63
|
|
|
* @param CustomerViewFactoryInterface $customerViewFactory |
|
64
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
|
65
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct( |
|
68
|
|
|
ViewHandlerInterface $viewHandler, |
|
69
|
|
|
ValidatorInterface $validator, |
|
70
|
|
|
CommandBus $bus, |
|
71
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
72
|
|
|
CustomerViewFactoryInterface $customerViewFactory, |
|
73
|
|
|
CustomerRepositoryInterface $customerRepository, |
|
74
|
|
|
TokenStorageInterface $tokenStorage |
|
75
|
|
|
) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->viewHandler = $viewHandler; |
|
78
|
|
|
$this->validator = $validator; |
|
79
|
|
|
$this->bus = $bus; |
|
80
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
|
81
|
|
|
$this->customerViewFactory = $customerViewFactory; |
|
82
|
|
|
$this->customerRepository = $customerRepository; |
|
83
|
|
|
$this->tokenStorage = $tokenStorage; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
* |
|
89
|
|
|
* @return Response |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __invoke(Request $request): Response |
|
92
|
|
|
{ |
|
93
|
|
|
/** @var ShopUserInterface $user */ |
|
94
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
|
95
|
|
|
|
|
96
|
|
|
Assert::isInstanceOf($user, ShopUserInterface::class); |
|
97
|
|
|
|
|
98
|
|
|
$customer = $user->getCustomer(); |
|
99
|
|
|
$updateCustomerRequest = new UpdateCustomerRequest($request); |
|
100
|
|
|
|
|
101
|
|
|
$validationResults = $this->validator->validate($updateCustomerRequest, null, 'sylius_customer_profile_update'); |
|
102
|
|
|
|
|
103
|
|
|
if (0 !== count($validationResults)) { |
|
104
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$updateCustomerCommand = $updateCustomerRequest->getCommand(); |
|
108
|
|
|
$this->bus->handle($updateCustomerCommand); |
|
109
|
|
|
|
|
110
|
|
|
return $this->viewHandler->handle(View::create( |
|
111
|
|
|
$this->customerViewFactory->create($customer), |
|
112
|
|
|
Response::HTTP_OK) |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|