forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue/732' of github.com:pmclain/graphql-ce into 2.3-de…
…velop-678 # Conflicts: # app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php # dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php
- Loading branch information
Showing
7 changed files
with
314 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
app/code/Magento/SendFriendGraphQl/Model/SendFriend/SendEmail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.