|
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\CreateAddress; |
|
11
|
|
|
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Model\Address; |
|
13
|
|
|
use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
|
17
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
18
|
|
|
|
|
19
|
|
|
final class CreateAddressAction |
|
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 TokenStorage |
|
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 TokenStorage $tokenStorage |
|
56
|
|
|
* @param CurrentUserProviderInterface $currentUserProvider |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( |
|
59
|
|
|
ViewHandlerInterface $viewHandler, |
|
60
|
|
|
CommandBus $bus, |
|
61
|
|
|
ValidatorInterface $validator, |
|
62
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
|
63
|
|
|
TokenStorage $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): Response |
|
80
|
|
|
{ |
|
81
|
|
|
$addressModel = Address::createFromRequest($request); |
|
82
|
|
|
|
|
83
|
|
|
$validationResults = $this->validator->validate($addressModel); |
|
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 CreateAddress($addressModel, $user->getEmail())); |
|
92
|
|
|
|
|
93
|
|
|
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|