You are here

public function UserManagementService::notifyUser in Auto Purge Users 8.3

Sends a user notification before cancel and anonymize it.

Parameters

\Drupal\user\UserInterface $user: The user to block.

Overrides UserManagementServiceInterface::notifyUser

File

src/Services/UserManagementService.php, line 143

Class

UserManagementService
Class that holds the purging logic.

Namespace

Drupal\purge_users\Services

Code

public function notifyUser(UserInterface $user) : void {
  $notifier = $this->loggerFactory
    ->get('notification_users');
  $config = $this->config
    ->get('purge_users.settings');
  $send_email_user_before_notification = $config
    ->get('send_email_user_before_notification');
  $send_notification = $config
    ->get('send_email_notification');
  $user_ids = purge_users_get_user_ids('purge_users');

  /*
   * Checks if notification before deletion is enabled,
   * AND if send email notification is enabled AND
   * the user was purged OR the current user
   * is not in the users purged we are not sending
   * an email to avoid spamming.
   */
  if ($send_email_user_before_notification == 1 && ($send_notification !== 1 && in_array($user
    ->id(), $user_ids)) || !in_array($user
    ->id(), $user_ids)) {
    purge_users_send_notification_email($user, 'notification_users');
    $this->messenger
      ->addStatus(t('%name has been notified.', [
      '%name' => $user
        ->getDisplayName(),
    ]));
    $notifier
      ->notice('Notified user: %name %email.', [
      '%name' => $user
        ->getAccountName(),
      '%email' => '<' . $user
        ->getEmail() . '>',
    ]);
  }
}