Notificator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\User\Model;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\Exception\MailException;
  10. use Magento\Store\Model\Store;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\User\Api\Data\UserInterface;
  13. use Magento\User\Model\Spi\NotificatorInterface;
  14. use Magento\Backend\App\ConfigInterface;
  15. use Magento\Framework\Mail\Template\TransportBuilder;
  16. use Magento\Framework\App\DeploymentConfig;
  17. use Magento\Backend\App\Area\FrontNameResolver;
  18. use Magento\Email\Model\BackendTemplate;
  19. /**
  20. * @inheritDoc
  21. *
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class Notificator implements NotificatorInterface
  25. {
  26. /**
  27. * @var TransportBuilder
  28. */
  29. private $transportBuilder;
  30. /**
  31. * @var ConfigInterface
  32. */
  33. private $config;
  34. /**
  35. * @var DeploymentConfig
  36. */
  37. private $deployConfig;
  38. /**
  39. * @var StoreManagerInterface
  40. */
  41. private $storeManager;
  42. /**
  43. * @param TransportBuilder $transportBuilder
  44. * @param ConfigInterface $config
  45. * @param DeploymentConfig $deployConfig
  46. * @param StoreManagerInterface $storeManager
  47. */
  48. public function __construct(
  49. TransportBuilder $transportBuilder,
  50. ConfigInterface $config,
  51. DeploymentConfig $deployConfig,
  52. StoreManagerInterface $storeManager
  53. ) {
  54. $this->transportBuilder = $transportBuilder;
  55. $this->config = $config;
  56. $this->deployConfig = $deployConfig;
  57. $this->storeManager = $storeManager;
  58. }
  59. /**
  60. * Send a notification.
  61. *
  62. * @param string $templateConfigId
  63. * @param array $templateVars
  64. * @param string $toEmail
  65. * @param string $toName
  66. * @throws MailException
  67. *
  68. * @return void
  69. */
  70. private function sendNotification(
  71. string $templateConfigId,
  72. array $templateVars,
  73. string $toEmail,
  74. string $toName
  75. ): void {
  76. $transport = $this->transportBuilder
  77. ->setTemplateIdentifier($this->config->getValue($templateConfigId))
  78. ->setTemplateModel(BackendTemplate::class)
  79. ->setTemplateOptions([
  80. 'area' => FrontNameResolver::AREA_CODE,
  81. 'store' => Store::DEFAULT_STORE_ID
  82. ])
  83. ->setTemplateVars($templateVars)
  84. ->setFrom(
  85. $this->config->getValue('admin/emails/forgot_email_identity')
  86. )
  87. ->addTo($toEmail, $toName)
  88. ->getTransport();
  89. $transport->sendMessage();
  90. }
  91. /**
  92. * @inheritDoc
  93. */
  94. public function sendForgotPassword(UserInterface $user): void
  95. {
  96. try {
  97. $this->sendNotification(
  98. 'admin/emails/forgot_email_template',
  99. [
  100. 'user' => $user,
  101. 'store' => $this->storeManager->getStore(
  102. Store::DEFAULT_STORE_ID
  103. )
  104. ],
  105. $user->getEmail(),
  106. $user->getFirstName().' '.$user->getLastName()
  107. );
  108. } catch (LocalizedException $exception) {
  109. throw new NotificatorException(
  110. __($exception->getMessage()),
  111. $exception
  112. );
  113. }
  114. }
  115. /**
  116. * @inheritDoc
  117. */
  118. public function sendCreated(UserInterface $user): void
  119. {
  120. $toEmails = [];
  121. $generalEmail = $this->config->getValue(
  122. 'trans_email/ident_general/email'
  123. );
  124. if ($generalEmail) {
  125. $toEmails[] = $generalEmail;
  126. }
  127. if ($adminEmail = $this->deployConfig->get('user_admin_email')) {
  128. $toEmails[] = $adminEmail;
  129. }
  130. try {
  131. foreach ($toEmails as $toEmail) {
  132. $this->sendNotification(
  133. 'admin/emails/new_user_notification_template',
  134. [
  135. 'user' => $user,
  136. 'store' => $this->storeManager->getStore(
  137. Store::DEFAULT_STORE_ID
  138. )
  139. ],
  140. $toEmail,
  141. __('Administrator')->getText()
  142. );
  143. }
  144. } catch (LocalizedException $exception) {
  145. throw new NotificatorException(
  146. __($exception->getMessage()),
  147. $exception
  148. );
  149. }
  150. }
  151. /**
  152. * @inheritDoc
  153. */
  154. public function sendUpdated(UserInterface $user, array $changed): void
  155. {
  156. $email = $user->getEmail();
  157. if ($user instanceof User) {
  158. $email = $user->getOrigData('email');
  159. }
  160. try {
  161. $this->sendNotification(
  162. 'admin/emails/user_notification_template',
  163. [
  164. 'user' => $user,
  165. 'store' => $this->storeManager->getStore(
  166. Store::DEFAULT_STORE_ID
  167. ),
  168. 'changes' => implode(', ', $changed)
  169. ],
  170. $email,
  171. $user->getFirstName().' '.$user->getLastName()
  172. );
  173. } catch (LocalizedException $exception) {
  174. throw new NotificatorException(
  175. __($exception->getMessage()),
  176. $exception
  177. );
  178. }
  179. }
  180. }