Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Mail / MailManagerWrapper.php
1 <?php
2
3 namespace Drupal\webprofiler\Mail;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
9 use Drupal\Core\Mail\MailManagerInterface;
10 use Drupal\Core\Plugin\DefaultPluginManager;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\Core\StringTranslation\TranslationInterface;
13 use Drupal\webprofiler\DataCollector\MailDataCollector;
14
15 /**
16  * Class MailManagerWrapper
17  */
18 class MailManagerWrapper extends DefaultPluginManager implements MailManagerInterface {
19
20   use StringTranslationTrait;
21
22   /**
23    * @var \Drupal\webprofiler\DataCollector\MailDataCollector
24    */
25   private $dataCollector;
26
27   /**
28    * @var \Drupal\Core\Mail\MailManagerInterface
29    */
30   private $mailManager;
31
32   /**
33    * The config factory.
34    *
35    * @var \Drupal\Core\Config\ConfigFactoryInterface
36    */
37   protected $configFactory;
38
39   /**
40    * The logger factory.
41    *
42    * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
43    */
44   protected $loggerFactory;
45
46   /**
47    * List of already instantiated mail plugins.
48    *
49    * @var array
50    */
51   protected $instances = array();
52
53   /**
54    * Constructs the MailManager object.
55    *
56    * @param \Traversable $namespaces
57    *   An object that implements \Traversable which contains the root paths
58    *   keyed by the corresponding namespace to look for plugin implementations.
59    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
60    *   Cache backend instance to use.
61    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
62    *   The module handler to invoke the alter hook with.
63    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
64    *   The configuration factory.
65    * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
66    *   The logger channel factory.
67    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
68    *   The string translation service.
69    * @param \Drupal\Core\Mail\MailManagerInterface $mailManager
70    * @param \Drupal\webprofiler\DataCollector\MailDataCollector $dataCollector
71    */
72   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, MailManagerInterface $mailManager, MailDataCollector $dataCollector) {
73     parent::__construct('Plugin/Mail', $namespaces, $module_handler, 'Drupal\Core\Mail\MailInterface', 'Drupal\Core\Annotation\Mail');
74     $this->alterInfo('mail_backend_info');
75     $this->setCacheBackend($cache_backend, 'mail_backend_plugins');
76     $this->configFactory = $config_factory;
77     $this->loggerFactory = $logger_factory;
78     $this->stringTranslation = $string_translation;
79     $this->dataCollector = $dataCollector;
80     $this->mailManager = $mailManager;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE) {
87     $message = $this->mailManager->mail($module, $key, $to, $langcode, $params, $reply, $send);
88
89     $instance = $this->mailManager->getInstance(['module' => $module, 'key' => $key]);
90     $this->dataCollector->addMessage($message, $instance);
91
92     return $message;
93   }
94 }