|
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\CustomerInterface; |
|
9
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
10
|
|
|
use Sylius\Component\Core\Repository\AddressRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Command\SetDefaultAddress; |
|
13
|
|
|
use Webmozart\Assert\Assert; |
|
14
|
|
|
|
|
15
|
|
|
class SetDefaultAddressHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CustomerRepositoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $customerRepository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var AddressRepositoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $addressRepository; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(CustomerRepositoryInterface $customerRepository, AddressRepositoryInterface $addressRepository) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->customerRepository = $customerRepository; |
|
30
|
|
|
$this->addressRepository = $addressRepository; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function handle(SetDefaultAddress $setDefaultAddress): void |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var AddressInterface $address */ |
|
36
|
|
|
$address = $this->addressRepository->find($setDefaultAddress->id); |
|
37
|
|
|
/** @var CustomerInterface $customer */ |
|
38
|
|
|
$customer = $setDefaultAddress->user->getCustomer(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertCurrentUserIsOwner($address, $setDefaultAddress->user); |
|
41
|
|
|
|
|
42
|
|
|
$customer->setDefaultAddress($address); |
|
43
|
|
|
$this->customerRepository->add($customer); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user) |
|
47
|
|
|
{ |
|
48
|
|
|
Assert::notNull($address->getCustomer(), 'Address is not associated with any user'); |
|
49
|
|
|
Assert::eq($address->getCustomer()->getId(), $user->getId(), 'Current user is not owner of this address'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|