1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Addressing\Model\ProvinceInterface; |
8
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
9
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
10
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
11
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
12
|
|
|
use Sylius\ShopApiPlugin\Command\CreateAddress; |
13
|
|
|
use Webmozart\Assert\Assert; |
14
|
|
|
|
15
|
|
|
final class CreateAddressHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var RepositoryInterface |
19
|
|
|
*/ |
20
|
|
|
private $addressRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var FactoryInterface |
24
|
|
|
*/ |
25
|
|
|
private $addressFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RepositoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $countryRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $provinceRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RepositoryInterface |
39
|
|
|
*/ |
40
|
|
|
private $customerRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param RepositoryInterface $addressRepository |
44
|
|
|
* @param RepositoryInterface $countryRepository |
45
|
|
|
* @param RepositoryInterface $provinceRepository |
46
|
|
|
* @param RepositoryInterface $customerRepository |
47
|
|
|
* @param FactoryInterface $addressFactory |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
50
|
|
|
RepositoryInterface $addressRepository, |
51
|
|
|
RepositoryInterface $countryRepository, |
52
|
|
|
RepositoryInterface $provinceRepository, |
53
|
|
|
RepositoryInterface $customerRepository, |
54
|
|
|
FactoryInterface $addressFactory |
55
|
|
|
) { |
56
|
|
|
$this->addressRepository = $addressRepository; |
57
|
|
|
$this->countryRepository = $countryRepository; |
58
|
|
|
$this->provinceRepository = $provinceRepository; |
59
|
|
|
$this->addressFactory = $addressFactory; |
60
|
|
|
$this->customerRepository = $customerRepository; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function handle(CreateAddress $command): void |
64
|
|
|
{ |
65
|
|
|
/** @var CustomerInterface $shopUser */ |
66
|
|
|
$customer = $this->customerRepository->findOneBy(['email' => $command->userEmail()]); |
67
|
|
|
|
68
|
|
|
$addressData = $command->address(); |
69
|
|
|
|
70
|
|
|
$this->assertCustomerExists($customer); |
71
|
|
|
$this->assertCountryExists($addressData->countryCode()); |
72
|
|
|
|
73
|
|
|
/** @var AddressInterface $address */ |
74
|
|
|
$address = $this->addressFactory->createNew(); |
75
|
|
|
$address->setFirstName($addressData->firstName()); |
76
|
|
|
$address->setLastName($addressData->lastName()); |
77
|
|
|
$address->setCompany($addressData->company()); |
78
|
|
|
$address->setStreet($addressData->street()); |
79
|
|
|
$address->setCountryCode($addressData->countryCode()); |
80
|
|
|
$address->setCity($addressData->city()); |
81
|
|
|
$address->setPostcode($addressData->postcode()); |
82
|
|
|
$address->setPhoneNumber($addressData->phoneNumber()); |
83
|
|
|
|
84
|
|
|
if (null !== $addressData->provinceCode()) { |
85
|
|
|
$province = $this->checkProvinceExists($addressData->provinceCode()); |
86
|
|
|
$address->setProvinceCode($province->getCode()); |
87
|
|
|
$address->setProvinceName($province->getName()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$customer->addAddress($address); |
91
|
|
|
|
92
|
|
|
$this->addressRepository->add($address); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $customer |
97
|
|
|
*/ |
98
|
|
|
private function assertCustomerExists($customer): void |
99
|
|
|
{ |
100
|
|
|
Assert::notNull($customer, 'Customer does not exists!'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $countryCode |
105
|
|
|
*/ |
106
|
|
|
private function assertCountryExists(string $countryCode): void |
107
|
|
|
{ |
108
|
|
|
Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $provinceCode |
113
|
|
|
* |
114
|
|
|
* @return ProvinceInterface |
115
|
|
|
*/ |
116
|
|
|
private function checkProvinceExists(string $provinceCode): ProvinceInterface |
117
|
|
|
{ |
118
|
|
|
/** @var ProvinceInterface $province */ |
119
|
|
|
$province = $this->provinceRepository->findOneBy(['code' => $provinceCode]); |
120
|
|
|
|
121
|
|
|
Assert::notNull($province, 'Province does not exist.'); |
122
|
|
|
|
123
|
|
|
return $province; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.