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