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
|
|
|
$this->viewHandler = $viewHandler; |
77
|
|
|
$this->validator = $validator; |
78
|
|
|
$this->bus = $bus; |
79
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
80
|
|
|
$this->customerViewFactory = $customerViewFactory; |
81
|
|
|
$this->customerRepository = $customerRepository; |
82
|
|
|
$this->tokenStorage = $tokenStorage; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Request $request |
87
|
|
|
* |
88
|
|
|
* @return Response |
89
|
|
|
*/ |
90
|
|
|
public function __invoke(Request $request): Response |
91
|
|
|
{ |
92
|
|
|
/** @var ShopUserInterface $user */ |
93
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
94
|
|
|
|
95
|
|
|
Assert::isInstanceOf($user, ShopUserInterface::class); |
96
|
|
|
|
97
|
|
|
$customer = $user->getCustomer(); |
98
|
|
|
$updateCustomerRequest = new UpdateCustomerRequest($request); |
99
|
|
|
|
100
|
|
|
$validationResults = $this->validator->validate($updateCustomerRequest, null, 'sylius_customer_profile_update'); |
101
|
|
|
|
102
|
|
|
if (0 !== count($validationResults)) { |
103
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$updateCustomerCommand = $updateCustomerRequest->getCommand(); |
107
|
|
|
$this->bus->handle($updateCustomerCommand); |
108
|
|
|
|
109
|
|
|
return $this->viewHandler->handle(View::create( |
110
|
|
|
$this->customerViewFactory->create($customer), |
111
|
|
|
Response::HTTP_OK) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|