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