CreateCustomer.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus;
  9. use Magento\CustomerGraphQl\Model\Customer\CreateAccount;
  10. use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
  11. use Magento\CustomerGraphQl\Model\Customer\SetUpUserContext;
  12. use Magento\Framework\Exception\State\InputMismatchException;
  13. use Magento\Framework\GraphQl\Config\Element\Field;
  14. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  15. use Magento\Framework\GraphQl\Query\ResolverInterface;
  16. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  17. use Magento\Framework\Validator\Exception as ValidatorException;
  18. /**
  19. * Create customer account resolver
  20. */
  21. class CreateCustomer implements ResolverInterface
  22. {
  23. /**
  24. * @var CustomerDataProvider
  25. */
  26. private $customerDataProvider;
  27. /**
  28. * @var ChangeSubscriptionStatus
  29. */
  30. private $changeSubscriptionStatus;
  31. /**
  32. * @var CreateAccount
  33. */
  34. private $createAccount;
  35. /**
  36. * @var SetUpUserContext
  37. */
  38. private $setUpUserContext;
  39. /**
  40. * @param CustomerDataProvider $customerDataProvider
  41. * @param ChangeSubscriptionStatus $changeSubscriptionStatus
  42. * @param SetUpUserContext $setUpUserContext
  43. * @param CreateAccount $createAccount
  44. */
  45. public function __construct(
  46. CustomerDataProvider $customerDataProvider,
  47. ChangeSubscriptionStatus $changeSubscriptionStatus,
  48. SetUpUserContext $setUpUserContext,
  49. CreateAccount $createAccount
  50. ) {
  51. $this->customerDataProvider = $customerDataProvider;
  52. $this->changeSubscriptionStatus = $changeSubscriptionStatus;
  53. $this->createAccount = $createAccount;
  54. $this->setUpUserContext = $setUpUserContext;
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function resolve(
  60. Field $field,
  61. $context,
  62. ResolveInfo $info,
  63. array $value = null,
  64. array $args = null
  65. ) {
  66. if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) {
  67. throw new GraphQlInputException(__('"input" value should be specified'));
  68. }
  69. try {
  70. $customer = $this->createAccount->execute($args);
  71. $customerId = (int)$customer->getId();
  72. $this->setUpUserContext->execute($context, $customer);
  73. if (array_key_exists('is_subscribed', $args['input'])) {
  74. if ($args['input']['is_subscribed']) {
  75. $this->changeSubscriptionStatus->execute($customerId, true);
  76. }
  77. }
  78. $data = $this->customerDataProvider->getCustomerById($customerId);
  79. } catch (ValidatorException $e) {
  80. throw new GraphQlInputException(__($e->getMessage()));
  81. } catch (InputMismatchException $e) {
  82. throw new GraphQlInputException(__($e->getMessage()));
  83. }
  84. return ['customer' => $data];
  85. }
  86. }