SendEmailToFriend.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\SendFriendGraphQl\Model\Resolver;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Framework\DataObjectFactory;
  11. use Magento\Framework\Event\ManagerInterface;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Framework\GraphQl\Config\Element\Field;
  14. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  15. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  16. use Magento\Framework\GraphQl\Query\ResolverInterface;
  17. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  18. use Magento\SendFriend\Model\SendFriend;
  19. use Magento\SendFriend\Model\SendFriendFactory;
  20. /**
  21. * @inheritdoc
  22. */
  23. class SendEmailToFriend implements ResolverInterface
  24. {
  25. /**
  26. * @var SendFriendFactory
  27. */
  28. private $sendFriendFactory;
  29. /**
  30. * @var ProductRepositoryInterface
  31. */
  32. private $productRepository;
  33. /**
  34. * @var DataObjectFactory
  35. */
  36. private $dataObjectFactory;
  37. /**
  38. * @var ManagerInterface
  39. */
  40. private $eventManager;
  41. /**
  42. * @param SendFriendFactory $sendFriendFactory
  43. * @param ProductRepositoryInterface $productRepository
  44. * @param DataObjectFactory $dataObjectFactory
  45. * @param ManagerInterface $eventManager
  46. */
  47. public function __construct(
  48. SendFriendFactory $sendFriendFactory,
  49. ProductRepositoryInterface $productRepository,
  50. DataObjectFactory $dataObjectFactory,
  51. ManagerInterface $eventManager
  52. ) {
  53. $this->sendFriendFactory = $sendFriendFactory;
  54. $this->productRepository = $productRepository;
  55. $this->dataObjectFactory = $dataObjectFactory;
  56. $this->eventManager = $eventManager;
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  62. {
  63. /** @var SendFriend $sendFriend */
  64. $sendFriend = $this->sendFriendFactory->create();
  65. if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
  66. throw new GraphQlInputException(
  67. __('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
  68. );
  69. }
  70. $product = $this->getProduct($args['input']['product_id']);
  71. $this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
  72. $sendFriend->setProduct($product);
  73. $senderData = $this->extractSenderData($args);
  74. $sendFriend->setSender($senderData);
  75. $recipientsData = $this->extractRecipientsData($args);
  76. $sendFriend->setRecipients($recipientsData);
  77. $this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
  78. $sendFriend->send();
  79. return array_merge($senderData, $recipientsData);
  80. }
  81. /**
  82. * Validate send friend model
  83. *
  84. * @param SendFriend $sendFriend
  85. * @param array $senderData
  86. * @param array $recipientsData
  87. * @return void
  88. * @throws GraphQlInputException
  89. */
  90. private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
  91. {
  92. $sender = $this->dataObjectFactory->create()->setData($senderData['sender']);
  93. $sendFriend->setData('_sender', $sender);
  94. $emails = array_column($recipientsData['recipients'], 'email');
  95. $recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
  96. $sendFriend->setData('_recipients', $recipients);
  97. $validationResult = $sendFriend->validate();
  98. if ($validationResult !== true) {
  99. throw new GraphQlInputException(__(implode($validationResult)));
  100. }
  101. }
  102. /**
  103. * Get product
  104. *
  105. * @param int $productId
  106. * @return ProductInterface
  107. * @throws GraphQlNoSuchEntityException
  108. */
  109. private function getProduct(int $productId): ProductInterface
  110. {
  111. try {
  112. $product = $this->productRepository->getById($productId);
  113. if (!$product->isVisibleInCatalog()) {
  114. throw new GraphQlNoSuchEntityException(
  115. __("The product that was requested doesn't exist. Verify the product and try again.")
  116. );
  117. }
  118. } catch (NoSuchEntityException $e) {
  119. throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
  120. }
  121. return $product;
  122. }
  123. /**
  124. * Extract recipients data
  125. *
  126. * @param array $args
  127. * @return array
  128. * @throws GraphQlInputException
  129. */
  130. private function extractRecipientsData(array $args): array
  131. {
  132. $recipients = [];
  133. foreach ($args['input']['recipients'] as $recipient) {
  134. if (empty($recipient['name'])) {
  135. throw new GraphQlInputException(__('Please provide Name for all of recipients.'));
  136. }
  137. if (empty($recipient['email'])) {
  138. throw new GraphQlInputException(__('Please provide Email for all of recipients.'));
  139. }
  140. $recipients[] = [
  141. 'name' => $recipient['name'],
  142. 'email' => $recipient['email'],
  143. ];
  144. }
  145. return ['recipients' => $recipients];
  146. }
  147. /**
  148. * Extract sender data
  149. *
  150. * @param array $args
  151. * @return array
  152. * @throws GraphQlInputException
  153. */
  154. private function extractSenderData(array $args): array
  155. {
  156. if (empty($args['input']['sender']['name'])) {
  157. throw new GraphQlInputException(__('Please provide Name of sender.'));
  158. }
  159. if (empty($args['input']['sender']['email'])) {
  160. throw new GraphQlInputException(__('Please provide Email of sender.'));
  161. }
  162. if (empty($args['input']['sender']['message'])) {
  163. throw new GraphQlInputException(__('Please provide Message.'));
  164. }
  165. return [
  166. 'sender' => [
  167. 'name' => $args['input']['sender']['name'],
  168. 'email' => $args['input']['sender']['email'],
  169. 'message' => $args['input']['sender']['message'],
  170. ],
  171. ];
  172. }
  173. }