|
7 | 7 |
|
8 | 8 | namespace Magento\SendFriendGraphQl\Model\Resolver; |
9 | 9 |
|
10 | | -use Magento\Catalog\Api\Data\ProductInterface; |
11 | | -use Magento\Catalog\Api\ProductRepositoryInterface; |
12 | | -use Magento\Framework\DataObjectFactory; |
13 | | -use Magento\Framework\Event\ManagerInterface; |
14 | | -use Magento\Framework\Exception\NoSuchEntityException; |
15 | 10 | use Magento\Framework\GraphQl\Config\Element\Field; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
16 | 12 | use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
17 | | -use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
18 | 13 | use Magento\Framework\GraphQl\Query\ResolverInterface; |
19 | 14 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
20 | | -use Magento\SendFriend\Model\SendFriend; |
21 | | -use Magento\SendFriend\Model\SendFriendFactory; |
| 15 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 16 | +use Magento\SendFriend\Helper\Data as SendFriendHelper; |
| 17 | +use Magento\SendFriendGraphQl\Model\SendFriend\SendEmail; |
22 | 18 |
|
23 | 19 | /** |
24 | 20 | * @inheritdoc |
25 | 21 | */ |
26 | 22 | class SendEmailToFriend implements ResolverInterface |
27 | 23 | { |
28 | 24 | /** |
29 | | - * @var SendFriendFactory |
| 25 | + * @var SendFriendHelper |
30 | 26 | */ |
31 | | - private $sendFriendFactory; |
| 27 | + private $sendFriendHelper; |
32 | 28 |
|
33 | 29 | /** |
34 | | - * @var ProductRepositoryInterface |
| 30 | + * @var SendEmail |
35 | 31 | */ |
36 | | - private $productRepository; |
| 32 | + private $sendEmail; |
37 | 33 |
|
38 | 34 | /** |
39 | | - * @var DataObjectFactory |
40 | | - */ |
41 | | - private $dataObjectFactory; |
42 | | - |
43 | | - /** |
44 | | - * @var ManagerInterface |
45 | | - */ |
46 | | - private $eventManager; |
47 | | - |
48 | | - /** |
49 | | - * @param SendFriendFactory $sendFriendFactory |
50 | | - * @param ProductRepositoryInterface $productRepository |
51 | | - * @param DataObjectFactory $dataObjectFactory |
52 | | - * @param ManagerInterface $eventManager |
| 35 | + * @param SendEmail $sendEmail |
| 36 | + * @param SendFriendHelper $sendFriendHelper |
53 | 37 | */ |
54 | 38 | public function __construct( |
55 | | - SendFriendFactory $sendFriendFactory, |
56 | | - ProductRepositoryInterface $productRepository, |
57 | | - DataObjectFactory $dataObjectFactory, |
58 | | - ManagerInterface $eventManager |
| 39 | + SendEmail $sendEmail, |
| 40 | + SendFriendHelper $sendFriendHelper |
59 | 41 | ) { |
60 | | - $this->sendFriendFactory = $sendFriendFactory; |
61 | | - $this->productRepository = $productRepository; |
62 | | - $this->dataObjectFactory = $dataObjectFactory; |
63 | | - $this->eventManager = $eventManager; |
| 42 | + $this->sendEmail = $sendEmail; |
| 43 | + $this->sendFriendHelper = $sendFriendHelper; |
64 | 44 | } |
65 | 45 |
|
66 | 46 | /** |
67 | 47 | * @inheritdoc |
68 | 48 | */ |
69 | 49 | public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
70 | 50 | { |
71 | | - /** @var SendFriend $sendFriend */ |
72 | | - $sendFriend = $this->sendFriendFactory->create(); |
73 | | - |
74 | | - if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) { |
75 | | - throw new GraphQlInputException( |
76 | | - __('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend()) |
77 | | - ); |
| 51 | + /** @var ContextInterface $context */ |
| 52 | + if (!$this->sendFriendHelper->isAllowForGuest() |
| 53 | + && false === $context->getExtensionAttributes()->getIsCustomer() |
| 54 | + ) { |
| 55 | + throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); |
78 | 56 | } |
79 | 57 |
|
80 | | - $product = $this->getProduct($args['input']['product_id']); |
81 | | - $this->eventManager->dispatch('sendfriend_product', ['product' => $product]); |
82 | | - $sendFriend->setProduct($product); |
83 | | - |
84 | 58 | $senderData = $this->extractSenderData($args); |
85 | | - $sendFriend->setSender($senderData); |
86 | | - |
87 | 59 | $recipientsData = $this->extractRecipientsData($args); |
88 | | - $sendFriend->setRecipients($recipientsData); |
89 | | - |
90 | | - $this->validateSendFriendModel($sendFriend, $senderData, $recipientsData); |
91 | | - $sendFriend->send(); |
92 | 60 |
|
| 61 | + $this->sendEmail->execute( |
| 62 | + $args['input']['product_id'], |
| 63 | + $senderData, |
| 64 | + $recipientsData |
| 65 | + ); |
93 | 66 | return array_merge($senderData, $recipientsData); |
94 | 67 | } |
95 | 68 |
|
96 | | - /** |
97 | | - * Validate send friend model |
98 | | - * |
99 | | - * @param SendFriend $sendFriend |
100 | | - * @param array $senderData |
101 | | - * @param array $recipientsData |
102 | | - * @return void |
103 | | - * @throws GraphQlInputException |
104 | | - */ |
105 | | - private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void |
106 | | - { |
107 | | - $sender = $this->dataObjectFactory->create()->setData($senderData['sender']); |
108 | | - $sendFriend->setData('_sender', $sender); |
109 | | - |
110 | | - $emails = array_column($recipientsData['recipients'], 'email'); |
111 | | - $recipients = $this->dataObjectFactory->create()->setData('emails', $emails); |
112 | | - $sendFriend->setData('_recipients', $recipients); |
113 | | - |
114 | | - $validationResult = $sendFriend->validate(); |
115 | | - if ($validationResult !== true) { |
116 | | - throw new GraphQlInputException(__(implode($validationResult))); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - /** |
121 | | - * Get product |
122 | | - * |
123 | | - * @param int $productId |
124 | | - * @return ProductInterface |
125 | | - * @throws GraphQlNoSuchEntityException |
126 | | - */ |
127 | | - private function getProduct(int $productId): ProductInterface |
128 | | - { |
129 | | - try { |
130 | | - $product = $this->productRepository->getById($productId); |
131 | | - if (!$product->isVisibleInCatalog()) { |
132 | | - throw new GraphQlNoSuchEntityException( |
133 | | - __("The product that was requested doesn't exist. Verify the product and try again.") |
134 | | - ); |
135 | | - } |
136 | | - } catch (NoSuchEntityException $e) { |
137 | | - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); |
138 | | - } |
139 | | - return $product; |
140 | | - } |
141 | | - |
142 | 69 | /** |
143 | 70 | * Extract recipients data |
144 | 71 | * |
|
0 commit comments