|
| 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\NewsletterGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
| 14 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 15 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 16 | +use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException; |
| 17 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 18 | +use Magento\Framework\GraphQl\Query\EnumLookup; |
| 19 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 20 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 21 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 22 | +use Magento\Framework\Validator\EmailAddress as EmailValidator; |
| 23 | +use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberResourceModel; |
| 24 | +use Magento\Newsletter\Model\Subscriber; |
| 25 | +use Magento\Newsletter\Model\SubscriptionManagerInterface; |
| 26 | +use Magento\Store\Model\ScopeInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * Resolver class for the `subscribeEmailToNewsletter` mutation. Adds an email into a newsletter subscription. |
| 30 | + */ |
| 31 | +class SubscribeEmailToNewsletter implements ResolverInterface |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var CustomerAccountManagement |
| 35 | + */ |
| 36 | + private $customerAccountManagement; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var CustomerRepositoryInterface |
| 40 | + */ |
| 41 | + private $customerRepository; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var EmailValidator |
| 45 | + */ |
| 46 | + private $emailValidator; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var EnumLookup |
| 50 | + */ |
| 51 | + private $enumLookup; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var ScopeConfigInterface |
| 55 | + */ |
| 56 | + private $scopeConfig; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var SubscriberResourceModel |
| 60 | + */ |
| 61 | + private $subscriberResource; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var SubscriptionManagerInterface |
| 65 | + */ |
| 66 | + private $subscriptionManager; |
| 67 | + |
| 68 | + /** |
| 69 | + * SubscribeEmailToNewsletter constructor. |
| 70 | + * |
| 71 | + * @param CustomerAccountManagement $customerAccountManagement |
| 72 | + * @param CustomerRepositoryInterface $customerRepository |
| 73 | + * @param EmailValidator $emailValidator |
| 74 | + * @param EnumLookup $enumLookup |
| 75 | + * @param ScopeConfigInterface $scopeConfig |
| 76 | + * @param SubscriberResourceModel $subscriberResource |
| 77 | + * @param SubscriptionManagerInterface $subscriptionManager |
| 78 | + */ |
| 79 | + public function __construct( |
| 80 | + CustomerAccountManagement $customerAccountManagement, |
| 81 | + CustomerRepositoryInterface $customerRepository, |
| 82 | + EmailValidator $emailValidator, |
| 83 | + EnumLookup $enumLookup, |
| 84 | + ScopeConfigInterface $scopeConfig, |
| 85 | + SubscriberResourceModel $subscriberResource, |
| 86 | + SubscriptionManagerInterface $subscriptionManager |
| 87 | + ) { |
| 88 | + $this->customerAccountManagement = $customerAccountManagement; |
| 89 | + $this->customerRepository = $customerRepository; |
| 90 | + $this->emailValidator = $emailValidator; |
| 91 | + $this->enumLookup = $enumLookup; |
| 92 | + $this->scopeConfig = $scopeConfig; |
| 93 | + $this->subscriberResource = $subscriberResource; |
| 94 | + $this->subscriptionManager = $subscriptionManager; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @inheritdoc |
| 99 | + */ |
| 100 | + public function resolve( |
| 101 | + Field $field, |
| 102 | + $context, |
| 103 | + ResolveInfo $info, |
| 104 | + array $value = null, |
| 105 | + array $args = null |
| 106 | + ) { |
| 107 | + $email = trim($args['email']); |
| 108 | + |
| 109 | + if (empty($email)) { |
| 110 | + throw new GraphQlInputException(__('You must specify an email address to subscribe to a newsletter.')); |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + $currentUserId = (int)$context->getUserId(); |
| 115 | + $storeId = (int)$context->getExtensionAttributes()->getStore()->getId(); |
| 116 | + $websiteId = (int)$context->getExtensionAttributes()->getStore()->getWebsiteId(); |
| 117 | + |
| 118 | + $this->validateEmailFormat($email); |
| 119 | + $this->validateGuestSubscription($context); |
| 120 | + $this->validateEmailAvailable($email, $currentUserId, $websiteId); |
| 121 | + $this->validateAlreadySubscribed($email, $websiteId); |
| 122 | + |
| 123 | + $subscriber = $this->isCustomerSubscription($email, $currentUserId) |
| 124 | + ? $this->subscriptionManager->subscribeCustomer($currentUserId, $storeId) |
| 125 | + : $this->subscriptionManager->subscribe($email, $storeId); |
| 126 | + |
| 127 | + $status = $this->enumLookup->getEnumValueFromField( |
| 128 | + 'SubscriptionStatusesEnum', |
| 129 | + (string)$subscriber->getSubscriberStatus() |
| 130 | + ); |
| 131 | + } catch (LocalizedException $e) { |
| 132 | + throw new GraphQlInputException(__($e->getMessage())); |
| 133 | + } |
| 134 | + |
| 135 | + return [ |
| 136 | + 'status' => $status |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Validate the format of the email address |
| 142 | + * |
| 143 | + * @param string $email |
| 144 | + * @throws GraphQlInputException |
| 145 | + */ |
| 146 | + private function validateEmailFormat(string $email): void |
| 147 | + { |
| 148 | + if (!$this->emailValidator->isValid($email)) { |
| 149 | + throw new GraphQlInputException(__('Enter a valid email address.')); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Validate if a guest user can be subscribed to a newsletter. |
| 155 | + * |
| 156 | + * @param ContextInterface $context |
| 157 | + * @throws GraphQlInputException |
| 158 | + */ |
| 159 | + private function validateGuestSubscription(ContextInterface $context): void |
| 160 | + { |
| 161 | + if (false === $context->getExtensionAttributes()->getIsCustomer() |
| 162 | + && !$this->scopeConfig->getValue( |
| 163 | + Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG, |
| 164 | + ScopeInterface::SCOPE_STORE |
| 165 | + ) |
| 166 | + ) { |
| 167 | + throw new GraphQlInputException( |
| 168 | + __('Guests can not subscribe to the newsletter. You must create an account to subscribe.') |
| 169 | + ); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Validates that the email address isn't being used by a different account. |
| 175 | + * |
| 176 | + * @param string $email |
| 177 | + * @param int $currentUserId |
| 178 | + * @param int $websiteId |
| 179 | + * @throws GraphQlInputException |
| 180 | + * @throws LocalizedException |
| 181 | + * @throws NoSuchEntityException |
| 182 | + */ |
| 183 | + private function validateEmailAvailable(string $email, int $currentUserId, int $websiteId): void |
| 184 | + { |
| 185 | + if ($currentUserId > 0) { |
| 186 | + $customer = $this->customerRepository->getById($currentUserId); |
| 187 | + |
| 188 | + if ($customer->getEmail() != $email |
| 189 | + && !$this->customerAccountManagement->isEmailAvailable($email, $websiteId)) { |
| 190 | + throw new GraphQlInputException( |
| 191 | + __('This email address is already assigned to another user.') |
| 192 | + ); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Verify if email is already subscribed |
| 199 | + * |
| 200 | + * @param string $email |
| 201 | + * @param int $websiteId |
| 202 | + * @throws GraphQlAlreadyExistsException |
| 203 | + */ |
| 204 | + private function validateAlreadySubscribed(string $email, int $websiteId): void |
| 205 | + { |
| 206 | + $subscriberData = $this->subscriberResource->loadBySubscriberEmail($email, $websiteId); |
| 207 | + |
| 208 | + if (isset($subscriberData['subscriber_status']) |
| 209 | + && (int)$subscriberData['subscriber_status'] === Subscriber::STATUS_SUBSCRIBED) { |
| 210 | + throw new GraphQlAlreadyExistsException( |
| 211 | + __('This email address is already subscribed.') |
| 212 | + ); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Returns true if a provided email equals to a current customer one |
| 218 | + * |
| 219 | + * @param string $email |
| 220 | + * @param int $currentUserId |
| 221 | + * @return bool |
| 222 | + * @throws LocalizedException |
| 223 | + * @throws NoSuchEntityException |
| 224 | + */ |
| 225 | + private function isCustomerSubscription(string $email, int $currentUserId): bool |
| 226 | + { |
| 227 | + if ($currentUserId > 0) { |
| 228 | + $customer = $this->customerRepository->getById($currentUserId); |
| 229 | + |
| 230 | + if ($customer->getEmail() == $email) { |
| 231 | + return true; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + return false; |
| 236 | + } |
| 237 | +} |
0 commit comments