|
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 Sylius\Component\Core\Model\Customer; |
|
10
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
11
|
|
|
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
15
|
|
|
use Webmozart\Assert\Assert; |
|
16
|
|
|
|
|
17
|
|
|
final class ShowAddressBookAction |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ViewHandlerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $viewHandler; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var TokenStorageInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $tokenStorage; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var AddressBookViewFactoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $addressBookViewFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ViewHandlerInterface $viewHandler |
|
36
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
37
|
|
|
* @param AddressBookViewFactoryInterface $addressBookViewFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
ViewHandlerInterface $viewHandler, |
|
41
|
|
|
TokenStorageInterface $tokenStorage, |
|
42
|
|
|
AddressBookViewFactoryInterface $addressBookViewFactory |
|
43
|
|
|
) { |
|
44
|
|
|
$this->viewHandler = $viewHandler; |
|
45
|
|
|
$this->tokenStorage = $tokenStorage; |
|
46
|
|
|
$this->addressBookViewFactory = $addressBookViewFactory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __invoke(Request $request): Response |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var ShopUserInterface $user */ |
|
52
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
|
53
|
|
|
|
|
54
|
|
|
Assert::isInstanceOf($user, ShopUserInterface::class); |
|
55
|
|
|
|
|
56
|
|
|
/** @var Customer $customer */ |
|
57
|
|
|
$customer = $user->getCustomer(); |
|
58
|
|
|
$addresses = $customer->getAddresses(); |
|
59
|
|
|
|
|
60
|
|
|
$addressViews = []; |
|
61
|
|
|
foreach ($addresses as $address) { |
|
62
|
|
|
$addressViews[] = $this->addressBookViewFactory->create($address, $customer); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->viewHandler->handle(View::create($addressViews, Response::HTTP_OK)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|