From dd2712d5c846eee24614bdfc099ae7aeaa21b957 Mon Sep 17 00:00:00 2001 From: sl-kr <55163442+sl-kr@users.noreply.github.com> Date: Fri, 19 Mar 2021 13:06:41 -0400 Subject: [PATCH] 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> --- .../SendConfirmationEmailController.php | 20 ++----- .../core/src/User/AccountActivationMailer.php | 42 ++------------ .../src/User/AccountActivationMailerTrait.php | 56 +++++++++++++++++++ 3 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 framework/core/src/User/AccountActivationMailerTrait.php diff --git a/framework/core/src/Api/Controller/SendConfirmationEmailController.php b/framework/core/src/Api/Controller/SendConfirmationEmailController.php index ce228e680..9c8a499e8 100644 --- a/framework/core/src/Api/Controller/SendConfirmationEmailController.php +++ b/framework/core/src/Api/Controller/SendConfirmationEmailController.php @@ -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; } diff --git a/framework/core/src/User/AccountActivationMailer.php b/framework/core/src/User/AccountActivationMailer.php index 3d6d79b9c..958539143 100644 --- a/framework/core/src/User/AccountActivationMailer.php +++ b/framework/core/src/User/AccountActivationMailer.php @@ -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); } } diff --git a/framework/core/src/User/AccountActivationMailerTrait.php b/framework/core/src/User/AccountActivationMailerTrait.php new file mode 100644 index 000000000..0cb32371a --- /dev/null +++ b/framework/core/src/User/AccountActivationMailerTrait.php @@ -0,0 +1,56 @@ +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)); + } +}