Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Notificator.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
9 namespace Magento\User\Model;
10 
22 
29 {
33  private $transportBuilder;
34 
38  private $config;
39 
43  private $deployConfig;
44 
48  private $storeManager;
49 
56  public function __construct(
57  TransportBuilder $transportBuilder,
58  ConfigInterface $config,
59  DeploymentConfig $deployConfig,
60  StoreManagerInterface $storeManager
61  ) {
62  $this->transportBuilder = $transportBuilder;
63  $this->config = $config;
64  $this->deployConfig = $deployConfig;
65  $this->storeManager = $storeManager;
66  }
67 
79  private function sendNotification(
80  string $templateConfigId,
81  array $templateVars,
82  string $toEmail,
83  string $toName
84  ): void {
85  $transport = $this->transportBuilder
86  ->setTemplateIdentifier($this->config->getValue($templateConfigId))
87  ->setTemplateModel(BackendTemplate::class)
88  ->setTemplateOptions([
90  'store' => Store::DEFAULT_STORE_ID
91  ])
92  ->setTemplateVars($templateVars)
93  ->setFrom(
94  $this->config->getValue('admin/emails/forgot_email_identity')
95  )
96  ->addTo($toEmail, $toName)
97  ->getTransport();
98  $transport->sendMessage();
99  }
100 
104  public function sendForgotPassword(UserInterface $user): void
105  {
106  try {
107  $this->sendNotification(
108  'admin/emails/forgot_email_template',
109  [
110  'user' => $user,
111  'store' => $this->storeManager->getStore(
113  )
114  ],
115  $user->getEmail(),
116  $user->getFirstName().' '.$user->getLastName()
117  );
118  } catch (LocalizedException $exception) {
119  throw new NotificatorException(
120  __($exception->getMessage()),
121  $exception
122  );
123  }
124  }
125 
129  public function sendCreated(UserInterface $user): void
130  {
131  $toEmails = [];
132  $generalEmail = $this->config->getValue(
133  'trans_email/ident_general/email'
134  );
135  if ($generalEmail) {
136  $toEmails[] = $generalEmail;
137  }
138  if ($adminEmail = $this->deployConfig->get('user_admin_email')) {
139  $toEmails[] = $adminEmail;
140  }
141 
142  try {
143  foreach ($toEmails as $toEmail) {
144  $this->sendNotification(
145  'admin/emails/new_user_notification_template',
146  [
147  'user' => $user,
148  'store' => $this->storeManager->getStore(
150  )
151  ],
152  $toEmail,
153  __('Administrator')->getText()
154  );
155  }
156  } catch (LocalizedException $exception) {
157  throw new NotificatorException(
158  __($exception->getMessage()),
159  $exception
160  );
161  }
162  }
163 
167  public function sendUpdated(UserInterface $user, array $changed): void
168  {
169  $email = $user->getEmail();
170  if ($user instanceof User) {
171  $email = $user->getOrigData('email');
172  }
173 
174  try {
175  $this->sendNotification(
176  'admin/emails/user_notification_template',
177  [
178  'user' => $user,
179  'store' => $this->storeManager->getStore(
181  ),
182  'changes' => implode(', ', $changed)
183  ],
184  $email,
185  $user->getFirstName().' '.$user->getLastName()
186  );
187  } catch (LocalizedException $exception) {
188  throw new NotificatorException(
189  __($exception->getMessage()),
190  $exception
191  );
192  }
193  }
194 }
sendCreated(UserInterface $user)
$email
Definition: details.phtml:13
$config
Definition: fraud_order.php:17
sendUpdated(UserInterface $user, array $changed)
$storeManager
sendForgotPassword(UserInterface $user)
__()
Definition: __.php:13
$user
Definition: dummy_user.php:13
$deployConfig
__construct(TransportBuilder $transportBuilder, ConfigInterface $config, DeploymentConfig $deployConfig, StoreManagerInterface $storeManager)
Definition: Notificator.php:56