|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Sylius\Component\Addressing\Model\ProvinceInterface; |
|
6
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
|
7
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
8
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
9
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
10
|
|
|
use Sylius\ShopApiPlugin\Command\CreateAddress; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
12
|
|
|
use Webmozart\Assert\Assert; |
|
13
|
|
|
|
|
14
|
|
|
final class CreateAddressHandler |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var RepositoryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $addressRepository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var FactoryInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $addressFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var RepositoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $countryRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var RepositoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $provinceRepository; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var TokenStorageInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $tokenStorage; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* CreateAddressHandler constructor. |
|
43
|
|
|
* @param RepositoryInterface $addressRepository |
|
44
|
|
|
* @param RepositoryInterface $countryRepository |
|
45
|
|
|
* @param RepositoryInterface $provinceRepository |
|
46
|
|
|
* @param FactoryInterface $addressFactory |
|
47
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( |
|
50
|
|
|
RepositoryInterface $addressRepository, |
|
51
|
|
|
RepositoryInterface $countryRepository, |
|
52
|
|
|
RepositoryInterface $provinceRepository, |
|
53
|
|
|
FactoryInterface $addressFactory, |
|
54
|
|
|
TokenStorageInterface $tokenStorage |
|
55
|
|
|
) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->addressRepository = $addressRepository; |
|
58
|
|
|
$this->countryRepository = $countryRepository; |
|
59
|
|
|
$this->provinceRepository = $provinceRepository; |
|
60
|
|
|
$this->addressFactory = $addressFactory; |
|
61
|
|
|
$this->tokenStorage = $tokenStorage; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function handle(CreateAddress $command): void |
|
65
|
|
|
{ |
|
66
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
|
67
|
|
|
$customer = $user->getCustomer(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertShopUserExists($user); |
|
70
|
|
|
$this->assertCountryExists($command->countryCode()); |
|
71
|
|
|
|
|
72
|
|
|
/** @var AddressInterface $address */ |
|
73
|
|
|
$address = $this->addressFactory->createNew(); |
|
74
|
|
|
$address->setFirstName($command->firstName()); |
|
75
|
|
|
$address->setLastName($command->lastName()); |
|
76
|
|
|
$address->setCompany($command->company()); |
|
77
|
|
|
$address->setStreet($command->street()); |
|
78
|
|
|
$address->setCountryCode($command->countryCode()); |
|
79
|
|
|
$address->setCity($command->city()); |
|
80
|
|
|
$address->setPostcode($command->postcode()); |
|
81
|
|
|
$address->setPhoneNumber($command->phoneNumber()); |
|
82
|
|
|
|
|
83
|
|
|
if($command->provinceCode()) { |
|
|
|
|
|
|
84
|
|
|
$province = $this->getProvince($command->provinceCode()); |
|
85
|
|
|
$this->assertProvinceExists($province); |
|
86
|
|
|
$address->setProvinceCode($province->getCode()); |
|
87
|
|
|
$address->setProvinceName($province->getName()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$customer->addAddress($address); |
|
91
|
|
|
$this->addressRepository->add($address); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $countryCode |
|
96
|
|
|
*/ |
|
97
|
|
|
private function assertCountryExists(string $countryCode): void |
|
98
|
|
|
{ |
|
99
|
|
|
Assert::notNull($this->countryRepository->findOneBy(["code" => $countryCode]), 'Country does not exist.'); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $province |
|
104
|
|
|
*/ |
|
105
|
|
|
private function assertProvinceExists($province): void |
|
106
|
|
|
{ |
|
107
|
|
|
Assert::notNull($province, 'Province does not exist.'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $provinceCode |
|
112
|
|
|
* @return ProvinceInterface|object |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getProvince(string $provinceCode) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->provinceRepository->findOneBy(["code" => $provinceCode]); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $user |
|
121
|
|
|
*/ |
|
122
|
|
|
private function assertShopUserExists($user) |
|
123
|
|
|
{ |
|
124
|
|
|
Assert::isInstanceOf($user, ShopUserInterface::class, "Logged in user does not exist"); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: