1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
6
|
|
|
|
7
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
9
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
10
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
11
|
|
|
use Sylius\ShopApiPlugin\Command\UpdateAddress; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
13
|
|
|
use Webmozart\Assert\Assert; |
14
|
|
|
|
15
|
|
|
final class UpdateAddressBookAddressHandler |
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 TokenStorageInterface |
39
|
|
|
*/ |
40
|
|
|
private $tokenStorage; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* CreateAddressHandler constructor. |
44
|
|
|
* |
45
|
|
|
* @param RepositoryInterface $addressRepository |
46
|
|
|
* @param RepositoryInterface $countryRepository |
47
|
|
|
* @param RepositoryInterface $provinceRepository |
48
|
|
|
* @param FactoryInterface $addressFactory |
49
|
|
|
* @param TokenStorageInterface $tokenStorage |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
52
|
|
|
RepositoryInterface $addressRepository, |
53
|
|
|
RepositoryInterface $countryRepository, |
54
|
|
|
RepositoryInterface $provinceRepository, |
55
|
|
|
FactoryInterface $addressFactory, |
56
|
|
|
TokenStorageInterface $tokenStorage |
57
|
|
|
) { |
58
|
|
|
$this->addressRepository = $addressRepository; |
59
|
|
|
$this->countryRepository = $countryRepository; |
60
|
|
|
$this->provinceRepository = $provinceRepository; |
61
|
|
|
$this->addressFactory = $addressFactory; |
62
|
|
|
$this->tokenStorage = $tokenStorage; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function handle(UpdateAddress $command): void |
66
|
|
|
{ |
67
|
|
|
/** @var AddressInterface $address */ |
68
|
|
|
$address = $this->addressRepository->findOneBy(['id' => $command->id()]); |
69
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
70
|
|
|
|
71
|
|
|
$this->assertCurrentUserIsOwner($address, $user); |
72
|
|
|
$this->assertCountryExists($command->countryCode()); |
73
|
|
|
|
74
|
|
|
/** @var AddressInterface $address */ |
75
|
|
|
$address->setFirstName($command->firstName()); |
76
|
|
|
$address->setLastName($command->lastName()); |
77
|
|
|
$address->setCompany($command->company()); |
78
|
|
|
$address->setStreet($command->street()); |
79
|
|
|
$address->setCountryCode($command->countryCode()); |
80
|
|
|
$address->setCity($command->city()); |
81
|
|
|
$address->setPostcode($command->postcode()); |
82
|
|
|
$address->setPhoneNumber($command->phoneNumber()); |
83
|
|
|
|
84
|
|
|
if (null !== $command->provinceCode() && $command->provinceCode() !== $address->getProvinceCode()) { |
85
|
|
|
$province = $this->provinceRepository->findOneBy(['code' => $command->provinceCode()]); |
86
|
|
|
$this->assertProvinceExists($province); |
87
|
|
|
$address->setProvinceCode($province->getCode()); |
88
|
|
|
$address->setProvinceName($province->getName()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->addressRepository->add($address); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function assertCountryExists(string $countryCode): void |
95
|
|
|
{ |
96
|
|
|
Assert::notNull($this->countryRepository->findOneBy(['code' => $countryCode]), 'Country does not exist.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function assertProvinceExists($province): void |
100
|
|
|
{ |
101
|
|
|
Assert::notNull($province, 'Province does not exist.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user) |
105
|
|
|
{ |
106
|
|
|
Assert::notNull($address->getCustomer(), 'Address is not associated with any user'); |
107
|
|
|
Assert::eq($address->getCustomer(), $user->getCustomer(), 'Current user is not owner of this address'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.