Skip to content

Commit

Permalink
Fix static test issues
Browse files Browse the repository at this point in the history
* Missing dep on magento/module-authorization
* Separate email send logic from resolver
  • Loading branch information
pmclain committed Jun 8, 2019
1 parent 63762d6 commit bbe60dd
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 109 deletions.
128 changes: 20 additions & 108 deletions app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,153 +8,65 @@
namespace Magento\SendFriendGraphQl\Model\Resolver;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\ObjectManager;
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\SendFriend\Helper\Data as SendFriendHelper;
use Magento\SendFriendGraphQl\Model\SendFriend\SendEmail;

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

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

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

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

/**
* @var SendFriendHelper
* @var SendEmail
*/
private $sendFriendHelper;
private $sendEmail;

/**
* @param SendFriendFactory $sendFriendFactory
* @param ProductRepositoryInterface $productRepository
* @param DataObjectFactory $dataObjectFactory
* @param ManagerInterface $eventManager
* @param SendFriendHelper|null $sendFriendHelper
* @param SendEmail $sendEmail
* @param SendFriendHelper $sendFriendHelper
*/
public function __construct(
SendFriendFactory $sendFriendFactory,
ProductRepositoryInterface $productRepository,
DataObjectFactory $dataObjectFactory,
ManagerInterface $eventManager,
SendFriendHelper $sendFriendHelper = null
SendEmail $sendEmail,
SendFriendHelper $sendFriendHelper
) {
$this->sendFriendFactory = $sendFriendFactory;
$this->productRepository = $productRepository;
$this->dataObjectFactory = $dataObjectFactory;
$this->eventManager = $eventManager;
$this->sendFriendHelper = $sendFriendHelper ?? ObjectManager::getInstance()->get(SendFriendHelper::class);
$this->sendEmail = $sendEmail;
$this->sendFriendHelper = $sendFriendHelper;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!$this->sendFriendHelper->isAllowForGuest() && $this->isUserGuest($context->getUserId(), $context->getUserType())) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}
$userId = $context->getUserId();
$userType = $context->getUserType();

/** @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())
);
if (!$this->sendFriendHelper->isAllowForGuest() && $this->isUserGuest($userId, $userType)) {
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
142 changes: 142 additions & 0 deletions app/code/Magento/SendFriendGraphQl/Model/SendFriend/SendEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?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;

/**
* Send Product Email to Friend(s)
*/
class SendEmail
{
/**
* @var DataObjectFactory
*/
private $dataObjectFactory;

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

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

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

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

/**
* 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->getProduct($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)));
}
}

/**
* 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;
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/SendFriendGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-catalog": "*",
"magento/module-send-friend": "*"
"magento/module-send-friend": "*",
"magento/module-authorization": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
Expand Down

0 comments on commit bbe60dd

Please sign in to comment.