Skip to content

Commit bbe60dd

Browse files
committed
Fix static test issues
* Missing dep on magento/module-authorization * Separate email send logic from resolver
1 parent 63762d6 commit bbe60dd

File tree

3 files changed

+164
-109
lines changed

3 files changed

+164
-109
lines changed

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

Lines changed: 20 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,153 +8,65 @@
88
namespace Magento\SendFriendGraphQl\Model\Resolver;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11-
use Magento\Catalog\Api\Data\ProductInterface;
12-
use Magento\Catalog\Api\ProductRepositoryInterface;
13-
use Magento\Framework\App\ObjectManager;
14-
use Magento\Framework\DataObjectFactory;
15-
use Magento\Framework\Event\ManagerInterface;
16-
use Magento\Framework\Exception\NoSuchEntityException;
1711
use Magento\Framework\GraphQl\Config\Element\Field;
1812
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1913
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
20-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
2114
use Magento\Framework\GraphQl\Query\ResolverInterface;
2215
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
23-
use Magento\SendFriend\Model\SendFriend;
24-
use Magento\SendFriend\Model\SendFriendFactory;
2516
use Magento\SendFriend\Helper\Data as SendFriendHelper;
17+
use Magento\SendFriendGraphQl\Model\SendFriend\SendEmail;
2618

2719
/**
2820
* @inheritdoc
2921
*/
3022
class SendEmailToFriend implements ResolverInterface
3123
{
3224
/**
33-
* @var SendFriendFactory
34-
*/
35-
private $sendFriendFactory;
36-
37-
/**
38-
* @var ProductRepositoryInterface
39-
*/
40-
private $productRepository;
41-
42-
/**
43-
* @var DataObjectFactory
44-
*/
45-
private $dataObjectFactory;
46-
47-
/**
48-
* @var ManagerInterface
25+
* @var SendFriendHelper
4926
*/
50-
private $eventManager;
27+
private $sendFriendHelper;
5128

5229
/**
53-
* @var SendFriendHelper
30+
* @var SendEmail
5431
*/
55-
private $sendFriendHelper;
32+
private $sendEmail;
5633

5734
/**
58-
* @param SendFriendFactory $sendFriendFactory
59-
* @param ProductRepositoryInterface $productRepository
60-
* @param DataObjectFactory $dataObjectFactory
61-
* @param ManagerInterface $eventManager
62-
* @param SendFriendHelper|null $sendFriendHelper
35+
* @param SendEmail $sendEmail
36+
* @param SendFriendHelper $sendFriendHelper
6337
*/
6438
public function __construct(
65-
SendFriendFactory $sendFriendFactory,
66-
ProductRepositoryInterface $productRepository,
67-
DataObjectFactory $dataObjectFactory,
68-
ManagerInterface $eventManager,
69-
SendFriendHelper $sendFriendHelper = null
39+
SendEmail $sendEmail,
40+
SendFriendHelper $sendFriendHelper
7041
) {
71-
$this->sendFriendFactory = $sendFriendFactory;
72-
$this->productRepository = $productRepository;
73-
$this->dataObjectFactory = $dataObjectFactory;
74-
$this->eventManager = $eventManager;
75-
$this->sendFriendHelper = $sendFriendHelper ?? ObjectManager::getInstance()->get(SendFriendHelper::class);
42+
$this->sendEmail = $sendEmail;
43+
$this->sendFriendHelper = $sendFriendHelper;
7644
}
7745

7846
/**
7947
* @inheritdoc
8048
*/
8149
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
8250
{
83-
if (!$this->sendFriendHelper->isAllowForGuest() && $this->isUserGuest($context->getUserId(), $context->getUserType())) {
84-
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
85-
}
51+
$userId = $context->getUserId();
52+
$userType = $context->getUserType();
8653

87-
/** @var SendFriend $sendFriend */
88-
$sendFriend = $this->sendFriendFactory->create();
89-
90-
if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
91-
throw new GraphQlInputException(
92-
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
93-
);
54+
if (!$this->sendFriendHelper->isAllowForGuest() && $this->isUserGuest($userId, $userType)) {
55+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
9456
}
9557

96-
$product = $this->getProduct($args['input']['product_id']);
97-
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
98-
$sendFriend->setProduct($product);
99-
10058
$senderData = $this->extractSenderData($args);
101-
$sendFriend->setSender($senderData);
102-
10359
$recipientsData = $this->extractRecipientsData($args);
104-
$sendFriend->setRecipients($recipientsData);
10560

106-
$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
107-
$sendFriend->send();
61+
$this->sendEmail->execute(
62+
$args['input']['product_id'],
63+
$senderData,
64+
$recipientsData
65+
);
10866

10967
return array_merge($senderData, $recipientsData);
11068
}
11169

112-
/**
113-
* Validate send friend model
114-
*
115-
* @param SendFriend $sendFriend
116-
* @param array $senderData
117-
* @param array $recipientsData
118-
* @return void
119-
* @throws GraphQlInputException
120-
*/
121-
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
122-
{
123-
$sender = $this->dataObjectFactory->create()->setData($senderData['sender']);
124-
$sendFriend->setData('_sender', $sender);
125-
126-
$emails = array_column($recipientsData['recipients'], 'email');
127-
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
128-
$sendFriend->setData('_recipients', $recipients);
129-
130-
$validationResult = $sendFriend->validate();
131-
if ($validationResult !== true) {
132-
throw new GraphQlInputException(__(implode($validationResult)));
133-
}
134-
}
135-
136-
/**
137-
* Get product
138-
*
139-
* @param int $productId
140-
* @return ProductInterface
141-
* @throws GraphQlNoSuchEntityException
142-
*/
143-
private function getProduct(int $productId): ProductInterface
144-
{
145-
try {
146-
$product = $this->productRepository->getById($productId);
147-
if (!$product->isVisibleInCatalog()) {
148-
throw new GraphQlNoSuchEntityException(
149-
__("The product that was requested doesn't exist. Verify the product and try again.")
150-
);
151-
}
152-
} catch (NoSuchEntityException $e) {
153-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
154-
}
155-
return $product;
156-
}
157-
15870
/**
15971
* Extract recipients data
16072
*
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"php": "~7.1.3||~7.2.0",
77
"magento/framework": "*",
88
"magento/module-catalog": "*",
9-
"magento/module-send-friend": "*"
9+
"magento/module-send-friend": "*",
10+
"magento/module-authorization": "*"
1011
},
1112
"suggest": {
1213
"magento/module-graph-ql": "*"

0 commit comments

Comments
 (0)