OrderMailEventSubscriber.php in Commerce Core 8.2
File
modules/log/src/EventSubscriber/OrderMailEventSubscriber.php
View source
<?php
namespace Drupal\commerce_log\EventSubscriber;
use Drupal\commerce\Event\CommerceEvents;
use Drupal\commerce\Event\PostMailSendEvent;
use Drupal\commerce_log\LogTemplateManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderMailEventSubscriber implements EventSubscriberInterface {
protected $entityTypeManager;
protected $logTemplateManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LogTemplateManagerInterface $log_template_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->logTemplateManager = $log_template_manager;
}
public static function getSubscribedEvents() {
return [
CommerceEvents::POST_MAIL_SEND => [
'onMailSend',
],
];
}
public function onMailSend(PostMailSendEvent $event) {
$params = $event
->getParams();
if (!isset($params['order'])) {
return;
}
$log_storage = $this->entityTypeManager
->getStorage('commerce_log');
$message = $event
->getMessage();
$result = (bool) $message['result'];
$definitions = $this->logTemplateManager
->getDefinitions();
$template_id = $result ? 'mail_' . $params['id'] : 'mail_' . $params['id'] . '_failure';
if (!isset($definitions[$template_id])) {
$template_id = $result ? 'order_mail' : 'order_mail_failure';
}
$log_params = [
'id' => $params['id'],
'to_email' => $event
->getMessage()['to'],
];
$log_storage
->generate($params['order'], $template_id, $log_params)
->save();
}
}