Skip to content

Commit ee8ad58

Browse files
committed
Merge branch 'issue/732' of github.com:pmclain/graphql-ce into 2.3-develop-678
# Conflicts: # app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php # dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php
2 parents f69dee0 + 2dded7c commit ee8ad58

File tree

7 files changed

+314
-92
lines changed

7 files changed

+314
-92
lines changed

app/code/Magento/SendFriendGraphQl/Model/Provider/GetProduct.php renamed to app/code/Magento/SendFriendGraphQl/Model/Provider/GetVisibleProduct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Class GetProduct
1919
*/
20-
class GetProduct
20+
class GetVisibleProduct
2121
{
2222
/** @var ProductRepositoryInterface */
2323
private $productRepository;

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

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,113 +7,65 @@
77

88
namespace Magento\SendFriendGraphQl\Model\Resolver;
99

10-
use Magento\Framework\DataObjectFactory;
11-
use Magento\Framework\Event\ManagerInterface;
1210
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1312
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16-
use Magento\SendFriend\Model\SendFriend;
17-
use Magento\SendFriend\Model\SendFriendFactory;
18-
use Magento\SendFriendGraphQl\Model\Provider\GetProduct;
15+
use Magento\GraphQl\Model\Query\ContextInterface;
16+
use Magento\SendFriend\Helper\Data as SendFriendHelper;
17+
use Magento\SendFriendGraphQl\Model\SendFriend\SendEmail;
1918

2019
/**
2120
* @inheritdoc
2221
*/
2322
class SendEmailToFriend implements ResolverInterface
2423
{
2524
/**
26-
* @var SendFriendFactory
25+
* @var SendFriendHelper
2726
*/
28-
private $sendFriendFactory;
27+
private $sendFriendHelper;
2928

3029
/**
31-
* @var DataObjectFactory
30+
* @var SendEmail
3231
*/
33-
private $dataObjectFactory;
32+
private $sendEmail;
3433

3534
/**
36-
* @var ManagerInterface
37-
*/
38-
private $eventManager;
39-
40-
/**
41-
* @var GetProduct
42-
*/
43-
private $getProductProvider;
44-
45-
/**
46-
* @param SendFriendFactory $sendFriendFactory
47-
* @param DataObjectFactory $dataObjectFactory
48-
* @param ManagerInterface $eventManager
49-
* @param GetProduct $getProductProvider
35+
* @param SendEmail $sendEmail
36+
* @param SendFriendHelper $sendFriendHelper
5037
*/
5138
public function __construct(
52-
SendFriendFactory $sendFriendFactory,
53-
DataObjectFactory $dataObjectFactory,
54-
ManagerInterface $eventManager,
55-
GetProduct $getProductProvider
39+
SendEmail $sendEmail,
40+
SendFriendHelper $sendFriendHelper
5641
) {
57-
$this->sendFriendFactory = $sendFriendFactory;
58-
$this->dataObjectFactory = $dataObjectFactory;
59-
$this->eventManager = $eventManager;
60-
$this->getProductProvider = $getProductProvider;
42+
$this->sendEmail = $sendEmail;
43+
$this->sendFriendHelper = $sendFriendHelper;
6144
}
6245

6346
/**
6447
* @inheritdoc
6548
*/
6649
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
6750
{
68-
/** @var SendFriend $sendFriend */
69-
$sendFriend = $this->sendFriendFactory->create();
70-
71-
if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
72-
throw new GraphQlInputException(
73-
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
74-
);
51+
/** @var ContextInterface $context */
52+
if (!$this->sendFriendHelper->isAllowForGuest()
53+
&& false === $context->getExtensionAttributes()->getIsCustomer()
54+
) {
55+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
7556
}
7657

77-
$product = $this->getProductProvider->execute($args['input']['product_id']);
78-
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
79-
$sendFriend->setProduct($product);
80-
8158
$senderData = $this->extractSenderData($args);
82-
$sendFriend->setSender($senderData);
83-
8459
$recipientsData = $this->extractRecipientsData($args);
85-
$sendFriend->setRecipients($recipientsData);
86-
87-
$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
88-
$sendFriend->send();
8960

61+
$this->sendEmail->execute(
62+
$args['input']['product_id'],
63+
$senderData,
64+
$recipientsData
65+
);
9066
return array_merge($senderData, $recipientsData);
9167
}
9268

93-
/**
94-
* Validate send friend model
95-
*
96-
* @param SendFriend $sendFriend
97-
* @param array $senderData
98-
* @param array $recipientsData
99-
* @return void
100-
* @throws GraphQlInputException
101-
*/
102-
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
103-
{
104-
$sender = $this->dataObjectFactory->create()->setData($senderData['sender']);
105-
$sendFriend->setData('_sender', $sender);
106-
107-
$emails = array_column($recipientsData['recipients'], 'email');
108-
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
109-
$sendFriend->setData('_recipients', $recipients);
110-
111-
$validationResult = $sendFriend->validate();
112-
if ($validationResult !== true) {
113-
throw new GraphQlInputException(__(implode($validationResult)));
114-
}
115-
}
116-
11769
/**
11870
* Extract recipients data
11971
*
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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\SendFriend;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\DataObjectFactory;
13+
use Magento\Framework\Event\ManagerInterface;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
17+
use Magento\SendFriend\Model\SendFriend;
18+
use Magento\SendFriend\Model\SendFriendFactory;
19+
20+
/**
21+
* Send Product Email to Friend(s)
22+
*/
23+
class SendEmail
24+
{
25+
/**
26+
* @var DataObjectFactory
27+
*/
28+
private $dataObjectFactory;
29+
30+
/**
31+
* @var ProductRepositoryInterface
32+
*/
33+
private $productRepository;
34+
35+
/**
36+
* @var SendFriendFactory
37+
*/
38+
private $sendFriendFactory;
39+
40+
/**
41+
* @var ManagerInterface
42+
*/
43+
private $eventManager;
44+
45+
/**
46+
* @param DataObjectFactory $dataObjectFactory
47+
* @param ProductRepositoryInterface $productRepository
48+
* @param SendFriendFactory $sendFriendFactory
49+
* @param ManagerInterface $eventManager
50+
*/
51+
public function __construct(
52+
DataObjectFactory $dataObjectFactory,
53+
ProductRepositoryInterface $productRepository,
54+
SendFriendFactory $sendFriendFactory,
55+
ManagerInterface $eventManager
56+
) {
57+
$this->dataObjectFactory = $dataObjectFactory;
58+
$this->productRepository = $productRepository;
59+
$this->sendFriendFactory = $sendFriendFactory;
60+
$this->eventManager = $eventManager;
61+
}
62+
63+
/**
64+
* Send product email to friend(s)
65+
*
66+
* @param int $productId
67+
* @param array $senderData
68+
* @param array $recipientsData
69+
* @throws GraphQlInputException
70+
* @throws GraphQlNoSuchEntityException
71+
* @throws \Magento\Framework\Exception\LocalizedException
72+
*/
73+
public function execute(int $productId, array $senderData, array $recipientsData): void
74+
{
75+
/** @var SendFriend $sendFriend */
76+
$sendFriend = $this->sendFriendFactory->create();
77+
78+
if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
79+
throw new GraphQlInputException(
80+
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
81+
);
82+
}
83+
84+
$product = $this->getProduct($productId);
85+
86+
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
87+
88+
$sendFriend->setProduct($product);
89+
$sendFriend->setSender($senderData);
90+
$sendFriend->setRecipients($recipientsData);
91+
92+
$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
93+
94+
$sendFriend->send();
95+
}
96+
97+
/**
98+
* Validate send friend model
99+
*
100+
* @param SendFriend $sendFriend
101+
* @param array $senderData
102+
* @param array $recipientsData
103+
* @return void
104+
* @throws GraphQlInputException
105+
*/
106+
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
107+
{
108+
$sender = $this->dataObjectFactory->create()->setData($senderData['sender']);
109+
$sendFriend->setData('_sender', $sender);
110+
111+
$emails = array_column($recipientsData['recipients'], 'email');
112+
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
113+
$sendFriend->setData('_recipients', $recipients);
114+
115+
$validationResult = $sendFriend->validate();
116+
if ($validationResult !== true) {
117+
throw new GraphQlInputException(__(implode($validationResult)));
118+
}
119+
}
120+
121+
/**
122+
* Get product
123+
*
124+
* @param int $productId
125+
* @return ProductInterface
126+
* @throws GraphQlNoSuchEntityException
127+
*/
128+
private function getProduct(int $productId): ProductInterface
129+
{
130+
try {
131+
$product = $this->productRepository->getById($productId);
132+
if (!$product->isVisibleInCatalog()) {
133+
throw new GraphQlNoSuchEntityException(
134+
__("The product that was requested doesn't exist. Verify the product and try again.")
135+
);
136+
}
137+
} catch (NoSuchEntityException $e) {
138+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
139+
}
140+
return $product;
141+
}
142+
}

app/code/Magento/SendFriendGraphQl/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
"php": "~7.1.3||~7.2.0",
77
"magento/framework": "*",
88
"magento/module-catalog": "*",
9-
"magento/module-send-friend": "*"
10-
},
11-
"suggest": {
9+
"magento/module-send-friend": "*",
1210
"magento/module-graph-ql": "*"
1311
},
1412
"license": [

0 commit comments

Comments
 (0)