|
| 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\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\DataObjectFactory; |
| 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\Query\ResolverInterface; |
| 16 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 17 | +use Magento\SendFriend\Model\SendFriend; |
| 18 | + |
| 19 | +class SendEmailToFriend implements ResolverInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var SendFriend |
| 23 | + */ |
| 24 | + private $sendFriend; |
| 25 | + /** |
| 26 | + * @var ProductRepositoryInterface |
| 27 | + */ |
| 28 | + private $productRepository; |
| 29 | + /** |
| 30 | + * @var DataObjectFactory |
| 31 | + */ |
| 32 | + private $dataObjectFactory; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + SendFriend $sendFriend, |
| 36 | + ProductRepositoryInterface $productRepository, |
| 37 | + DataObjectFactory $dataObjectFactory |
| 38 | + ) { |
| 39 | + $this->sendFriend = $sendFriend; |
| 40 | + $this->productRepository = $productRepository; |
| 41 | + $this->dataObjectFactory = $dataObjectFactory; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritdoc |
| 46 | + */ |
| 47 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 48 | + { |
| 49 | + if ($this->sendFriend->getMaxSendsToFriend() && $this->sendFriend->isExceedLimit()) { |
| 50 | + throw new GraphQlInputException(__('You can\'t send messages more than %1 times an hour.', |
| 51 | + $this->sendFriend->getMaxSendsToFriend() |
| 52 | + )); |
| 53 | + } |
| 54 | + |
| 55 | + $product = $this->getProductFromRepository($args['input']['params']['product_id']); |
| 56 | + $senderArray = $this->getSenderArrayFromArgs($args); |
| 57 | + $recipientsArray = $this->getRecipientsArray($args); |
| 58 | + //@todo clarify if event should be dispatched |
| 59 | + //$this->_eventManager->dispatch('sendfriend_product', ['product' => $product]); |
| 60 | + $this->sendFriend->setSender($senderArray); |
| 61 | + $this->sendFriend->setRecipients($recipientsArray); |
| 62 | + $this->sendFriend->setProduct($product); |
| 63 | + |
| 64 | + $this->prepareDataForValidation($args, $recipientsArray); |
| 65 | + $validationResult = $this->sendFriend->validate(); |
| 66 | + $this->addRecipientNameValidation($args); |
| 67 | + if ($validationResult !== true) { |
| 68 | + throw new GraphQlInputException(__(implode($validationResult))); |
| 69 | + } |
| 70 | + |
| 71 | + $this->sendFriend->send(); |
| 72 | + |
| 73 | + return array_merge($senderArray, $recipientsArray); |
| 74 | + } |
| 75 | + |
| 76 | + private function prepareDataForValidation(array $args, array $recipientsArray): void |
| 77 | + { |
| 78 | + $sender = $this->dataObjectFactory->create()->setData([ |
| 79 | + 'name' => $args['input']['sender']['name'], |
| 80 | + 'email'=> $args['input']['sender']['email'], |
| 81 | + 'message' => $args['input']['sender']['message'], |
| 82 | + ]); |
| 83 | + $emails = []; |
| 84 | + foreach ($recipientsArray['recipients'] as $recipient) { |
| 85 | + $emails[] = $recipient['email']; |
| 86 | + } |
| 87 | + $recipients = $this->dataObjectFactory->create()->setData('emails', $emails); |
| 88 | + |
| 89 | + $this->sendFriend->setData('_sender', $sender); |
| 90 | + $this->sendFriend->setData('_recipients', $recipients); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param array $args |
| 95 | + * @throws GraphQlInputException |
| 96 | + */ |
| 97 | + private function addRecipientNameValidation(array $args): void |
| 98 | + { |
| 99 | + foreach ($args['input']['recipients'] as $recipient) { |
| 100 | + if (empty($recipient['name'])) { |
| 101 | + throw new GraphQlInputException( |
| 102 | + __('Please Provide Name for Recipient with this Email Address: ' . $recipient['email'] |
| 103 | + )); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param int $productId |
| 110 | + * @return bool|\Magento\Catalog\Api\Data\ProductInterface |
| 111 | + */ |
| 112 | + private function getProductFromRepository(int $productId) |
| 113 | + { |
| 114 | + try { |
| 115 | + $product = $this->productRepository->getById($productId); |
| 116 | + if (!$product->isVisibleInCatalog()) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } catch (NoSuchEntityException $noEntityException) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + return $product; |
| 124 | + } |
| 125 | + |
| 126 | + private function getRecipientsArray(array $args): array |
| 127 | + { |
| 128 | + $recipientsArray = []; |
| 129 | + foreach ($args['input']['recipients'] as $recipient) { |
| 130 | + $recipientsArray[] = [ |
| 131 | + 'name' => $recipient['name'], |
| 132 | + 'email' => $recipient['email'], |
| 133 | + ]; |
| 134 | + } |
| 135 | + return ['recipients' => $recipientsArray]; |
| 136 | + } |
| 137 | + |
| 138 | + private function getSenderArrayFromArgs(array $args): array |
| 139 | + { |
| 140 | + return ['sender' => [ |
| 141 | + 'name' => $args['input']['sender']['name'], |
| 142 | + 'email' => $args['input']['sender']['email'], |
| 143 | + 'message' => $args['input']['sender']['message'], |
| 144 | + ] |
| 145 | + ]; |
| 146 | + } |
| 147 | +} |
0 commit comments