GetCustomerAddress.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\QuoteGraphQl\Model\Cart;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\Customer\Api\Data\AddressInterface;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
  13. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  14. /**
  15. * Get customer address. Throws exception if customer is not owner of address
  16. */
  17. class GetCustomerAddress
  18. {
  19. /**
  20. * @var AddressRepositoryInterface
  21. */
  22. private $addressRepository;
  23. /**
  24. * @param AddressRepositoryInterface $addressRepository
  25. */
  26. public function __construct(AddressRepositoryInterface $addressRepository)
  27. {
  28. $this->addressRepository = $addressRepository;
  29. }
  30. /**
  31. * Get customer address. Throws exception if customer is not owner of address
  32. *
  33. * @param int $addressId
  34. * @param int $customerId
  35. * @return AddressInterface
  36. * @throws GraphQlAuthorizationException
  37. * @throws GraphQlNoSuchEntityException
  38. * @throws LocalizedException
  39. */
  40. public function execute(int $addressId, int $customerId): AddressInterface
  41. {
  42. try {
  43. $customerAddress = $this->addressRepository->getById($addressId);
  44. } catch (NoSuchEntityException $e) {
  45. throw new GraphQlNoSuchEntityException(
  46. __('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
  47. );
  48. }
  49. if ((int)$customerAddress->getCustomerId() !== $customerId) {
  50. throw new GraphQlAuthorizationException(
  51. __(
  52. 'The current user cannot use address with ID "%address_id"',
  53. ['address_id' => $addressId]
  54. )
  55. );
  56. }
  57. return $customerAddress;
  58. }
  59. }