Skip to content

Commit 193d4d1

Browse files
committed
magento/graphql-ce:#678 Send email to friend
1 parent ee8ad58 commit 193d4d1

File tree

3 files changed

+17
-30
lines changed

3 files changed

+17
-30
lines changed

app/code/Magento/SendFriendGraphQl/Model/Provider/GetVisibleProduct.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
declare(strict_types=1);
77

8-
98
namespace Magento\SendFriendGraphQl\Model\Provider;
109

1110
use Magento\Catalog\Api\ProductRepositoryInterface;
@@ -15,7 +14,7 @@
1514
use Magento\Framework\Exception\NoSuchEntityException;
1615

1716
/**
18-
* Class GetProduct
17+
* Returns product if it is visible in catalog.
1918
*/
2019
class GetVisibleProduct
2120
{

app/code/Magento/SendFriendGraphQl/Model/SendFriend/SendEmail.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1717
use Magento\SendFriend\Model\SendFriend;
1818
use Magento\SendFriend\Model\SendFriendFactory;
19+
use Magento\SendFriendGraphQl\Model\Provider\GetVisibleProduct;
1920

2021
/**
2122
* Send Product Email to Friend(s)
@@ -43,21 +44,30 @@ class SendEmail
4344
private $eventManager;
4445

4546
/**
47+
* @var GetVisibleProduct
48+
*/
49+
private $visibleProductProvider;
50+
51+
/**
52+
* SendEmail constructor.
4653
* @param DataObjectFactory $dataObjectFactory
4754
* @param ProductRepositoryInterface $productRepository
4855
* @param SendFriendFactory $sendFriendFactory
4956
* @param ManagerInterface $eventManager
57+
* @param GetVisibleProduct $visibleProductProvider
5058
*/
5159
public function __construct(
5260
DataObjectFactory $dataObjectFactory,
5361
ProductRepositoryInterface $productRepository,
5462
SendFriendFactory $sendFriendFactory,
55-
ManagerInterface $eventManager
63+
ManagerInterface $eventManager,
64+
GetVisibleProduct $visibleProductProvider
5665
) {
5766
$this->dataObjectFactory = $dataObjectFactory;
5867
$this->productRepository = $productRepository;
5968
$this->sendFriendFactory = $sendFriendFactory;
6069
$this->eventManager = $eventManager;
70+
$this->visibleProductProvider = $visibleProductProvider;
6171
}
6272

6373
/**
@@ -81,7 +91,7 @@ public function execute(int $productId, array $senderData, array $recipientsData
8191
);
8292
}
8393

84-
$product = $this->getProduct($productId);
94+
$product = $this->visibleProductProvider->execute($productId);
8595

8696
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
8797

@@ -117,26 +127,4 @@ private function validateSendFriendModel(SendFriend $sendFriend, array $senderDa
117127
throw new GraphQlInputException(__(implode($validationResult)));
118128
}
119129
}
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-
}
142130
}

dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public function testSendWithoutExistProduct()
125125
}';
126126
$query = $this->getQuery($productId, $recipients);
127127

128-
$this->expectException(\Exception::class);
129128
$this->expectExceptionMessage(
130129
'The product that was requested doesn\'t exist. Verify the product and try again.'
131130
);
@@ -276,9 +275,10 @@ public function testSendProductWithoutVisibility()
276275
277276
}';
278277
$query = $this->getQuery($productId, $recipients);
279-
280-
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
281-
$this->assertResponse($response);
278+
$this->expectExceptionMessage(
279+
'The product that was requested doesn\'t exist. Verify the product and try again.'
280+
);
281+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
282282
}
283283

284284
/**

0 commit comments

Comments
 (0)