Skip to content

Commit 505ac52

Browse files
author
Tobias Maile
committed
GraphQL-202: [Mutations] adds sendEmailFriend Logic
1 parent fcf9eed commit 505ac52

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
10+
<module name="Magento_SendFriendGraphGl"/>
11+
</config>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Mutation {
5+
sendEmailToFriend (input: SendEmailToFriendSenderInput): SendEmailToFriendOutput @resolver(class: "\\Magento\\SendFriendGraphQl\\Model\\Resolver\\SendEmailToFriend") @doc(description:"@todo")
6+
}
7+
8+
input SendEmailToFriendSenderInput {
9+
sender: Sender!
10+
params: Params!
11+
recipients: [Recipient]
12+
}
13+
14+
type Sender {
15+
name: String!
16+
email: String!
17+
message: String!
18+
}
19+
#@todo Prams can be removed if dispatching of event in app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php not needed
20+
type Params {
21+
product_id: Int!
22+
}
23+
24+
type Recipient {
25+
name: String!
26+
email: String!
27+
}
28+
29+
30+
type SendEmailToFriendOutput {
31+
sender: Sender
32+
recipients: [Recipient]
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_SendFriendGraphGl', __DIR__);

0 commit comments

Comments
 (0)