Skip to content

Commit 1fb624d

Browse files
author
Tobias Maile
committed
GraphQL-202: [Mutations] moves validation logic to separate module
1 parent 2f995d1 commit 1fb624d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SendFriendGraphQl\Model\Validation;
9+
10+
use Magento\Framework\DataObjectFactory;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\SendFriend\Model\SendFriend;
13+
14+
class Validation
15+
{
16+
/**
17+
* @var SendFriend
18+
*/
19+
private $sendFriend;
20+
/**
21+
* @var DataObjectFactory
22+
*/
23+
private $dataObjectFactory;
24+
25+
public function __construct(
26+
DataObjectFactory $dataObjectFactory,
27+
SendFriend $sendFriend
28+
) {
29+
$this->sendFriend = $sendFriend;
30+
$this->dataObjectFactory = $dataObjectFactory;
31+
}
32+
33+
/**
34+
* @param $args
35+
* @param array $recipientsArray
36+
* @throws GraphQlInputException
37+
*/
38+
public function validate($args, array $recipientsArray): void
39+
{
40+
$this->prepareDataForSendFriendValidation($args, $recipientsArray);
41+
$validationResult = $this->sendFriend->validate();
42+
if ($validationResult !== true) {
43+
throw new GraphQlInputException(__(implode($validationResult)));
44+
}
45+
if ($this->sendFriend->getMaxSendsToFriend() && $this->sendFriend->isExceedLimit()) {
46+
throw new GraphQlInputException(__('You can\'t send messages more than %1 times an hour.',
47+
$this->sendFriend->getMaxSendsToFriend()
48+
));
49+
}
50+
}
51+
52+
private function prepareDataForSendFriendValidation(array $args, array $recipientsArray): void
53+
{
54+
$sender = $this->dataObjectFactory->create()->setData([
55+
'name' => $args['input']['sender']['name'],
56+
'email'=> $args['input']['sender']['email'],
57+
'message' => $args['input']['sender']['message'],
58+
]);
59+
$emails = [];
60+
foreach ($recipientsArray['recipients'] as $recipient) {
61+
$emails[] = $recipient['email'];
62+
}
63+
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails);
64+
65+
$this->sendFriend->setData('_sender', $sender);
66+
$this->sendFriend->setData('_recipients', $recipients);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)