1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Sylius\ShopApiPlugin\Controller\Customer; |
5
|
|
|
|
6
|
|
|
use FOS\RestBundle\View\View; |
7
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
8
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
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 CustomerAddressAction |
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
|
|
|
$customer = $user->getCustomer(); |
56
|
|
|
|
57
|
|
|
/** @var Customer $customer */ |
58
|
|
|
Assert::isInstanceOf($customer, Customer::class); |
59
|
|
|
|
60
|
|
|
/** @var AddressInterface $address */ |
61
|
|
|
if ($customer === null) { |
62
|
|
|
$address = null; |
63
|
|
|
$otherAddress = []; |
64
|
|
|
} else { |
65
|
|
|
|
66
|
|
|
$address = $customer->getDefaultAddress(); |
67
|
|
|
$otherAddress = $customer->getAddresses(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->viewHandler->handle( |
71
|
|
|
View::create($this->addressBookViewFactory->create($address, $otherAddress),Response::HTTP_OK) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|