Skip to content

Commit

Permalink
magento/graphql-ce:magento#678 Send email to friend
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaorobei committed Jun 24, 2019
1 parent ee8ad58 commit 193d4d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
declare(strict_types=1);


namespace Magento\SendFriendGraphQl\Model\Provider;

use Magento\Catalog\Api\ProductRepositoryInterface;
Expand All @@ -15,7 +14,7 @@
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class GetProduct
* Returns product if it is visible in catalog.
*/
class GetVisibleProduct
{
Expand Down
36 changes: 12 additions & 24 deletions app/code/Magento/SendFriendGraphQl/Model/SendFriend/SendEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\SendFriend\Model\SendFriend;
use Magento\SendFriend\Model\SendFriendFactory;
use Magento\SendFriendGraphQl\Model\Provider\GetVisibleProduct;

/**
* Send Product Email to Friend(s)
Expand Down Expand Up @@ -43,21 +44,30 @@ class SendEmail
private $eventManager;

/**
* @var GetVisibleProduct
*/
private $visibleProductProvider;

/**
* SendEmail constructor.
* @param DataObjectFactory $dataObjectFactory
* @param ProductRepositoryInterface $productRepository
* @param SendFriendFactory $sendFriendFactory
* @param ManagerInterface $eventManager
* @param GetVisibleProduct $visibleProductProvider
*/
public function __construct(
DataObjectFactory $dataObjectFactory,
ProductRepositoryInterface $productRepository,
SendFriendFactory $sendFriendFactory,
ManagerInterface $eventManager
ManagerInterface $eventManager,
GetVisibleProduct $visibleProductProvider
) {
$this->dataObjectFactory = $dataObjectFactory;
$this->productRepository = $productRepository;
$this->sendFriendFactory = $sendFriendFactory;
$this->eventManager = $eventManager;
$this->visibleProductProvider = $visibleProductProvider;
}

/**
Expand All @@ -81,7 +91,7 @@ public function execute(int $productId, array $senderData, array $recipientsData
);
}

$product = $this->getProduct($productId);
$product = $this->visibleProductProvider->execute($productId);

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

Expand Down Expand Up @@ -117,26 +127,4 @@ private function validateSendFriendModel(SendFriend $sendFriend, array $senderDa
throw new GraphQlInputException(__(implode($validationResult)));
}
}

/**
* Get product
*
* @param int $productId
* @return ProductInterface
* @throws GraphQlNoSuchEntityException
*/
private function getProduct(int $productId): ProductInterface
{
try {
$product = $this->productRepository->getById($productId);
if (!$product->isVisibleInCatalog()) {
throw new GraphQlNoSuchEntityException(
__("The product that was requested doesn't exist. Verify the product and try again.")
);
}
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
return $product;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public function testSendWithoutExistProduct()
}';
$query = $this->getQuery($productId, $recipients);

$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'The product that was requested doesn\'t exist. Verify the product and try again.'
);
Expand Down Expand Up @@ -276,9 +275,10 @@ public function testSendProductWithoutVisibility()
email:"[email protected]"
}';
$query = $this->getQuery($productId, $recipients);

$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
$this->assertResponse($response);
$this->expectExceptionMessage(
'The product that was requested doesn\'t exist. Verify the product and try again.'
);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
Expand Down

0 comments on commit 193d4d1

Please sign in to comment.