Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
unified_mail_dispatcher
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
unified_mail_dispatcher
Commits
ddd8b796
Commit
ddd8b796
authored
1 year ago
by
Shanu Chouhan
Committed by
Mohammad Abdul-Qader
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/DiscordManager.php
+48
-45
48 additions, 45 deletions
src/DiscordManager.php
src/Form/UnifiedMailDispatcherSettingsForm.php
+75
-69
75 additions, 69 deletions
src/Form/UnifiedMailDispatcherSettingsForm.php
src/UnifiedMailManager.php
+32
-25
32 additions, 25 deletions
src/UnifiedMailManager.php
with
155 additions
and
139 deletions
src/DiscordManager.php
+
48
−
45
View file @
ddd8b796
...
...
@@ -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
;
}
}
This diff is collapsed.
Click to expand it.
src/Form/UnifiedMailDispatcherSettingsForm.php
+
75
−
69
View file @
ddd8b796
...
...
@@ -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
);
}
}
This diff is collapsed.
Click to expand it.
src/UnifiedMailManager.php
+
32
−
25
View file @
ddd8b796
...
...
@@ -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
\n
Key:
$key
\n
Recipient:
$to
\n
Language:
$langcode
\n
Params: "
,
];
$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
\n
Key:
$key
\n
Recipient:
$to
\n
Language:
$langcode
\n
Params: "
,
];
$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
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment