|
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\OrderInterface; |
|
9
|
|
|
use Sylius\Component\Core\Model\ShopUserInterface; |
|
10
|
|
|
use Sylius\Component\Core\Repository\AddressRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Command\RemoveAddress; |
|
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
14
|
|
|
use Webmozart\Assert\Assert; |
|
15
|
|
|
|
|
16
|
|
|
final class RemoveAddressHandler |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var AddressRepositoryInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $addressRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var OrderRepositoryInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $orderRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var TokenStorageInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $tokenStorage; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param AddressRepositoryInterface $addressRepository |
|
35
|
|
|
* @param OrderRepositoryInterface $orderRepository |
|
36
|
|
|
* @param TokenStorageInterface $tokenStorage |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(AddressRepositoryInterface $addressRepository, OrderRepositoryInterface $orderRepository, TokenStorageInterface $tokenStorage) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->addressRepository = $addressRepository; |
|
41
|
|
|
$this->orderRepository = $orderRepository; |
|
42
|
|
|
$this->tokenStorage = $tokenStorage; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function handle(RemoveAddress $removeAddress) |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var ShopUserInterface $user */ |
|
48
|
|
|
$user = $this->tokenStorage->getToken()->getUser(); |
|
49
|
|
|
|
|
50
|
|
|
/** @var AddressInterface $address */ |
|
51
|
|
|
$address = $this->addressRepository->findOneBy(['id' => $removeAddress->id()]); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertCurrentUserIsOwner($address, $user); |
|
54
|
|
|
$this->assertOrderWithAddressNotExists($address); |
|
55
|
|
|
|
|
56
|
|
|
$this->addressRepository->remove($address); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function assertOrderWithAddressNotExists($address) |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var OrderInterface $orderShippingAddress */ |
|
62
|
|
|
$orderShippingAddress = $this->orderRepository->findBy(['billingAddress' => $address]); |
|
63
|
|
|
/** @var OrderInterface $orderBillingAddress */ |
|
64
|
|
|
$orderBillingAddress = $this->orderRepository->findBy(['shippingAddress' => $address]); |
|
65
|
|
|
Assert::allIsEmpty([$orderShippingAddress, $orderBillingAddress], 'Cant delete address because it is associated with one or more orders'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user) |
|
69
|
|
|
{ |
|
70
|
|
|
Assert::eq($address->getCustomer()->getId(), $user->getId(), 'User is not owner of this address'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|