CourierManager.php in Courier 2.x
File
src/Service/CourierManager.php
View source
<?php
namespace Drupal\courier\Service;
use Drupal\courier\TemplateCollectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\courier\Entity\MessageQueueItem;
use Drupal\courier\Exception\IdentityException;
class CourierManager implements CourierManagerInterface {
protected $entityTypeManager;
protected $configFactory;
protected $logger;
protected $identityChannelManager;
protected $messageQueue;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, IdentityChannelManagerInterface $identity_channel_manager, MessageQueueManagerInterface $message_queue) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->logger = $logger_factory
->get('courier');
$this->identityChannelManager = $identity_channel_manager;
$this->messageQueue = $message_queue;
}
public function addTemplates(TemplateCollectionInterface &$template_collection) {
foreach (array_keys($this->identityChannelManager
->getChannels()) as $entity_type_id) {
if (!$template_collection
->getTemplate($entity_type_id)) {
$template = $this->entityTypeManager
->getStorage($entity_type_id)
->create();
$template_collection
->setTemplate($template);
}
}
}
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(),
];
$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;
}
public function getSkipQueue() : bool {
$skip_queue = $this->configFactory
->get('courier.settings')
->get('skip_queue');
return !empty($skip_queue);
}
}