Skip to content

Commit faa8535

Browse files
author
Valeriy Nayda
committed
GraphQL-202: Products: Email to a Friend
1 parent 1fb624d commit faa8535

File tree

6 files changed

+145
-141
lines changed

6 files changed

+145
-141
lines changed

app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php

Lines changed: 115 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,192 @@
77

88
namespace Magento\SendFriendGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Framework\DataObjectFactory;
13+
use Magento\Framework\Event\ManagerInterface;
1214
use Magento\Framework\Exception\NoSuchEntityException;
1315
use Magento\Framework\GraphQl\Config\Element\Field;
1416
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1518
use Magento\Framework\GraphQl\Query\ResolverInterface;
1619
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1720
use Magento\SendFriend\Model\SendFriend;
21+
use Magento\SendFriend\Model\SendFriendFactory;
1822

23+
/**
24+
* @inheritdoc
25+
*/
1926
class SendEmailToFriend implements ResolverInterface
2027
{
2128
/**
22-
* @var SendFriend
29+
* @var SendFriendFactory
2330
*/
24-
private $sendFriend;
31+
private $sendFriendFactory;
32+
2533
/**
2634
* @var ProductRepositoryInterface
2735
*/
2836
private $productRepository;
37+
2938
/**
3039
* @var DataObjectFactory
3140
*/
3241
private $dataObjectFactory;
3342

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+
*/
3454
public function __construct(
35-
SendFriend $sendFriend,
55+
SendFriendFactory $sendFriendFactory,
3656
ProductRepositoryInterface $productRepository,
37-
DataObjectFactory $dataObjectFactory
57+
DataObjectFactory $dataObjectFactory,
58+
ManagerInterface $eventManager
3859
) {
39-
$this->sendFriend = $sendFriend;
60+
$this->sendFriendFactory = $sendFriendFactory;
4061
$this->productRepository = $productRepository;
4162
$this->dataObjectFactory = $dataObjectFactory;
63+
$this->eventManager = $eventManager;
4264
}
4365

4466
/**
4567
* @inheritdoc
4668
*/
4769
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
4870
{
49-
if ($this->sendFriend->getMaxSendsToFriend() && $this->sendFriend->isExceedLimit()) {
71+
/** @var SendFriend $sendFriend */
72+
$sendFriend = $this->sendFriendFactory->create();
73+
74+
if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
5075
throw new GraphQlInputException(__('You can\'t send messages more than %1 times an hour.',
51-
$this->sendFriend->getMaxSendsToFriend()
76+
$sendFriend->getMaxSendsToFriend()
5277
));
5378
}
5479

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-
}
80+
$product = $this->getProduct($args['input']['product_id']);
81+
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
82+
$sendFriend->setProduct($product);
7083

71-
$this->sendFriend->send();
84+
$senderData = $this->extractSenderData($args);
85+
$sendFriend->setSender($senderData);
7286

73-
return array_merge($senderArray, $recipientsArray);
74-
}
87+
$recipientsData = $this->extractRecipientsData($args);
88+
$sendFriend->setRecipients($recipientsData);
7589

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);
90+
$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
91+
$sendFriend->send();
8892

89-
$this->sendFriend->setData('_sender', $sender);
90-
$this->sendFriend->setData('_recipients', $recipients);
93+
return array_merge($senderData, $recipientsData);
9194
}
9295

9396
/**
94-
* @param array $args
97+
* Validate send friend model
98+
*
99+
* @param SendFriend $sendFriend
100+
* @param array $senderData
101+
* @param array $recipientsData
102+
* @return void
95103
* @throws GraphQlInputException
96104
*/
97-
private function addRecipientNameValidation(array $args): void
105+
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
98106
{
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-
}
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)));
105117
}
106118
}
107119

108120
/**
121+
* Get product
122+
*
109123
* @param int $productId
110-
* @return bool|\Magento\Catalog\Api\Data\ProductInterface
124+
* @return ProductInterface
125+
* @throws GraphQlNoSuchEntityException
111126
*/
112-
private function getProductFromRepository(int $productId)
127+
private function getProduct(int $productId): ProductInterface
113128
{
114129
try {
115130
$product = $this->productRepository->getById($productId);
116131
if (!$product->isVisibleInCatalog()) {
117-
return false;
132+
throw new GraphQlNoSuchEntityException(
133+
__("The product that was requested doesn't exist. Verify the product and try again.")
134+
);
118135
}
119-
} catch (NoSuchEntityException $noEntityException) {
120-
return false;
136+
} catch (NoSuchEntityException $e) {
137+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
121138
}
122-
123139
return $product;
124140
}
125141

126-
private function getRecipientsArray(array $args): array
142+
/**
143+
* Extract recipients data
144+
*
145+
* @param array $args
146+
* @return array
147+
* @throws GraphQlInputException
148+
*/
149+
private function extractRecipientsData(array $args): array
127150
{
128-
$recipientsArray = [];
151+
$recipients = [];
129152
foreach ($args['input']['recipients'] as $recipient) {
130-
$recipientsArray[] = [
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[] = [
131162
'name' => $recipient['name'],
132163
'email' => $recipient['email'],
133164
];
134165
}
135-
return ['recipients' => $recipientsArray];
166+
return ['recipients' => $recipients];
136167
}
137168

138-
private function getSenderArrayFromArgs(array $args): array
169+
/**
170+
* Extract sender data
171+
*
172+
* @param array $args
173+
* @return array
174+
* @throws GraphQlInputException
175+
*/
176+
private function extractSenderData(array $args): array
139177
{
140-
return ['sender' => [
141-
'name' => $args['input']['sender']['name'],
142-
'email' => $args['input']['sender']['email'],
143-
'message' => $args['input']['sender']['message'],
144-
]
145-
];
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+
];
146197
}
147198
}

app/code/Magento/SendFriendGraphQl/Model/Validation/Validation.php

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "magento/module-send-friend-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-catalog": "*",
9+
"magento/module-send-friend": "*"
10+
},
11+
"suggest": {
12+
"magento/module-graph-ql": "*"
13+
},
14+
"license": [
15+
"OSL-3.0",
16+
"AFL-3.0"
17+
],
18+
"autoload": {
19+
"files": [
20+
"registration.php"
21+
],
22+
"psr-4": {
23+
"Magento\\SendFriendGraphGl\\": ""
24+
}
25+
}
26+
}

app/code/Magento/SendFriendGraphQl/etc/schema.graphqls

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ type Mutation {
66
}
77

88
input SendEmailToFriendSenderInput {
9+
product_id: Int!
910
sender: Sender!
10-
params: Params!
11-
recipients: [Recipient]
11+
recipients: [Recipient!]!
1212
}
1313

1414
type Sender {
1515
name: String!
1616
email: String!
1717
message: String!
1818
}
19-
type Params {
20-
product_id: Int!
21-
category_id: Int!
22-
}
2319

2420
type Recipient {
2521
name: String!
2622
email: String!
2723
}
2824

29-
3025
type SendEmailToFriendOutput {
3126
sender: Sender
3227
recipients: [Recipient]

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
"magento/module-search": "*",
209209
"magento/module-security": "*",
210210
"magento/module-send-friend": "*",
211+
"magento/module-send-friend-graph-ql": "*",
211212
"magento/module-shipping": "*",
212213
"magento/module-signifyd": "*",
213214
"magento/module-sitemap": "*",

0 commit comments

Comments
 (0)