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
rleshchenko committed Jun 22, 2019
1 parent 890bf89 commit a1d87ba
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);


namespace Magento\SendFriendGraphQl\Model\Resolver;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class ProductService
*/
class ProductService
{
/** @var ProductRepositoryInterface */
private $productRepository;

/** @var Visibility */
private $visibility;

/**
* @param ProductRepositoryInterface $productRepository
* @param Visibility $visibility
*/
public function __construct(
ProductRepositoryInterface $productRepository,
Visibility $visibility
) {
$this->productRepository = $productRepository;
$this->visibility = $visibility;
}

/**
* Get product
*
* @param int $productId
* @return ProductInterface
* @throws GraphQlNoSuchEntityException
*/
public function getProduct(int $productId): ProductInterface
{
try {
$product = $this->productRepository->getById($productId);

if (!in_array(
$product->getVisibility(),
$this->visibility->getVisibleInCatalogIds()
)) {
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 @@ -7,18 +7,15 @@

namespace Magento\SendFriendGraphQl\Model\Resolver;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\SendFriend\Model\SendFriend;
use Magento\SendFriend\Model\SendFriendFactory;
use Magento\SendFriendGraphQl\Model\Resolver\ProductService;

/**
* @inheritdoc
Expand All @@ -30,11 +27,6 @@ class SendEmailToFriend implements ResolverInterface
*/
private $sendFriendFactory;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var DataObjectFactory
*/
Expand All @@ -45,22 +37,27 @@ class SendEmailToFriend implements ResolverInterface
*/
private $eventManager;

/**
* @var ProductService
*/
private $productService;

/**
* @param SendFriendFactory $sendFriendFactory
* @param ProductRepositoryInterface $productRepository
* @param DataObjectFactory $dataObjectFactory
* @param ManagerInterface $eventManager
* @param ProductService $productService
*/
public function __construct(
SendFriendFactory $sendFriendFactory,
ProductRepositoryInterface $productRepository,
DataObjectFactory $dataObjectFactory,
ManagerInterface $eventManager
ManagerInterface $eventManager,
ProductService $productService
) {
$this->sendFriendFactory = $sendFriendFactory;
$this->productRepository = $productRepository;
$this->dataObjectFactory = $dataObjectFactory;
$this->eventManager = $eventManager;
$this->productService = $productService;
}

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

$product = $this->getProduct($args['input']['product_id']);
$product = $this->productService->getProduct($args['input']['product_id']);
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]);
$sendFriend->setProduct($product);

Expand Down Expand Up @@ -117,28 +114,6 @@ private function validateSendFriendModel(SendFriend $sendFriend, array $senderDa
}
}

/**
* 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;
}

/**
* Extract recipients data
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\SendFriend\Model\SendFriend;
use Magento\SendFriend\Model\SendFriendFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
Expand Down Expand Up @@ -219,15 +220,8 @@ public function testSendProductWithoutVisibility()
email:"[email protected]"
}';
$query = $this->getQuery($productId, $recipients);

$response = $this->graphQlMutation($query);
self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']);
self::assertEquals('[email protected]', $response['sendEmailToFriend']['sender']['email']);
self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']);
self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']);
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][0]['email']);
self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']);
self::assertEquals('[email protected]', $response['sendEmailToFriend']['recipients'][1]['email']);
$this->expectException(ResponseContainsErrorsException::class);
$this->graphQlMutation($query);
}

/**
Expand Down

0 comments on commit a1d87ba

Please sign in to comment.