|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained from |
| 13 | + * Adobe. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Magento\CustomerGraphQl\Model\Resolver; |
| 18 | + |
| 19 | +use Magento\Customer\Api\AccountManagementInterface; |
| 20 | +use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData; |
| 21 | +use Magento\Framework\Exception\State\InputMismatchException; |
| 22 | +use Magento\Framework\Exception\State\InvalidTransitionException; |
| 23 | +use Magento\Framework\Exception\StateException; |
| 24 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 25 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 26 | +use Magento\Framework\GraphQl\Query\Resolver\Value; |
| 27 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 28 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 29 | +use Magento\Framework\Validator\EmailAddress as EmailValidator; |
| 30 | + |
| 31 | +/** |
| 32 | + * Customer email confirmation, used for GraphQL request processing |
| 33 | + */ |
| 34 | +class ConfirmEmail implements ResolverInterface |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @param AccountManagementInterface $accountManagement |
| 38 | + * @param EmailValidator $emailValidator |
| 39 | + * @param ExtractCustomerData $extractCustomerData |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + private readonly AccountManagementInterface $accountManagement, |
| 43 | + private readonly EmailValidator $emailValidator, |
| 44 | + private readonly ExtractCustomerData $extractCustomerData |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Confirm customer email mutation |
| 50 | + * |
| 51 | + * @param \Magento\Framework\GraphQl\Config\Element\Field $field |
| 52 | + * @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context |
| 53 | + * @param ResolveInfo $info |
| 54 | + * @param array|null $value |
| 55 | + * @param array|null $args |
| 56 | + * @return array|Value |
| 57 | + * @throws \Exception |
| 58 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 59 | + */ |
| 60 | + public function resolve( |
| 61 | + Field $field, |
| 62 | + $context, |
| 63 | + ResolveInfo $info, |
| 64 | + array $value = null, |
| 65 | + array $args = null |
| 66 | + ) { |
| 67 | + if (!$this->emailValidator->isValid($args['input']['email'])) { |
| 68 | + throw new GraphQlInputException(__('Email is invalid')); |
| 69 | + } |
| 70 | + try { |
| 71 | + $customer = $this->accountManagement->activate($args['input']['email'], $args['input']['confirmation_key']); |
| 72 | + } catch (InvalidTransitionException | InputMismatchException $e) { |
| 73 | + throw new GraphQlInputException(__($e->getRawMessage())); |
| 74 | + } catch (StateException) { |
| 75 | + throw new GraphQlInputException(__('This confirmation key is invalid or has expired.')); |
| 76 | + } catch (\Exception) { |
| 77 | + throw new GraphQlInputException(__('There was an error confirming the account')); |
| 78 | + } |
| 79 | + return ['customer' => $this->extractCustomerData->execute($customer)]; |
| 80 | + } |
| 81 | +} |
0 commit comments