abstract class DigestBase in Message Digest 8
Message Digest notifier.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\message_notify\Plugin\Notifier\MessageNotifierBase implements MessageNotifierInterface
- class \Drupal\message_digest\Plugin\Notifier\DigestBase implements ContainerFactoryPluginInterface, DigestInterface
- class \Drupal\message_notify\Plugin\Notifier\MessageNotifierBase implements MessageNotifierInterface
Expanded class hierarchy of DigestBase
File
- src/
Plugin/ Notifier/ DigestBase.php, line 22
Namespace
Drupal\message_digest\Plugin\NotifierView source
abstract class DigestBase extends MessageNotifierBase implements ContainerFactoryPluginInterface, DigestInterface {
/**
* Database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The digest interval.
*
* @var string
*/
protected $digestInterval;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* The state service for tracking last sent time.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs the digest notifier plugins.
*
* @param array $configuration
* Plugin configuration array.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* The message notify logger channel.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The rendering service.
* @param \Drupal\message\MessageInterface $message
* (optional) The message entity.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, MessageInterface $message = NULL, StateInterface $state, Connection $connection, TimeInterface $time) {
// Set some defaults.
$configuration += [
'entity_type' => '',
'entity_id' => '',
];
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $entity_type_manager, $renderer, $message);
$this->connection = $connection;
$this->digestInterval = $plugin_definition['digest_interval'];
$this->time = $time;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MessageInterface $message = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.channel.message_notify'), $container
->get('entity_type.manager'), $container
->get('renderer'), $message, $container
->get('state'), $container
->get('database'), $container
->get('datetime.time'));
}
/**
* {@inheritdoc}
*/
public function deliver(array $output = []) {
// Do not actually deliver this message because it will be delivered
// via cron in a digest, but return TRUE to prevent a logged error.
// Instead, we "deliver" it to the message_digest DB table so that it
// can be retrieved at a later time.
$message = $this->message;
$message_digest = [
'receiver' => $message
->getOwnerId(),
'entity_type' => $this->configuration['entity_type'],
'entity_id' => $this->configuration['entity_id'],
'notifier' => $this
->getPluginId(),
'timestamp' => $message
->getCreatedTime(),
];
// Don't allow entity_id without entity_type, or the reverse.
if ($this->configuration['entity_type'] xor $this->configuration['entity_id']) {
throw new InvalidDigestGroupingException(sprintf('Tried to create a message digest without both entity_type (%s) and entity_id (%s). These either both need to be empty, or have values.', $this->configuration['entity_type'], $this->configuration['entity_id']));
}
// Our $message is a cloned copy of the original $message with the mid field
// removed to prevent overwriting (this happens in message_subscribe) so we
// need to fetch the mid manually.
$mid = $message
->id();
if (!$mid && isset($message->original_message)) {
$mid = $message->original_message
->id();
}
assert(!empty($mid), 'The message entity (or $message->original_message) must be saved in order to create a digest entry.');
$message_digest['mid'] = $mid;
$this->connection
->insert('message_digest')
->fields($message_digest)
->execute();
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getInterval() {
return $this->digestInterval;
}
/**
* {@inheritdoc}
*/
public function getRecipients() {
$query = $this->connection
->select('message_digest', 'md');
$query
->fields('md', [
'receiver',
]);
$query
->condition('timestamp', $this
->getEndTime(), '<=');
$query
->condition('sent', 0);
$query
->condition('notifier', $this
->getPluginId());
$query
->distinct();
return $query
->execute()
->fetchCol();
}
/**
* {@inheritdoc}
*/
public function aggregate($uid, $end) {
$message_groups = [];
$query = $this->connection
->select('message_digest', 'md');
$query
->fields('md')
->condition('timestamp', $end, '<=')
->condition('receiver', $uid)
->condition('sent', 0)
->condition('notifier', $this
->getPluginId());
$query
->orderBy('id');
$result = $query
->execute();
foreach ($result as $row) {
$entity_type = $row->entity_type;
$entity_id = $row->entity_id;
$context = [
'data' => $row,
// Set this to zero to aggregate group content.
'entity_type' => $entity_type,
'entity_id' => $entity_id,
];
if (!empty($context['data']->mid)) {
$message_groups[$context['entity_type']][$context['entity_id']][] = $context['data']->mid;
}
}
return $message_groups;
}
/**
* Determine if it is time to process this digest or not.
*
* @return bool
* Returns TRUE if a sufficient amount of time has passed.
*/
public function processDigest() {
$interval = $this
->getInterval();
$key = $this->pluginId . '_last_run';
$last_run = $this->state
->get($key, 0);
// Allow some buffer for environmental differences that cause minor
// variations in the request times. This will prevent digest processing from
// being pushed to the next cron run when it really should be going this
// time.
$buffer = 30;
return $last_run < strtotime('-' . $interval, $this->time
->getRequestTime()) + $buffer;
}
/**
* {@inheritdoc}
*/
public function markSent(UserInterface $account, $last_mid) {
$this->connection
->update('message_digest')
->fields([
'sent' => 1,
])
->condition('receiver', $account
->id())
->condition('notifier', $this
->getPluginId())
->condition('mid', $last_mid, '<=')
->execute();
}
/**
* {@inheritdoc}
*/
public function getEndTime() {
return $this->time
->getRequestTime();
}
/**
* {@inheritdoc}
*/
public function setLastSent() {
$this->state
->set($this
->getPluginId() . '_last_run', $this->time
->getRequestTime());
}
/**
* Implements the magic __sleep() method.
*/
public function __sleep() {
// Only serialize the local properties, ignoring all dependencies from the
// container. The database connection cannot be serialized and neither can
// other services like the state service and the entity type manager since
// they in turn also depend on an active database connection.
return [
'configuration',
'pluginId',
'pluginDefinition',
'message',
'digestInterval',
];
}
/**
* Implements the magic __wakeup() method.
*/
public function __wakeup() {
// Restore the database connection.
$this->connection = Database::getConnection();
// Restore the dependencies from the container.
$container = \Drupal::getContainer();
$this->entityTypeManager = $container
->get('entity_type.manager');
$this->logger = $container
->get('logger.channel.message_notify');
$this->renderer = $container
->get('renderer');
$this->state = $container
->get('state');
$this->time = $container
->get('datetime.time');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DigestBase:: |
protected | property | Database connection. | |
DigestBase:: |
protected | property | The digest interval. | |
DigestBase:: |
protected | property | The state service for tracking last sent time. | |
DigestBase:: |
protected | property | The time service. | |
DigestBase:: |
public | function |
Aggregate all of the messages for this digest. Overrides DigestInterface:: |
|
DigestBase:: |
public static | function |
Creates an instance of the plugin. Overrides MessageNotifierBase:: |
|
DigestBase:: |
public | function |
Deliver a message via the required transport method. Overrides MessageNotifierInterface:: |
|
DigestBase:: |
public | function |
Gets the end time for which to digest messages prior to. Overrides DigestInterface:: |
|
DigestBase:: |
public | function |
The interval to compile digests for. Overrides DigestInterface:: |
|
DigestBase:: |
public | function |
Get a unique list of recipient user IDs for this digest. Overrides DigestInterface:: |
|
DigestBase:: |
public | function |
Mark the sent digest messages as sent in the message_digest DB table. Overrides DigestInterface:: |
|
DigestBase:: |
public | function |
Determine if it is time to process this digest or not. Overrides DigestInterface:: |
|
DigestBase:: |
public | function |
Sets the last sent time. Overrides DigestInterface:: |
|
DigestBase:: |
public | function |
Constructs the digest notifier plugins. Overrides MessageNotifierBase:: |
|
DigestBase:: |
public | function | Implements the magic __sleep() method. | |
DigestBase:: |
public | function | Implements the magic __wakeup() method. | |
MessageNotifierBase:: |
protected | property | The entity type manager service. | |
MessageNotifierBase:: |
protected | property | The logger channel. | |
MessageNotifierBase:: |
protected | property | The message entity. | |
MessageNotifierBase:: |
protected | property | The rendering service. | |
MessageNotifierBase:: |
public | function |
Determine if user can access notifier. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public | function |
Save the rendered messages if needed.
Invoke watchdog error on failure. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public | function |
Entry point to send and process a message. Overrides MessageNotifierInterface:: |
|
MessageNotifierBase:: |
public | function |
Set the message object for the notifier. Overrides MessageNotifierInterface:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |