|
7 | 7 |
|
8 | 8 | namespace Magento\SendFriendGraphQl\Model\Resolver; |
9 | 9 |
|
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
10 | 11 | use Magento\Catalog\Api\ProductRepositoryInterface; |
11 | 12 | use Magento\Framework\DataObjectFactory; |
| 13 | +use Magento\Framework\Event\ManagerInterface; |
12 | 14 | use Magento\Framework\Exception\NoSuchEntityException; |
13 | 15 | use Magento\Framework\GraphQl\Config\Element\Field; |
14 | 16 | use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 17 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
15 | 18 | use Magento\Framework\GraphQl\Query\ResolverInterface; |
16 | 19 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
17 | 20 | use Magento\SendFriend\Model\SendFriend; |
| 21 | +use Magento\SendFriend\Model\SendFriendFactory; |
18 | 22 |
|
| 23 | +/** |
| 24 | + * @inheritdoc |
| 25 | + */ |
19 | 26 | class SendEmailToFriend implements ResolverInterface |
20 | 27 | { |
21 | 28 | /** |
22 | | - * @var SendFriend |
| 29 | + * @var SendFriendFactory |
23 | 30 | */ |
24 | | - private $sendFriend; |
| 31 | + private $sendFriendFactory; |
| 32 | + |
25 | 33 | /** |
26 | 34 | * @var ProductRepositoryInterface |
27 | 35 | */ |
28 | 36 | private $productRepository; |
| 37 | + |
29 | 38 | /** |
30 | 39 | * @var DataObjectFactory |
31 | 40 | */ |
32 | 41 | private $dataObjectFactory; |
33 | 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 |
| 53 | + */ |
34 | 54 | public function __construct( |
35 | | - SendFriend $sendFriend, |
| 55 | + SendFriendFactory $sendFriendFactory, |
36 | 56 | ProductRepositoryInterface $productRepository, |
37 | | - DataObjectFactory $dataObjectFactory |
| 57 | + DataObjectFactory $dataObjectFactory, |
| 58 | + ManagerInterface $eventManager |
38 | 59 | ) { |
39 | | - $this->sendFriend = $sendFriend; |
| 60 | + $this->sendFriendFactory = $sendFriendFactory; |
40 | 61 | $this->productRepository = $productRepository; |
41 | 62 | $this->dataObjectFactory = $dataObjectFactory; |
| 63 | + $this->eventManager = $eventManager; |
42 | 64 | } |
43 | 65 |
|
44 | 66 | /** |
45 | 67 | * @inheritdoc |
46 | 68 | */ |
47 | 69 | public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
48 | 70 | { |
49 | | - if ($this->sendFriend->getMaxSendsToFriend() && $this->sendFriend->isExceedLimit()) { |
| 71 | + /** @var SendFriend $sendFriend */ |
| 72 | + $sendFriend = $this->sendFriendFactory->create(); |
| 73 | + |
| 74 | + if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) { |
50 | 75 | throw new GraphQlInputException(__('You can\'t send messages more than %1 times an hour.', |
51 | | - $this->sendFriend->getMaxSendsToFriend() |
| 76 | + $sendFriend->getMaxSendsToFriend() |
52 | 77 | )); |
53 | 78 | } |
54 | 79 |
|
55 | | - $product = $this->getProductFromRepository($args['input']['params']['product_id']); |
56 | | - $senderArray = $this->getSenderArrayFromArgs($args); |
57 | | - $recipientsArray = $this->getRecipientsArray($args); |
58 | | - //@todo clarify if event should be dispatched |
59 | | - //$this->_eventManager->dispatch('sendfriend_product', ['product' => $product]); |
60 | | - $this->sendFriend->setSender($senderArray); |
61 | | - $this->sendFriend->setRecipients($recipientsArray); |
62 | | - $this->sendFriend->setProduct($product); |
63 | | - |
64 | | - $this->prepareDataForValidation($args, $recipientsArray); |
65 | | - $validationResult = $this->sendFriend->validate(); |
66 | | - $this->addRecipientNameValidation($args); |
67 | | - if ($validationResult !== true) { |
68 | | - throw new GraphQlInputException(__(implode($validationResult))); |
69 | | - } |
| 80 | + $product = $this->getProduct($args['input']['product_id']); |
| 81 | + $this->eventManager->dispatch('sendfriend_product', ['product' => $product]); |
| 82 | + $sendFriend->setProduct($product); |
70 | 83 |
|
71 | | - $this->sendFriend->send(); |
| 84 | + $senderData = $this->extractSenderData($args); |
| 85 | + $sendFriend->setSender($senderData); |
72 | 86 |
|
73 | | - return array_merge($senderArray, $recipientsArray); |
74 | | - } |
| 87 | + $recipientsData = $this->extractRecipientsData($args); |
| 88 | + $sendFriend->setRecipients($recipientsData); |
75 | 89 |
|
76 | | - private function prepareDataForValidation(array $args, array $recipientsArray): void |
77 | | - { |
78 | | - $sender = $this->dataObjectFactory->create()->setData([ |
79 | | - 'name' => $args['input']['sender']['name'], |
80 | | - 'email'=> $args['input']['sender']['email'], |
81 | | - 'message' => $args['input']['sender']['message'], |
82 | | - ]); |
83 | | - $emails = []; |
84 | | - foreach ($recipientsArray['recipients'] as $recipient) { |
85 | | - $emails[] = $recipient['email']; |
86 | | - } |
87 | | - $recipients = $this->dataObjectFactory->create()->setData('emails', $emails); |
| 90 | + $this->validateSendFriendModel($sendFriend, $senderData, $recipientsData); |
| 91 | + $sendFriend->send(); |
88 | 92 |
|
89 | | - $this->sendFriend->setData('_sender', $sender); |
90 | | - $this->sendFriend->setData('_recipients', $recipients); |
| 93 | + return array_merge($senderData, $recipientsData); |
91 | 94 | } |
92 | 95 |
|
93 | 96 | /** |
94 | | - * @param array $args |
| 97 | + * Validate send friend model |
| 98 | + * |
| 99 | + * @param SendFriend $sendFriend |
| 100 | + * @param array $senderData |
| 101 | + * @param array $recipientsData |
| 102 | + * @return void |
95 | 103 | * @throws GraphQlInputException |
96 | 104 | */ |
97 | | - private function addRecipientNameValidation(array $args): void |
| 105 | + private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void |
98 | 106 | { |
99 | | - foreach ($args['input']['recipients'] as $recipient) { |
100 | | - if (empty($recipient['name'])) { |
101 | | - throw new GraphQlInputException( |
102 | | - __('Please Provide Name for Recipient with this Email Address: ' . $recipient['email'] |
103 | | - )); |
104 | | - } |
| 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))); |
105 | 117 | } |
106 | 118 | } |
107 | 119 |
|
108 | 120 | /** |
| 121 | + * Get product |
| 122 | + * |
109 | 123 | * @param int $productId |
110 | | - * @return bool|\Magento\Catalog\Api\Data\ProductInterface |
| 124 | + * @return ProductInterface |
| 125 | + * @throws GraphQlNoSuchEntityException |
111 | 126 | */ |
112 | | - private function getProductFromRepository(int $productId) |
| 127 | + private function getProduct(int $productId): ProductInterface |
113 | 128 | { |
114 | 129 | try { |
115 | 130 | $product = $this->productRepository->getById($productId); |
116 | 131 | if (!$product->isVisibleInCatalog()) { |
117 | | - return false; |
| 132 | + throw new GraphQlNoSuchEntityException( |
| 133 | + __("The product that was requested doesn't exist. Verify the product and try again.") |
| 134 | + ); |
118 | 135 | } |
119 | | - } catch (NoSuchEntityException $noEntityException) { |
120 | | - return false; |
| 136 | + } catch (NoSuchEntityException $e) { |
| 137 | + throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); |
121 | 138 | } |
122 | | - |
123 | 139 | return $product; |
124 | 140 | } |
125 | 141 |
|
126 | | - private function getRecipientsArray(array $args): array |
| 142 | + /** |
| 143 | + * Extract recipients data |
| 144 | + * |
| 145 | + * @param array $args |
| 146 | + * @return array |
| 147 | + * @throws GraphQlInputException |
| 148 | + */ |
| 149 | + private function extractRecipientsData(array $args): array |
127 | 150 | { |
128 | | - $recipientsArray = []; |
| 151 | + $recipients = []; |
129 | 152 | foreach ($args['input']['recipients'] as $recipient) { |
130 | | - $recipientsArray[] = [ |
| 153 | + if (empty($recipient['name'])) { |
| 154 | + throw new GraphQlInputException(__('Please provide Name for all of recipients.')); |
| 155 | + } |
| 156 | + |
| 157 | + if (empty($recipient['email'])) { |
| 158 | + throw new GraphQlInputException(__('Please provide Email for all of recipients.')); |
| 159 | + } |
| 160 | + |
| 161 | + $recipients[] = [ |
131 | 162 | 'name' => $recipient['name'], |
132 | 163 | 'email' => $recipient['email'], |
133 | 164 | ]; |
134 | 165 | } |
135 | | - return ['recipients' => $recipientsArray]; |
| 166 | + return ['recipients' => $recipients]; |
136 | 167 | } |
137 | 168 |
|
138 | | - private function getSenderArrayFromArgs(array $args): array |
| 169 | + /** |
| 170 | + * Extract sender data |
| 171 | + * |
| 172 | + * @param array $args |
| 173 | + * @return array |
| 174 | + * @throws GraphQlInputException |
| 175 | + */ |
| 176 | + private function extractSenderData(array $args): array |
139 | 177 | { |
140 | | - return ['sender' => [ |
141 | | - 'name' => $args['input']['sender']['name'], |
142 | | - 'email' => $args['input']['sender']['email'], |
143 | | - 'message' => $args['input']['sender']['message'], |
144 | | - ] |
145 | | - ]; |
| 178 | + if (empty($args['input']['sender']['name'])) { |
| 179 | + throw new GraphQlInputException(__('Please provide Name of sender.')); |
| 180 | + } |
| 181 | + |
| 182 | + if (empty($args['input']['sender']['email'])) { |
| 183 | + throw new GraphQlInputException(__('Please provide Email of sender.')); |
| 184 | + } |
| 185 | + |
| 186 | + if (empty($args['input']['sender']['message'])) { |
| 187 | + throw new GraphQlInputException(__('Please provide Message.')); |
| 188 | + } |
| 189 | + |
| 190 | + return [ |
| 191 | + 'sender' => [ |
| 192 | + 'name' => $args['input']['sender']['name'], |
| 193 | + 'email' => $args['input']['sender']['email'], |
| 194 | + 'message' => $args['input']['sender']['message'], |
| 195 | + ], |
| 196 | + ]; |
146 | 197 | } |
147 | 198 | } |
0 commit comments