DeleteCustomerAddress.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CustomerGraphQl\Model\Resolver;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddressForUser;
  10. use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
  11. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  12. use Magento\Framework\GraphQl\Config\Element\Field;
  13. use Magento\Framework\GraphQl\Query\ResolverInterface;
  14. use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
  15. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  16. /**
  17. * Customers address delete, used for GraphQL request processing.
  18. */
  19. class DeleteCustomerAddress implements ResolverInterface
  20. {
  21. /**
  22. * @var CheckCustomerAccount
  23. */
  24. private $checkCustomerAccount;
  25. /**
  26. * @var AddressRepositoryInterface
  27. */
  28. private $addressRepository;
  29. /**
  30. * @var GetCustomerAddressForUser
  31. */
  32. private $getCustomerAddressForUser;
  33. /**
  34. * @param CheckCustomerAccount $checkCustomerAccount
  35. * @param AddressRepositoryInterface $addressRepository
  36. * @param GetCustomerAddressForUser $getCustomerAddressForUser
  37. */
  38. public function __construct(
  39. CheckCustomerAccount $checkCustomerAccount,
  40. AddressRepositoryInterface $addressRepository,
  41. GetCustomerAddressForUser $getCustomerAddressForUser
  42. ) {
  43. $this->checkCustomerAccount = $checkCustomerAccount;
  44. $this->addressRepository = $addressRepository;
  45. $this->getCustomerAddressForUser = $getCustomerAddressForUser;
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function resolve(
  51. Field $field,
  52. $context,
  53. ResolveInfo $info,
  54. array $value = null,
  55. array $args = null
  56. ) {
  57. $currentUserId = $context->getUserId();
  58. $currentUserType = $context->getUserType();
  59. $this->checkCustomerAccount->execute($currentUserId, $currentUserType);
  60. return $this->deleteCustomerAddress((int)$currentUserId, (int)$args['id']);
  61. }
  62. /**
  63. * Delete customer address
  64. *
  65. * @param int $customerId
  66. * @param int $addressId
  67. * @return bool
  68. * @throws GraphQlAuthorizationException
  69. * @throws GraphQlNoSuchEntityException
  70. */
  71. private function deleteCustomerAddress($customerId, $addressId)
  72. {
  73. $address = $this->getCustomerAddressForUser->execute($addressId, $customerId);
  74. if ($address->isDefaultBilling()) {
  75. throw new GraphQlAuthorizationException(
  76. __('Customer Address %1 is set as default billing address and can not be deleted', [$addressId])
  77. );
  78. }
  79. if ($address->isDefaultShipping()) {
  80. throw new GraphQlAuthorizationException(
  81. __('Customer Address %1 is set as default shipping address and can not be deleted', [$addressId])
  82. );
  83. }
  84. return $this->addressRepository->delete($address);
  85. }
  86. }