Refactor AccountActivationMailer and SendConfirmationEmailController (#2493)

* Add AccountActivationMailerTrait and use in AccountActivationMailer and SendConfirmationEmailController
* Remove prefix

Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com>
This commit is contained in:
sl-kr 2021-03-19 13:06:41 -04:00 committed by GitHub
parent 2114af59a3
commit dd2712d5c8
3 changed files with 67 additions and 51 deletions

View File

@ -10,9 +10,8 @@
namespace Flarum\Api\Controller;
use Flarum\Http\UrlGenerator;
use Flarum\Mail\Job\SendRawEmailJob;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\EmailToken;
use Flarum\User\AccountActivationMailerTrait;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Support\Arr;
@ -24,6 +23,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class SendConfirmationEmailController implements RequestHandlerInterface
{
use AccountActivationMailerTrait;
/**
* @var SettingsRepositoryInterface
*/
@ -72,19 +73,10 @@ class SendConfirmationEmailController implements RequestHandlerInterface
throw new PermissionDeniedException;
}
$token = EmailToken::generate($actor->email, $actor->id);
$token->save();
$token = $this->generateToken($actor, $actor->email);
$data = $this->getEmailData($actor, $token);
$data = [
'{username}' => $actor->username,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
$body = $this->translator->trans('core.email.activate_account.body', $data);
$subject = $this->translator->trans('core.email.activate_account.subject');
$this->queue->push(new SendRawEmailJob($actor->email, $subject, $body));
$this->sendConfirmationEmail($actor, $data);
return new EmptyResponse;
}

View File

@ -10,7 +10,6 @@
namespace Flarum\User;
use Flarum\Http\UrlGenerator;
use Flarum\Mail\Job\SendRawEmailJob;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Event\Registered;
use Illuminate\Contracts\Queue\Queue;
@ -18,6 +17,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class AccountActivationMailer
{
use AccountActivationMailerTrait;
/**
* @var SettingsRepositoryInterface
*/
@ -60,42 +61,9 @@ class AccountActivationMailer
return;
}
$data = $this->getEmailData($user, $user->email);
$token = $this->generateToken($user, $user->email);
$data = $this->getEmailData($user, $token);
$body = $this->translator->trans('core.email.activate_account.body', $data);
$subject = '['.$data['{forum}'].'] '.$this->translator->trans('core.email.activate_account.subject');
$this->queue->push(new SendRawEmailJob($user->email, $subject, $body));
}
/**
* @param User $user
* @param string $email
* @return EmailToken
*/
protected function generateToken(User $user, $email)
{
$token = EmailToken::generate($email, $user->id);
$token->save();
return $token;
}
/**
* Get the data that should be made available to email templates.
*
* @param User $user
* @param string $email
* @return array
*/
protected function getEmailData(User $user, $email)
{
$token = $this->generateToken($user, $email);
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
$this->sendConfirmationEmail($user, $data);
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User;
use Flarum\Mail\Job\SendRawEmailJob;
trait AccountActivationMailerTrait
{
/**
* @param User $user
* @param string $email
* @return EmailToken
*/
protected function generateToken(User $user, $email)
{
$token = EmailToken::generate($email, $user->id);
$token->save();
return $token;
}
/**
* Get the data that should be made available to email templates.
*
* @param User $user
* @param EmailToken $token
* @return array
*/
protected function getEmailData(User $user, EmailToken $token)
{
return [
'{username}' => $user->display_name,
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->token]),
'{forum}' => $this->settings->get('forum_title')
];
}
/**
* @param User $user
* @param array $data
*/
protected function sendConfirmationEmail(User $user, $data)
{
$body = $this->translator->trans('core.email.activate_account.body', $data);
$subject = $this->translator->trans('core.email.activate_account.subject');
$this->queue->push(new SendRawEmailJob($user->email, $subject, $body));
}
}