UpdateCustomerData.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\Exception\AlreadyExistsException;
  10. use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
  11. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  12. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. /**
  15. * Update customer data
  16. */
  17. class UpdateCustomerData
  18. {
  19. /**
  20. * @var CustomerRepositoryInterface
  21. */
  22. private $customerRepository;
  23. /**
  24. * @var StoreManagerInterface
  25. */
  26. private $storeManager;
  27. /**
  28. * @var CheckCustomerPassword
  29. */
  30. private $checkCustomerPassword;
  31. /**
  32. * @param CustomerRepositoryInterface $customerRepository
  33. * @param StoreManagerInterface $storeManager
  34. * @param CheckCustomerPassword $checkCustomerPassword
  35. */
  36. public function __construct(
  37. CustomerRepositoryInterface $customerRepository,
  38. StoreManagerInterface $storeManager,
  39. CheckCustomerPassword $checkCustomerPassword
  40. ) {
  41. $this->customerRepository = $customerRepository;
  42. $this->storeManager = $storeManager;
  43. $this->checkCustomerPassword = $checkCustomerPassword;
  44. }
  45. /**
  46. * Update account information
  47. *
  48. * @param int $customerId
  49. * @param array $data
  50. * @return void
  51. * @throws GraphQlNoSuchEntityException
  52. * @throws GraphQlInputException
  53. * @throws GraphQlAlreadyExistsException
  54. */
  55. public function execute(int $customerId, array $data): void
  56. {
  57. $customer = $this->customerRepository->getById($customerId);
  58. if (isset($data['firstname'])) {
  59. $customer->setFirstname($data['firstname']);
  60. }
  61. if (isset($data['lastname'])) {
  62. $customer->setLastname($data['lastname']);
  63. }
  64. if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
  65. if (!isset($data['password']) || empty($data['password'])) {
  66. throw new GraphQlInputException(__('Provide the current "password" to change "email".'));
  67. }
  68. $this->checkCustomerPassword->execute($data['password'], $customerId);
  69. $customer->setEmail($data['email']);
  70. }
  71. $customer->setStoreId($this->storeManager->getStore()->getId());
  72. try {
  73. $this->customerRepository->save($customer);
  74. } catch (AlreadyExistsException $e) {
  75. throw new GraphQlAlreadyExistsException(
  76. __('A customer with the same email address already exists in an associated website.'),
  77. $e
  78. );
  79. }
  80. }
  81. }