|
| 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\ContactGraphQlPwa\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Contact\Model\ConfigInterface; |
| 11 | +use Magento\Contact\Model\MailInterface; |
| 12 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 13 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 14 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 15 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 16 | +use Magento\Framework\Validator\EmailAddress; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class ContactUs implements ResolverInterface |
| 20 | +{ |
| 21 | + public const DEFAULT_VALUES = [ |
| 22 | + 'telephone' => '-' |
| 23 | + ]; |
| 24 | + |
| 25 | + /** @var \Magento\Contact\Model\MailInterface */ |
| 26 | + private $mail; |
| 27 | + |
| 28 | + /** @var \Magento\Contact\Model\ConfigInterface */ |
| 29 | + private $contactConfig; |
| 30 | + |
| 31 | + /** @var \Psr\Log\LoggerInterface */ |
| 32 | + private $logger; |
| 33 | + |
| 34 | + /** @var \Magento\Framework\Validator\EmailAddress */ |
| 35 | + private $emailValidator; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param \Magento\Contact\Model\MailInterface $mail |
| 39 | + * @param \Magento\Contact\Model\ConfigInterface $contactConfig |
| 40 | + * @param \Psr\Log\LoggerInterface $logger |
| 41 | + * @param \Magento\Framework\Validator\EmailAddress $emailValidator |
| 42 | + */ |
| 43 | + public function __construct( |
| 44 | + MailInterface $mail, |
| 45 | + ConfigInterface $contactConfig, |
| 46 | + LoggerInterface $logger, |
| 47 | + EmailAddress $emailValidator |
| 48 | + ) { |
| 49 | + $this->mail = $mail; |
| 50 | + $this->contactConfig = $contactConfig; |
| 51 | + $this->logger = $logger; |
| 52 | + $this->emailValidator = $emailValidator; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritDoc |
| 57 | + */ |
| 58 | + public function resolve( |
| 59 | + Field $field, |
| 60 | + $context, |
| 61 | + ResolveInfo $info, |
| 62 | + array $value = null, |
| 63 | + array $args = null |
| 64 | + ) { |
| 65 | + if (!$this->contactConfig->isEnabled()) { |
| 66 | + throw new GraphQlInputException( |
| 67 | + __('The contact form is unavailable.') |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + $input = $this->cleanInput($args['input']); |
| 72 | + $this->validateInput($input); |
| 73 | + |
| 74 | + $variables = ['data' => $input]; |
| 75 | + try { |
| 76 | + $this->mail->send($input['email'], $variables); |
| 77 | + } catch (\Exception $e) { |
| 78 | + $this->logger->critical($e); |
| 79 | + |
| 80 | + throw new GraphQlInputException( |
| 81 | + __('An error occurred while processing your form. Please try again later.') |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + return [ |
| 86 | + 'status' => true |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get default values map |
| 92 | + * |
| 93 | + * @return string[] |
| 94 | + */ |
| 95 | + public function getDefaultValues(): array |
| 96 | + { |
| 97 | + return self::DEFAULT_VALUES; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Clean input values and set default values |
| 102 | + * |
| 103 | + * @param array<string, string> $input |
| 104 | + * |
| 105 | + * @return array<string, string> |
| 106 | + */ |
| 107 | + public function cleanInput(array $input): array |
| 108 | + { |
| 109 | + $values = []; |
| 110 | + $defaults = $this->getDefaultValues(); |
| 111 | + foreach ($input as $field => $value) { |
| 112 | + $cleanValue = $value === null ? '' : trim($value); |
| 113 | + |
| 114 | + if ($cleanValue === '' && isset($defaults[$field])) { |
| 115 | + $cleanValue = $defaults[$field]; |
| 116 | + } |
| 117 | + |
| 118 | + $values[$field] = $cleanValue; |
| 119 | + } |
| 120 | + |
| 121 | + foreach ($defaults as $field => $value) { |
| 122 | + if (!isset($values[$field])) { |
| 123 | + $values[$field] = $value; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return $values; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Validate input data |
| 132 | + * |
| 133 | + * @param array<string, string> $input |
| 134 | + * |
| 135 | + * @return void |
| 136 | + * @throws \Magento\Framework\GraphQl\Exception\GraphQlInputException |
| 137 | + */ |
| 138 | + public function validateInput(array $input): void |
| 139 | + { |
| 140 | + if (!$this->emailValidator->isValid($input['email'])) { |
| 141 | + throw new GraphQlInputException( |
| 142 | + __('The email address is invalid. Verify the email address and try again.') |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + if ($input['name'] === '') { |
| 147 | + throw new GraphQlInputException(__('Enter the Name and try again.')); |
| 148 | + } |
| 149 | + |
| 150 | + if ($input['comment'] === '') { |
| 151 | + throw new GraphQlInputException(__('Enter the comment and try again.')); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments