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