Skip to content
Snippets Groups Projects
Commit d0678e39 authored by Brady's avatar Brady
Browse files

Reformat and whitespace cleanup

parent 09f5cf0f
Branches
Tags 8.x-2.2
1 merge request!30Issue #2971991 by amykhailova, code-drupal: "Cc" Recipient not receiving email...
{
"name": "drupal/sendgrid_integration",
"description": "Drupal module for integrating with the SendGrid API.",
"type": "drupal-module",
"homepage": "https://www.drupal.org/project/sendgrid_integration",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Brady Owens (perignon)",
"homepage": "https://www.drupal.org/u/perignon",
"role": "Maintainer"
}
],
"keywords": [
"SendGrid",
"sendgrid",
"email",
"send",
"grid",
"php"
],
"support": {
"issues": "https://drupal.org/project/issues/sendgrid_integration",
"source": "https://git.drupalcode.org/project/sendgrid_integration"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/taz77/sendgrid-php-ng"
}
],
"require": {
"fastglass/sendgrid": "~2.0.0",
"html2text/html2text": "^4.3.1",
"ext-json": "*"
"name": "drupal/sendgrid_integration",
"description": "Drupal module for integrating with the SendGrid API.",
"type": "drupal-module",
"homepage": "https://www.drupal.org/project/sendgrid_integration",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Brady Owens (perignon)",
"homepage": "https://www.drupal.org/u/perignon",
"role": "Maintainer"
}
],
"keywords": [
"SendGrid",
"sendgrid",
"email",
"send",
"grid",
"php"
],
"support": {
"issues": "https://drupal.org/project/issues/sendgrid_integration",
"source": "https://git.drupalcode.org/project/sendgrid_integration"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/taz77/sendgrid-php-ng"
}
],
"require": {
"fastglass/sendgrid": "~2.0.0",
"html2text/html2text": "^4.3.1",
"ext-json": "*"
}
}
......@@ -9,4 +9,4 @@ services:
class: Drupal\sendgrid_integration_reports\Api
tags:
- { name: sendgrid }
arguments: ['@config.factory', '@messenger', '@logger.factory', '@module_handler', '@cache_factory']
arguments: [ '@config.factory', '@messenger', '@logger.factory', '@module_handler', '@cache_factory' ]
......@@ -111,60 +111,6 @@ class Api {
}
}
/**
* Sets the cache to sendgrid_integration_reports bin.
*
* @param string $cid
* Cache Id.
* @param array $data
* The data should be cached.
*/
protected function setCache($cid, array $data) {
if (!empty($data)) {
$this->cacheFactory->get($this->bin)->set($cid, $data);
}
}
/**
* Returns response from SendGrid.
*
* @param string $path
* Part of SendGrid endpoint.
* @param array $query
* Query params to the request.
* @param string $onBehalfOf
* Subuser to peform this request on behalf of.
*
* @return bool|mixed
* Decoded json or FALSE.
*/
protected function getResponse($path, array $query, string $onBehalfOf = '') {
// Set headers and create a Guzzle client to communicate with Sendgrid.
$headers['Authorization'] = 'Bearer ' . $this->apiKey;
if ($onBehalfOf) {
$headers['on-behalf-of'] = $onBehalfOf;
}
$clienttest = new Client([
'base_uri' => 'https://api.sendgrid.com/v3/',
'headers' => $headers,
]);
// Lets attempt the request and catch an error if it fails.
try {
$response = $clienttest->get($path, ['query' => $query]);
}
catch (ClientException $e) {
$code = Xss::filter($e->getCode());
$this->loggerFactory->get('sendgrid_integration_reports')
->error(t('SendGrid Reports module failed to receive data. HTTP Error Code @errno', ['@errno' => $code]));
$this->messenger->addError(t('SendGrid Reports module failed to receive data. See logs.'));
return FALSE;
}
// Sanitize return before using in Drupal.
$body = Xss::filter($response->getBody());
return json_decode($body);
}
/**
* Returns stats.
*
......@@ -264,6 +210,60 @@ class Api {
return $data;
}
/**
* Returns response from SendGrid.
*
* @param string $path
* Part of SendGrid endpoint.
* @param array $query
* Query params to the request.
* @param string $onBehalfOf
* Subuser to peform this request on behalf of.
*
* @return bool|mixed
* Decoded json or FALSE.
*/
protected function getResponse($path, array $query, string $onBehalfOf = '') {
// Set headers and create a Guzzle client to communicate with Sendgrid.
$headers['Authorization'] = 'Bearer ' . $this->apiKey;
if ($onBehalfOf) {
$headers['on-behalf-of'] = $onBehalfOf;
}
$clienttest = new Client([
'base_uri' => 'https://api.sendgrid.com/v3/',
'headers' => $headers,
]);
// Lets attempt the request and catch an error if it fails.
try {
$response = $clienttest->get($path, ['query' => $query]);
}
catch (ClientException $e) {
$code = Xss::filter($e->getCode());
$this->loggerFactory->get('sendgrid_integration_reports')
->error(t('SendGrid Reports module failed to receive data. HTTP Error Code @errno', ['@errno' => $code]));
$this->messenger->addError(t('SendGrid Reports module failed to receive data. See logs.'));
return FALSE;
}
// Sanitize return before using in Drupal.
$body = Xss::filter($response->getBody());
return json_decode($body);
}
/**
* Sets the cache to sendgrid_integration_reports bin.
*
* @param string $cid
* Cache Id.
* @param array $data
* The data should be cached.
*/
protected function setCache($cid, array $data) {
if (!empty($data)) {
$this->cacheFactory->get($this->bin)->set($cid, $data);
}
}
/**
* Returns browser stats.
*/
......@@ -372,29 +372,6 @@ class Api {
return $data;
}
/**
* Get list of subusers.
*/
public function getSubusers() {
$cid = 'sendgrid_reports_subusers';
if ($cache = $this->cacheFactory->get($this->bin)->get($cid)) {
return $cache->data;
}
$path = 'subusers';
$query = [
'limit' => 500,
'offset' => 0,
];
$subusers = [];
do {
$response = $this->getResponse($path, $query);
$query['offset'] += 500;
$subusers = array_merge($subusers, $response);
} while (!empty($response));
$this->setCache($cid, $subusers);
return $subusers;
}
/**
* Get bounces by subuser.
*/
......@@ -421,4 +398,27 @@ class Api {
return $bounces;
}
/**
* Get list of subusers.
*/
public function getSubusers() {
$cid = 'sendgrid_reports_subusers';
if ($cache = $this->cacheFactory->get($this->bin)->get($cid)) {
return $cache->data;
}
$path = 'subusers';
$query = [
'limit' => 500,
'offset' => 0,
];
$subusers = [];
do {
$response = $this->getResponse($path, $query);
$query['offset'] += 500;
$subusers = array_merge($subusers, $response);
} while (!empty($response));
$this->setCache($cid, $subusers);
return $subusers;
}
}
......@@ -52,13 +52,6 @@ class SendGridReportsSettingsForm extends ConfigFormBase {
return 'sendgrid_integration_reports_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['sendgrid_integration_reports.settings'];
}
/**
* {@inheritdoc}
*/
......@@ -143,4 +136,11 @@ class SendGridReportsSettingsForm extends ConfigFormBase {
\Drupal::cache('sendgrid_integration_reports')->deleteAll();
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['sendgrid_integration_reports.settings'];
}
}
......@@ -52,13 +52,6 @@ class SendGridSettingsForm extends ConfigFormBase {
return 'sendgrid_integration_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['sendgrid_integration.settings'];
}
/**
* {@inheritdoc}
*/
......@@ -106,14 +99,14 @@ class SendGridSettingsForm extends ConfigFormBase {
'#type' => 'checkbox',
'#title' => $this->t('Track Opens'),
'#description' => $this->t('Track opening of emails. This will include a small image in each email. Set to off by default.'),
'#default_value' => !empty($config->get('trackopens')) ? $config->get('trackopens') : 0
'#default_value' => !empty($config->get('trackopens')) ? $config->get('trackopens') : 0,
];
$form['settings']['sendgrig_intergration_trackclicks'] = [
'#type' => 'checkbox',
'#title' => $this->t('Track Clicks'),
'#description' => $this->t('Track the clicking of links in email. Set to off by default.'),
'#default_value' => !empty($config->get('trackclicks')) ? $config->get('trackclicks') : 0
'#default_value' => !empty($config->get('trackclicks')) ? $config->get('trackclicks') : 0,
];
return parent::buildForm($form, $form_state);
......@@ -170,4 +163,11 @@ class SendGridSettingsForm extends ConfigFormBase {
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['sendgrid_integration.settings'];
}
}
......@@ -146,7 +146,7 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
// The doMail function returns a boolean.
$mail = $this->doMail($message);
}
// Log the exception.
// Log the exception.
catch (SendgridException $e) {
$this->logger->error($e->getMessage());
}
......@@ -651,6 +651,24 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
return FALSE;
}
/**
* Split an email address into it's name and address components.
* Returns an array with the first element as the email address and the
* second element as the name.
*
* @param $email string
*
* @return mixed
*/
protected function parseAddress(string $email) {
if (preg_match(self::SENDGRID_INTEGRATION_EMAIL_REGEX, $email, $matches)) {
return [$matches[2], strval($matches[1])];
}
else {
return [$email];
}
}
/**
* Returns a string that is contained within another string.
*
......@@ -765,24 +783,6 @@ class SendGridMail implements MailInterface, ContainerFactoryPluginInterface {
return implode("\n", $part_array);
}
/**
* Split an email address into it's name and address components.
* Returns an array with the first element as the email address and the
* second element as the name.
*
* @param $email string
*
* @return mixed
*/
protected function parseAddress(string $email) {
if (preg_match(self::SENDGRID_INTEGRATION_EMAIL_REGEX, $email, $matches)) {
return [$matches[2], strval($matches[1])];
}
else {
return [$email];
}
}
/**
* Return an array structure for a message attachment.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment