CreateAccount.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\AccountManagementInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  11. use Magento\Framework\Api\DataObjectHelper;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Exception\NoSuchEntityException;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. /**
  16. * Class CreateAccount creates new customer account
  17. */
  18. class CreateAccount
  19. {
  20. /**
  21. * @var DataObjectHelper
  22. */
  23. private $dataObjectHelper;
  24. /**
  25. * @var CustomerInterfaceFactory
  26. */
  27. private $customerFactory;
  28. /**
  29. * @var AccountManagementInterface
  30. */
  31. private $accountManagement;
  32. /**
  33. * @var StoreManagerInterface
  34. */
  35. private $storeManager;
  36. /**
  37. * @param DataObjectHelper $dataObjectHelper
  38. * @param CustomerInterfaceFactory $customerFactory
  39. * @param StoreManagerInterface $storeManager
  40. * @param AccountManagementInterface $accountManagement
  41. */
  42. public function __construct(
  43. DataObjectHelper $dataObjectHelper,
  44. CustomerInterfaceFactory $customerFactory,
  45. StoreManagerInterface $storeManager,
  46. AccountManagementInterface $accountManagement
  47. ) {
  48. $this->dataObjectHelper = $dataObjectHelper;
  49. $this->customerFactory = $customerFactory;
  50. $this->accountManagement = $accountManagement;
  51. $this->storeManager = $storeManager;
  52. }
  53. /**
  54. * Creates new customer account
  55. *
  56. * @param array $args
  57. * @return CustomerInterface
  58. * @throws LocalizedException
  59. * @throws NoSuchEntityException
  60. */
  61. public function execute(array $args): CustomerInterface
  62. {
  63. $customerDataObject = $this->customerFactory->create();
  64. $this->dataObjectHelper->populateWithArray(
  65. $customerDataObject,
  66. $args['input'],
  67. CustomerInterface::class
  68. );
  69. $store = $this->storeManager->getStore();
  70. $customerDataObject->setWebsiteId($store->getWebsiteId());
  71. $customerDataObject->setStoreId($store->getId());
  72. $password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null;
  73. return $this->accountManagement->createAccount($customerDataObject, $password);
  74. }
  75. }