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