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\ShopUserInterface; |
11
|
|
|
use Sylius\ShopApiPlugin\Command\CreateAddress; |
12
|
|
|
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Model\Address; |
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
|
|
|
/** |
47
|
|
|
* @param ViewHandlerInterface $viewHandler |
48
|
|
|
* @param CommandBus $bus |
49
|
|
|
* @param ValidatorInterface $validator |
50
|
|
|
* @param ValidationErrorViewFactoryInterface $validationErrorViewFactory |
51
|
|
|
* @param TokenStorage $tokenStorage |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
ViewHandlerInterface $viewHandler, |
55
|
|
|
CommandBus $bus, |
56
|
|
|
ValidatorInterface $validator, |
57
|
|
|
ValidationErrorViewFactoryInterface $validationErrorViewFactory, |
58
|
|
|
TokenStorage $tokenStorage |
59
|
|
|
) { |
60
|
|
|
$this->viewHandler = $viewHandler; |
61
|
|
|
$this->bus = $bus; |
62
|
|
|
$this->validator = $validator; |
63
|
|
|
$this->validationErrorViewFactory = $validationErrorViewFactory; |
64
|
|
|
$this->tokenStorage = $tokenStorage; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Request $request |
69
|
|
|
* |
70
|
|
|
* @return Response |
71
|
|
|
*/ |
72
|
|
|
public function __invoke(Request $request): Response |
73
|
|
|
{ |
74
|
|
|
$addressModel = Address::createFromRequest($request); |
75
|
|
|
|
76
|
|
|
$validationResults = $this->validator->validate($addressModel); |
77
|
|
|
|
78
|
|
|
if (0 !== count($validationResults)) { |
79
|
|
|
return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** @var ShopUserInterface $shopUser */ |
83
|
|
|
$shopUser = $this->tokenStorage->getToken()->getUser(); |
84
|
|
|
|
85
|
|
|
$this->bus->handle(new CreateAddress($addressModel, $shopUser->getEmail())); |
86
|
|
|
|
87
|
|
|
return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|