-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: 7.x-2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -140,4 +162,18 @@ abstract class MessageNotifierBase implements MessageNotifierInterface { | |
public function access() { | ||
return TRUE; | ||
} | ||
|
||
public function getLogger() { | ||
if (!isset($this->logger)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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, | ||
), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing PHPdocs.