Skip to content
Snippets Groups Projects
Commit ddd8b796 authored by Shanu Chouhan's avatar Shanu Chouhan Committed by Mohammad Abdul-Qader
Browse files

Issue #3359950 by Shanu Chouhan, apaderno, m.abdulqader: Fix the issues reported by phpcs

parent 5787d897
No related branches found
No related tags found
No related merge requests found
......@@ -9,59 +9,62 @@ use GuzzleHttp\Exception\RequestException;
/**
* Class DiscordManager.
*
* {@inheritdoc}
*/
class DiscordManager {
use StringTranslationTrait;
use StringTranslationTrait;
/**
* GuzzleHttp\ClientInterface definition.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* GuzzleHttp\ClientInterface definition.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Logger service.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Logger service.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs a new DiscordManager object.
*/
public function __construct(ClientInterface $http_client, LoggerChannelFactoryInterface $logger_factory) {
$this->httpClient = $http_client;
$this->logger = $logger_factory->get('unified_mail_dispatcher');
}
/**
* Sends a message to the Discord channel via the provided webhook URL.
*
* @param string $webhook_url
* The Discord webhook URL.
* @param array $message
* The message to be sent.
*
* @return bool
* Returns TRUE on success, FALSE on failure.
*/
public function send(string $webhook_url, array $message): bool {
try {
$response = $this->httpClient->request('POST', $webhook_url, [
'json' => $message,
]);
/**
* Constructs a new DiscordManager object.
*/
public function __construct(ClientInterface $http_client, LoggerChannelFactoryInterface $logger_factory) {
$this->httpClient = $http_client;
$this->logger = $logger_factory->get('unified_mail_dispatcher');
}
// Check if the request was successful.
if ($response->getStatusCode() == 204) {
return TRUE;
}
} catch (RequestException $e) {
$this->logger->error($this->t('Could not send Discord notification. Error message: @error', ['@error' => $e->getMessage()]));
}
/**
* Sends a message to the Discord channel via the provided webhook URL.
*
* @param string $webhook_url
* The Discord webhook URL.
* @param array $message
* The message to be sent.
*
* @return bool
* Returns TRUE on success, FALSE on failure.
*/
public function send(string $webhook_url, array $message): bool {
try {
$response = $this->httpClient->request('POST', $webhook_url, [
'json' => $message,
]);
return FALSE;
// Check if the request was successful.
if ($response->getStatusCode() == 204) {
return TRUE;
}
}
catch (RequestException $e) {
$this->logger->error($this->t('Could not send Discord notification. Error message: @error', ['@error' => $e->getMessage()]));
}
return FALSE;
}
}
......@@ -5,85 +5,91 @@ namespace Drupal\unified_mail_dispatcher\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* {@inheritdoc}
*/
class UnifiedMailDispatcherSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'unified_mail_dispatcher_settings_form';
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'unified_mail_dispatcher_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['unified_mail_dispatcher.settings'];
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['unified_mail_dispatcher.settings'];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('unified_mail_dispatcher.settings');
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('unified_mail_dispatcher.settings');
$form['mail_alter'] = [
'#type' => 'checkbox',
'#title' => $this->t('Activate Route Alter'),
'#default_value' => $config->get('mail_alter'),
];
$form['mail_alter'] = [
'#type' => 'checkbox',
'#title' => $this->t('Activate Route Alter'),
'#default_value' => $config->get('mail_alter'),
];
$form['alter_option'] = [
'#type' => 'radios',
'#title' => $this->t('Alter Option'),
'#options' => [
'discord' => $this->t('Send to Discord'),
'email' => $this->t('Send to Single Email'),
],
'#default_value' => $config->get('alter_option'),
'#states' => [
'visible' => [
':input[name="mail_alter"]' => ['checked' => TRUE],
],
],
];
$form['alter_option'] = [
'#type' => 'radios',
'#title' => $this->t('Alter Option'),
'#options' => [
'discord' => $this->t('Send to Discord'),
'email' => $this->t('Send to Single Email'),
],
'#default_value' => $config->get('alter_option'),
'#states' => [
'visible' => [
':input[name="mail_alter"]' => ['checked' => TRUE],
],
],
];
$form['webhook_url'] = [
'#type' => 'url',
'#title' => $this->t('Discord Webhook URL'),
'#default_value' => $config->get('webhook_url'),
'#states' => [
'visible' => [
':input[name="alter_option"]' => ['value' => 'discord'],
':input[name="mail_alter"]' => ['checked' => TRUE],
],
],
];
$form['webhook_url'] = [
'#type' => 'url',
'#title' => $this->t('Discord Webhook URL'),
'#default_value' => $config->get('webhook_url'),
'#states' => [
'visible' => [
':input[name="alter_option"]' => ['value' => 'discord'],
':input[name="mail_alter"]' => ['checked' => TRUE],
],
],
];
$form['email'] = [
'#type' => 'email',
'#title' => $this->t('Single Email Address'),
'#default_value' => $config->get('email'),
'#states' => [
'visible' => [
':input[name="alter_option"]' => ['value' => 'email'],
':input[name="mail_alter"]' => ['checked' => TRUE],
],
],
];
$form['email'] = [
'#type' => 'email',
'#title' => $this->t('Single Email Address'),
'#default_value' => $config->get('email'),
'#states' => [
'visible' => [
':input[name="alter_option"]' => ['value' => 'email'],
':input[name="mail_alter"]' => ['checked' => TRUE],
],
],
];
return parent::buildForm($form, $form_state);
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array&$form, FormStateInterface $form_state) {
$this->config('unified_mail_dispatcher.settings')
->set('mail_alter', $form_state->getValue('mail_alter'))
->set('alter_option', $form_state->getValue('alter_option'))
->set('webhook_url', $form_state->getValue('webhook_url'))
->set('email', $form_state->getValue('email'))
->save();
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('unified_mail_dispatcher.settings')
->set('mail_alter', $form_state->getValue('mail_alter'))
->set('alter_option', $form_state->getValue('alter_option'))
->set('webhook_url', $form_state->getValue('webhook_url'))
->set('email', $form_state->getValue('email'))
->save();
parent::submitForm($form, $form_state);
}
parent::submitForm($form, $form_state);
}
}
......@@ -6,33 +6,40 @@ use Drupal\Core\Mail\MailManager;
/**
* Class UnifiedMailManager.
*
* {@inheritdoc}
*/
class UnifiedMailManager extends MailManager {
public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
$config = \Drupal::config('unified_mail_dispatcher.settings');
$alter = $config->get('mail_alter');
if ($alter) {
$option = $config->get('alter_option');
if ($option == 'discord') {
// Load the discord service
$discord_manager = \Drupal::service('unified_mail_dispatcher.discord_manager');
$webhook_url = $config->get('webhook_url');
// Construct your message
$message = [
'content' => "Module: $module\nKey: $key\nRecipient: $to\nLanguage: $langcode\nParams: ",
];
$discord_manager->send($webhook_url, $message);
return TRUE;
} elseif ($option == 'email') {
// Send email to the specified single email
$to = $config->get('email');
}
}
// If not altered, or email option is selected, send the email normally
return parent::mail($module, $key, $to, $langcode, $params, $reply, $send);
/**
* {@inheritdoc}
*/
public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
$config = \Drupal::config('unified_mail_dispatcher.settings');
$alter = $config->get('mail_alter');
if ($alter) {
$option = $config->get('alter_option');
if ($option == 'discord') {
// Load the discord service.
$discord_manager = \Drupal::service('unified_mail_dispatcher.discord_manager');
$webhook_url = $config->get('webhook_url');
// Construct your message.
$message = [
'content' => "Module: $module\nKey: $key\nRecipient: $to\nLanguage: $langcode\nParams: ",
];
$discord_manager->send($webhook_url, $message);
return TRUE;
}
elseif ($option == 'email') {
// Send email to the specified single email.
$to = $config->get('email');
}
}
// If not altered, or email option is selected, send the email normally.
return parent::mail($module, $key, $to, $langcode, $params, $reply, $send);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment