Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a logger delegate class for logging. #2

Open
wants to merge 2 commits into
base: 7.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions message_notify.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies[] = ctools
; Notifier plugins
files[] = includes/message_notify.exception.inc
files[] = plugins/notifier/abstract.inc
files[] = plugins/notifier_logger/abstract.inc

; Tests
files[] = message_notify.test
21 changes: 21 additions & 0 deletions message_notify.module
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ function message_notify_plugin_process(&$plugin, $info) {
'save on success' => TRUE,
'language override' => FALSE,
'rendered fields' => array(),
'logger plugin' => 'watchdog',
);
}

/**
* Add defaults values to the notifier logger plugins.
*
* - 'description': The description of the plugin.
* - 'options': An array of options.
*/
function message_notify_logger_plugin_process(&$plugin, $info) {
$plugin += array(
'description' => '',
'options' => array(),
);
}

Expand Down Expand Up @@ -127,6 +141,9 @@ function message_notify_ctools_plugin_api($module, $api) {
if ($module == 'message_notify' && $api == 'notifier') {
return array('version' => 1);
}
if ($module == 'message_notify' && $api == 'notifier_logger') {
return array('version' => 1);
}
}

/**
Expand All @@ -137,6 +154,10 @@ function message_notify_ctools_plugin_type() {
'classes' => array('class'),
'process' => 'message_notify_plugin_process',
);
$plugins['notifier_logger'] = array(
'classes' => array('class'),
'process' => 'message_notify_logger_plugin_process',
);
return $plugins;
}

Expand Down
46 changes: 41 additions & 5 deletions plugins/notifier/abstract.inc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ interface MessageNotifierInterface {
*/
public function __construct($plugin, Message $message);


/**
* Return the plugin definition array that is passed in on construction.
*
* @return array
* The ctools plugin definiton array.
*/
public function getPluginDefinition();

/**
* Entry point to send and process a message.
*
Expand Down Expand Up @@ -48,6 +57,11 @@ interface MessageNotifierInterface {
* Determine if user can access notifier.
*/
public function access();

/**
* Return an instance of a class for logging send results.
*/
public function getLogger();
}

/**
Expand All @@ -65,6 +79,15 @@ abstract class MessageNotifierBase implements MessageNotifierInterface {
*/
protected $message;

/**
* The logger class for this Notfier.
*/
protected $logger;

public function getPluginDefinition() {
return $this->plugin;
}

public function __construct($plugin, Message $message) {
$this->plugin = $plugin;
$this->message = $message;
Expand Down Expand Up @@ -96,12 +119,11 @@ abstract class MessageNotifierBase implements MessageNotifierInterface {

$options = $plugin['options'];

// Log the result of sending.
$this->getLogger()->log($result, $message);
$save = FALSE;
if (!$result) {
watchdog('message_notify', t('Could not send message using @title to user ID @uid.'), array('@label' => $plugin['title'], '@uid' => $message->uid), WATCHDOG_ERROR);
if ($options['save on fail']) {
$save = TRUE;
}
if (!$result && $options['save on fail']) {
$save = TRUE;
}
elseif ($result && $options['save on success']) {
$save = TRUE;
Expand Down Expand Up @@ -140,4 +162,18 @@ abstract class MessageNotifierBase implements MessageNotifierInterface {
public function access() {
return TRUE;
}

public function getLogger() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing PHPdocs.

if (!isset($this->logger)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets return early if possible, i.e.

if (!empty($this->logger) {
  return $this->logger;
}

...

$options = $this->plugin['options'];
$logger_plugin = $options['logger plugin'];

ctools_include('plugins');
$plugin = ctools_get_plugins('message_notify', 'notifier_logger', $options['logger plugin']);

$class = ctools_plugin_load_class('message_notify', 'notifier_logger', $logger_plugin, 'class');
$this->logger = new $class($plugin, $this);
}
return $this->logger;
}
}
21 changes: 21 additions & 0 deletions plugins/notifier_logger/abstract.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Interface for logging notification sending of messages.
*/
interface MessageNotifierLoggerInterface {

public function __construct($plugin, MessageNotifierInterface $notifier);


/**
* Log the sending of a message.
*
* @param $result
* The sending result from the notifier class.
* @param $message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param Message $message

* The message that was sent.
*/
public function log($result, $message);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Simple class the implements logging to the watchdog.
*/
class MessageNotifierLoggerWatchdog implements MessageNotifierLoggerInterface {

public function __construct($plugin, MessageNotifierInterface $notifier) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPdocs here and below.

$this->plugin = $plugin;
$this->notifier = $notifier;
}

public function log($result, $message) {
$options = $this->plugin['options'];
$notifier_definition = $this->notifier->getPluginDefinition();
if (!$result && $options['log on fail']) {
watchdog('message_notify', t('Could not send message using @title to user ID @uid.'), array('@title' => $notifier_definition['title'], '@uid' => $message->uid), WATCHDOG_ERROR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the t() isn't needed here.

}
elseif ($result && $options['log on success']) {
watchdog('message_notify', t('Sent message using @title to user ID @uid.'), array('@title' => $notifier_definition['title'], '@uid' => $message->uid), WATCHDOG_INFO);
}
}
}
11 changes: 11 additions & 0 deletions plugins/notifier_logger/watchdog/watchdog.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$plugin = array(
'title' => t('Watchdog'),
'description' => t('Logs failed message notifications to the watchdog.'),
'class' => 'MessageNotifierLoggerWatchdog',
'options' => array(
'log on fail' => TRUE,
'log on success' => FALSE,
),
);