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\ShopApiPlugin\Factory\CustomerViewFactoryInterface; |
12
|
|
|
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Request\UpdateCustomerRequest; |
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
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
final class UpdateCustomerAction |
21
|
|
|
{ |
22
|
|
|
/** @var ViewHandlerInterface */ |
23
|
|
|
private $viewHandler; |
24
|
|
|
|
25
|
|
|
/** @var ValidatorInterface */ |
26
|
|
|
private $validator; |
27
|
|
|
|
28
|
|
|
/** @var CommandBus */ |
29
|
|
|
private $bus; |
30
|
|
|
|
31
|
|
|
/** @var ValidationErrorViewFactoryInterface */ |
32
|
|
|
private $validationErrorViewFactory; |
33
|
|
|
|
34
|
|
|
/** @var CustomerViewFactoryInterface */ |
35
|
|
|
private $customerViewFactory; |
36
|
|
|
|
37
|
|
|
/** @var TokenStorageInterface */ |
38
|
|
|
private $tokenStorage; |
39
|
|
|
|
40
|
|
|
public function __construct( |
41
|
|
|
ViewHandlerInterface $viewHandler, |
42
|
|
|
ValidatorInterface $validator, |
43
|
|
|
CommandBus $bus, |
44
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
45
|
|
|
CustomerViewFactoryInterface $customerViewFactory, |
46
|
|
|
TokenStorageInterface $tokenStorage |
47
|
|
|
) { |
48
|
|
|
$this->viewHandler = $viewHandler; |
49
|
|
|
$this->validator = $validator; |
50
|
|
|
$this->bus = $bus; |
51
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
52
|
|
|
$this->customerViewFactory = $customerViewFactory; |
53
|
|
|
$this->tokenStorage = $tokenStorage; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Request $request |
58
|
|
|
* |
59
|
|
|
* @return Response |
60
|
|
|
*/ |
61
|
|
|
public function __invoke(Request $request): Response |
62
|
|
|
{ |
63
|
|
|
/** @var ShopUserInterface $user */ |
64
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
65
|
|
|
|
66
|
|
|
Assert::isInstanceOf($user, ShopUserInterface::class); |
67
|
|
|
|
68
|
|
|
$customer = $user->getCustomer(); |
69
|
|
|
$updateCustomerRequest = new UpdateCustomerRequest($request); |
70
|
|
|
|
71
|
|
|
$validationResults = $this->validator->validate($updateCustomerRequest, null, 'sylius_customer_profile_update'); |
72
|
|
|
|
73
|
|
|
if (0 !== count($validationResults)) { |
74
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$updateCustomerCommand = $updateCustomerRequest->getCommand(); |
78
|
|
|
$this->bus->handle($updateCustomerCommand); |
79
|
|
|
|
80
|
|
|
return $this->viewHandler->handle(View::create( |
81
|
|
|
$this->customerViewFactory->create($customer), |
82
|
|
|
Response::HTTP_OK) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|