public function CourierManager::sendMessage in Courier 2.x
Same name and namespace in other branches
- 8 src/Service/CourierManager.php \Drupal\courier\Service\CourierManager::sendMessage()
Prepares messages for an identity and and queues them for transmission.
Once this method is executed, responsibility for transmission is passed to Courier.
$options = [
'my_option' => 123,
'channels' => [
'courier_email' => [
'foo' => 456,
],
'sms' => [
'bar' => 679,
],
],
];
// Will transform options into this array when sending to courier_email:
$options = [
'my_option' => 123,
'foo' => 456,
];
Parameters
\Drupal\courier\TemplateCollectionInterface $template_collection: A template collection entity.
\Drupal\Core\Entity\EntityInterface $identity: An identity entity.
array $options: Optional options to pass to the channel. If the 'channels' key is specified, this will find a sub array with the key of the channel being transmitted to, and merge it into the base base array. The channels key will then be unset. e.g: Sending to the courier_email channel:
Return value
\Drupal\courier\MessageQueueItemInterface|false A message queue item entity, or FALSE if no messages could be generated.
Overrides CourierManagerInterface::sendMessage
File
- src/
Service/ CourierManager.php, line 93
Class
- CourierManager
- The courier manager.
Namespace
Drupal\courier\ServiceCode
public function sendMessage(TemplateCollectionInterface $template_collection, EntityInterface $identity, array $options = []) {
$template_collection
->validateTokenValues();
$message_queue = MessageQueueItem::create()
->setOptions($options)
->setIdentity($identity);
$t_args_base = [
'%identity' => $identity
->label(),
'@template_collection' => $template_collection
->id(),
];
// All templates are 'rendered' into messages in case preferred channels
// fail.
/** @var \Drupal\courier\ChannelInterface[] $templates */
$templates = [];
foreach ($this->identityChannelManager
->getChannelsForIdentity($identity) as $channel) {
if ($template = $template_collection
->getTemplate($channel)) {
$templates[$channel] = $template;
}
}
foreach ($templates as $channel => $template) {
$t_args = $t_args_base;
$t_args['@template'] = $template
->id();
$t_args['%channel'] = $channel;
$plugin = $this->identityChannelManager
->getCourierIdentity($channel, $identity
->getEntityTypeId());
if ($plugin) {
$message = $template
->createDuplicate();
if ($message
->id()) {
throw new \Exception(sprintf('Failed to clone `%s`', $channel));
}
try {
$plugin
->applyIdentity($message, $identity);
} catch (IdentityException $e) {
$this->logger
->notice('Identity %identity could not be applied to %channel: @message.', $t_args + [
'@message' => $e
->getMessage(),
]);
continue;
}
if ($message
->isEmpty()) {
$this->logger
->debug('Template @template (%channel) for collection @template_collection was empty.', $t_args);
continue;
}
foreach ($template_collection
->getTokenValues() as $token => $value) {
$message
->setTokenValue($token, $value);
}
foreach ($template_collection
->getTokenOptions() as $token_option => $value) {
$message
->setTokenOption($token_option, $value);
}
$message
->setTokenValue('identity', $identity)
->applyTokens();
$this->logger
->debug('Template @template (%channel) added to a message queue item.', $t_args);
if ($message
->save()) {
$message_queue
->addMessage($message);
}
}
}
if ($message_queue
->getMessages()) {
if ($this
->getSkipQueue()) {
$this->messageQueue
->sendMessage($message_queue);
}
else {
$message_queue
->save();
$queue = \Drupal::queue('courier_message');
$queue
->createItem([
'id' => $message_queue
->id(),
]);
}
return $message_queue;
}
$this->logger
->info('No messages could be sent to %identity. No messages were generated.', $t_args_base);
return FALSE;
}