Skip to content

Commit

Permalink
ENGCOM-5347: magento/graphql-ce:magento#678 Send email to friend mage…
Browse files Browse the repository at this point in the history
…nto#752

 - Merge Pull Request magento/graphql-ce#752 from rleshchenko/graphql-ce:2.3-develop-678
 - Merged commits:
   1. 63762d6
   2. bbe60dd
   3. 0e59bcf
   4. 2dded7c
   5. a1d87ba
   6. e8c102a
   7. 07e081f
   8. f69dee0
   9. ee8ad58
   10. 193d4d1
  • Loading branch information
magento-engcom-team committed Jun 24, 2019
2 parents 1e58f70 + 193d4d1 commit 5379d12
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SendFriendGraphQl\Model\Provider;

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;

/**
* Returns product if it is visible in catalog.
*/
class GetVisibleProduct
{
/** @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 execute(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,138 +7,65 @@

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\GraphQlAuthorizationException;
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\GraphQl\Model\Query\ContextInterface;
use Magento\SendFriend\Helper\Data as SendFriendHelper;
use Magento\SendFriendGraphQl\Model\SendFriend\SendEmail;

/**
* @inheritdoc
*/
class SendEmailToFriend implements ResolverInterface
{
/**
* @var SendFriendFactory
* @var SendFriendHelper
*/
private $sendFriendFactory;
private $sendFriendHelper;

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

/**
* @var DataObjectFactory
*/
private $dataObjectFactory;

/**
* @var ManagerInterface
*/
private $eventManager;

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

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
/** @var SendFriend $sendFriend */
$sendFriend = $this->sendFriendFactory->create();

if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
throw new GraphQlInputException(
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
);
/** @var ContextInterface $context */
if (!$this->sendFriendHelper->isAllowForGuest()
&& false === $context->getExtensionAttributes()->getIsCustomer()
) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

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

$senderData = $this->extractSenderData($args);
$sendFriend->setSender($senderData);

$recipientsData = $this->extractRecipientsData($args);
$sendFriend->setRecipients($recipientsData);

$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);
$sendFriend->send();

$this->sendEmail->execute(
$args['input']['product_id'],
$senderData,
$recipientsData
);
return array_merge($senderData, $recipientsData);
}

/**
* Validate send friend model
*
* @param SendFriend $sendFriend
* @param array $senderData
* @param array $recipientsData
* @return void
* @throws GraphQlInputException
*/
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
{
$sender = $this->dataObjectFactory->create()->setData($senderData['sender']);
$sendFriend->setData('_sender', $sender);

$emails = array_column($recipientsData['recipients'], 'email');
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
$sendFriend->setData('_recipients', $recipients);

$validationResult = $sendFriend->validate();
if ($validationResult !== true) {
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;
}

/**
* Extract recipients data
*
Expand Down
130 changes: 130 additions & 0 deletions app/code/Magento/SendFriendGraphQl/Model/SendFriend/SendEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\SendFriendGraphQl\Model\SendFriend;

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\Exception\GraphQlInputException;
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)
*/
class SendEmail
{
/**
* @var DataObjectFactory
*/
private $dataObjectFactory;

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

/**
* @var SendFriendFactory
*/
private $sendFriendFactory;

/**
* @var ManagerInterface
*/
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,
GetVisibleProduct $visibleProductProvider
) {
$this->dataObjectFactory = $dataObjectFactory;
$this->productRepository = $productRepository;
$this->sendFriendFactory = $sendFriendFactory;
$this->eventManager = $eventManager;
$this->visibleProductProvider = $visibleProductProvider;
}

/**
* Send product email to friend(s)
*
* @param int $productId
* @param array $senderData
* @param array $recipientsData
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(int $productId, array $senderData, array $recipientsData): void
{
/** @var SendFriend $sendFriend */
$sendFriend = $this->sendFriendFactory->create();

if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) {
throw new GraphQlInputException(
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend())
);
}

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

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

$sendFriend->setProduct($product);
$sendFriend->setSender($senderData);
$sendFriend->setRecipients($recipientsData);

$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData);

$sendFriend->send();
}

/**
* Validate send friend model
*
* @param SendFriend $sendFriend
* @param array $senderData
* @param array $recipientsData
* @return void
* @throws GraphQlInputException
*/
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void
{
$sender = $this->dataObjectFactory->create()->setData($senderData['sender']);
$sendFriend->setData('_sender', $sender);

$emails = array_column($recipientsData['recipients'], 'email');
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
$sendFriend->setData('_recipients', $recipients);

$validationResult = $sendFriend->validate();
if ($validationResult !== true) {
throw new GraphQlInputException(__(implode($validationResult)));
}
}
}
4 changes: 1 addition & 3 deletions app/code/Magento/SendFriendGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-catalog": "*",
"magento/module-send-friend": "*"
},
"suggest": {
"magento/module-send-friend": "*",
"magento/module-graph-ql": "*"
},
"license": [
Expand Down
Loading

0 comments on commit 5379d12

Please sign in to comment.