CheckCustomerAccount.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Customer;
  8. use Magento\Authorization\Model\UserContextInterface;
  9. use Magento\Customer\Api\AccountManagementInterface;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Customer\Model\AuthenticationInterface;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
  14. use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
  15. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  16. /**
  17. * Check customer account
  18. */
  19. class CheckCustomerAccount
  20. {
  21. /**
  22. * @var AuthenticationInterface
  23. */
  24. private $authentication;
  25. /**
  26. * @var CustomerRepositoryInterface
  27. */
  28. private $customerRepository;
  29. /**
  30. * @var AccountManagementInterface
  31. */
  32. private $accountManagement;
  33. /**
  34. * @param AuthenticationInterface $authentication
  35. * @param CustomerRepositoryInterface $customerRepository
  36. * @param AccountManagementInterface $accountManagement
  37. */
  38. public function __construct(
  39. AuthenticationInterface $authentication,
  40. CustomerRepositoryInterface $customerRepository,
  41. AccountManagementInterface $accountManagement
  42. ) {
  43. $this->authentication = $authentication;
  44. $this->customerRepository = $customerRepository;
  45. $this->accountManagement = $accountManagement;
  46. }
  47. /**
  48. * Check customer account
  49. *
  50. * @param int|null $customerId
  51. * @param int|null $customerType
  52. * @return void
  53. * @throws GraphQlAuthorizationException
  54. * @throws GraphQlNoSuchEntityException
  55. * @throws GraphQlAuthenticationException
  56. */
  57. public function execute(?int $customerId, ?int $customerType): void
  58. {
  59. if (true === $this->isCustomerGuest($customerId, $customerType)) {
  60. throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
  61. }
  62. try {
  63. $this->customerRepository->getById($customerId);
  64. } catch (NoSuchEntityException $e) {
  65. throw new GraphQlNoSuchEntityException(
  66. __('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]),
  67. $e
  68. );
  69. }
  70. if (true === $this->authentication->isLocked($customerId)) {
  71. throw new GraphQlAuthenticationException(__('The account is locked.'));
  72. }
  73. $confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
  74. if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
  75. throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again."));
  76. }
  77. }
  78. /**
  79. * Checking if current customer is guest
  80. *
  81. * @param int|null $customerId
  82. * @param int|null $customerType
  83. * @return bool
  84. */
  85. private function isCustomerGuest(?int $customerId, ?int $customerType): bool
  86. {
  87. if (null === $customerId || null === $customerType) {
  88. return true;
  89. }
  90. return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
  91. }
  92. }