Skip to content

Commit f69dee0

Browse files
committed
Merge branch '2.3-develop-678' of github.com:rleshchenko/graphql-ce into 2.3-develop-678
2 parents 1e58f70 + 07e081f commit f69dee0

File tree

3 files changed

+79
-45
lines changed

3 files changed

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

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

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77

88
namespace Magento\SendFriendGraphQl\Model\Resolver;
99

10-
use Magento\Catalog\Api\Data\ProductInterface;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
1210
use Magento\Framework\DataObjectFactory;
1311
use Magento\Framework\Event\ManagerInterface;
14-
use Magento\Framework\Exception\NoSuchEntityException;
1512
use Magento\Framework\GraphQl\Config\Element\Field;
1613
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1814
use Magento\Framework\GraphQl\Query\ResolverInterface;
1915
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2016
use Magento\SendFriend\Model\SendFriend;
2117
use Magento\SendFriend\Model\SendFriendFactory;
18+
use Magento\SendFriendGraphQl\Model\Provider\GetProduct;
2219

2320
/**
2421
* @inheritdoc
@@ -30,11 +27,6 @@ class SendEmailToFriend implements ResolverInterface
3027
*/
3128
private $sendFriendFactory;
3229

33-
/**
34-
* @var ProductRepositoryInterface
35-
*/
36-
private $productRepository;
37-
3830
/**
3931
* @var DataObjectFactory
4032
*/
@@ -45,22 +37,27 @@ class SendEmailToFriend implements ResolverInterface
4537
*/
4638
private $eventManager;
4739

40+
/**
41+
* @var GetProduct
42+
*/
43+
private $getProductProvider;
44+
4845
/**
4946
* @param SendFriendFactory $sendFriendFactory
50-
* @param ProductRepositoryInterface $productRepository
5147
* @param DataObjectFactory $dataObjectFactory
5248
* @param ManagerInterface $eventManager
49+
* @param GetProduct $getProductProvider
5350
*/
5451
public function __construct(
5552
SendFriendFactory $sendFriendFactory,
56-
ProductRepositoryInterface $productRepository,
5753
DataObjectFactory $dataObjectFactory,
58-
ManagerInterface $eventManager
54+
ManagerInterface $eventManager,
55+
GetProduct $getProductProvider
5956
) {
6057
$this->sendFriendFactory = $sendFriendFactory;
61-
$this->productRepository = $productRepository;
6258
$this->dataObjectFactory = $dataObjectFactory;
6359
$this->eventManager = $eventManager;
60+
$this->getProductProvider = $getProductProvider;
6461
}
6562

6663
/**
@@ -77,7 +74,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7774
);
7875
}
7976

80-
$product = $this->getProduct($args['input']['product_id']);
77+
$product = $this->getProductProvider->execute($args['input']['product_id']);
8178
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
8279
$sendFriend->setProduct($product);
8380

@@ -117,28 +114,6 @@ private function validateSendFriendModel(SendFriend $sendFriend, array $senderDa
117114
}
118115
}
119116

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-
142117
/**
143118
* Extract recipients data
144119
*

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\SendFriend\Model\SendFriend;
1212
use Magento\SendFriend\Model\SendFriendFactory;
1313
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
1415
use Magento\TestFramework\TestCase\GraphQlAbstract;
1516

1617
/**
@@ -219,15 +220,8 @@ public function testSendProductWithoutVisibility()
219220
220221
}';
221222
$query = $this->getQuery($productId, $recipients);
222-
223-
$response = $this->graphQlMutation($query);
224-
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
225-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']);
226-
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
227-
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
228-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']);
229-
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
230-
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']);
223+
$this->expectException(ResponseContainsErrorsException::class);
224+
$this->graphQlMutation($query);
231225
}
232226

233227
/**

0 commit comments

Comments
 (0)