MessageQueueManager.php in Courier 2.x
File
src/Service/MessageQueueManager.php
View source
<?php
namespace Drupal\courier\Service;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\courier\MessageQueueItemInterface;
class MessageQueueManager implements MessageQueueManagerInterface {
protected $logger;
protected $identityChannelManager;
public function __construct(LoggerChannelFactoryInterface $logger_factory, IdentityChannelManagerInterface $identity_channel_manager) {
$this->logger = $logger_factory
->get('courier');
$this->identityChannelManager = $identity_channel_manager;
}
public function sendMessage(MessageQueueItemInterface $mqi) {
$options = $mqi
->getOptions();
$channel_options = array_key_exists('channels', $options) ? $options['channels'] : [];
unset($options['channels']);
$channels = $this->identityChannelManager
->getChannelsForIdentity($mqi
->getIdentity());
$messages = [];
foreach ($channels as $channel) {
if ($message = $mqi
->getMessage($channel)) {
$messages[] = $message;
}
}
foreach ($messages as $message) {
$message_options = $options;
$channel = $message
->getEntityTypeId();
if (array_key_exists($channel, $channel_options)) {
$message_options = array_merge($message_options, $channel_options[$channel]);
}
$t_args = [
'@channel' => $channel,
'@identity' => $mqi
->getIdentity()
->label(),
];
try {
$message::sendMessages([
$message,
], $message_options);
$this->logger
->info('Successfully sent @channel to @identity', $t_args);
$mqi
->delete();
return $message;
} catch (\Exception $e) {
$t_args['@exception'] = $e
->getMessage();
$this->logger
->warning('Failed to send @channel to @identity: @exception', $t_args);
continue;
}
break;
}
return FALSE;
}
}