Skip to content

Commit 5379d12

Browse files
ENGCOM-5347: magento/graphql-ce:magento#678 Send email to friend magento#752
- Merge Pull Request magento/graphql-ce#752 from rleshchenko/graphql-ce:2.3-develop-678 - Merged commits: 1. 63762d6 2. bbe60dd 3. 0e59bcf 4. 2dded7c 5. a1d87ba 6. e8c102a 7. 07e081f 8. f69dee0 9. ee8ad58 10. 193d4d1
2 parents 1e58f70 + 193d4d1 commit 5379d12

File tree

7 files changed

+366
-123
lines changed

7 files changed

+366
-123
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Provider;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product\Visibility;
12+
use Magento\Catalog\Api\Data\ProductInterface;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
16+
/**
17+
* Returns product if it is visible in catalog.
18+
*/
19+
class GetVisibleProduct
20+
{
21+
/** @var ProductRepositoryInterface */
22+
private $productRepository;
23+
24+
/** @var Visibility */
25+
private $visibility;
26+
27+
/**
28+
* @param ProductRepositoryInterface $productRepository
29+
* @param Visibility $visibility
30+
*/
31+
public function __construct(
32+
ProductRepositoryInterface $productRepository,
33+
Visibility $visibility
34+
) {
35+
$this->productRepository = $productRepository;
36+
$this->visibility = $visibility;
37+
}
38+
39+
/**
40+
* Get product
41+
*
42+
* @param int $productId
43+
* @return ProductInterface
44+
* @throws GraphQlNoSuchEntityException
45+
*/
46+
public function execute(int $productId): ProductInterface
47+
{
48+
try {
49+
$product = $this->productRepository->getById($productId);
50+
51+
if (!in_array(
52+
$product->getVisibility(),
53+
$this->visibility->getVisibleInCatalogIds()
54+
)) {
55+
throw new GraphQlNoSuchEntityException(
56+
__("The product that was requested doesn't exist. Verify the product and try again.")
57+
);
58+
}
59+
} catch (NoSuchEntityException $e) {
60+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
61+
}
62+
return $product;
63+
}
64+
}

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

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

88
namespace Magento\SendFriendGraphQl\Model\Resolver;
99

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;
1510
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1612
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1813
use Magento\Framework\GraphQl\Query\ResolverInterface;
1914
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
20-
use Magento\SendFriend\Model\SendFriend;
21-
use Magento\SendFriend\Model\SendFriendFactory;
15+
use Magento\GraphQl\Model\Query\ContextInterface;
16+
use Magento\SendFriend\Helper\Data as SendFriendHelper;
17+
use Magento\SendFriendGraphQl\Model\SendFriend\SendEmail;
2218

2319
/**
2420
* @inheritdoc
2521
*/
2622
class SendEmailToFriend implements ResolverInterface
2723
{
2824
/**
29-
* @var SendFriendFactory
25+
* @var SendFriendHelper
3026
*/
31-
private $sendFriendFactory;
27+
private $sendFriendHelper;
3228

3329
/**
34-
* @var ProductRepositoryInterface
30+
* @var SendEmail
3531
*/
36-
private $productRepository;
32+
private $sendEmail;
3733

3834
/**
39-
* @var DataObjectFactory
40-
*/
41-
private $dataObjectFactory;
42-
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
35+
* @param SendEmail $sendEmail
36+
* @param SendFriendHelper $sendFriendHelper
5337
*/
5438
public function __construct(
55-
SendFriendFactory $sendFriendFactory,
56-
ProductRepositoryInterface $productRepository,
57-
DataObjectFactory $dataObjectFactory,
58-
ManagerInterface $eventManager
39+
SendEmail $sendEmail,
40+
SendFriendHelper $sendFriendHelper
5941
) {
60-
$this->sendFriendFactory = $sendFriendFactory;
61-
$this->productRepository = $productRepository;
62-
$this->dataObjectFactory = $dataObjectFactory;
63-
$this->eventManager = $eventManager;
42+
$this->sendEmail = $sendEmail;
43+
$this->sendFriendHelper = $sendFriendHelper;
6444
}
6545

6646
/**
6747
* @inheritdoc
6848
*/
6949
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
7050
{
71-
/** @var SendFriend $sendFriend */
72-
$sendFriend = $this->sendFriendFactory->create();
73-
74-
if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
75-
throw new GraphQlInputException(
76-
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
77-
);
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.'));
7856
}
7957

80-
$product = $this->getProduct($args['input']['product_id']);
81-
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
82-
$sendFriend->setProduct($product);
83-
8458
$senderData = $this->extractSenderData($args);
85-
$sendFriend->setSender($senderData);
86-
8759
$recipientsData = $this->extractRecipientsData($args);
88-
$sendFriend->setRecipients($recipientsData);
89-
90-
$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
91-
$sendFriend->send();
9260

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

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

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)