Skip to content

Commit

Permalink
Refactor AccountActivationMailer and SendConfirmationEmailController (#…
Browse files Browse the repository at this point in the history
…2493)

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

Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com>
  • Loading branch information
slkrx and askvortsov1 authored Mar 19, 2021
1 parent fe8dda6 commit 374189d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
20 changes: 6 additions & 14 deletions src/Api/Controller/SendConfirmationEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +23,8 @@

class SendConfirmationEmailController implements RequestHandlerInterface
{
use AccountActivationMailerTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand Down Expand Up @@ -72,19 +73,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
throw new PermissionDeniedException;
}

$token = EmailToken::generate($actor->email, $actor->id);
$token->save();

$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');
$token = $this->generateToken($actor, $actor->email);
$data = $this->getEmailData($actor, $token);

$this->queue->push(new SendRawEmailJob($actor->email, $subject, $body));
$this->sendConfirmationEmail($actor, $data);

return new EmptyResponse;
}
Expand Down
42 changes: 5 additions & 37 deletions src/User/AccountActivationMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
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;
use Symfony\Contracts\Translation\TranslatorInterface;

class AccountActivationMailer
{
use AccountActivationMailerTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand Down Expand Up @@ -60,42 +61,9 @@ public function handle(Registered $event)
return;
}

$data = $this->getEmailData($user, $user->email);

$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);
$token = $this->generateToken($user, $user->email);
$data = $this->getEmailData($user, $token);

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);
}
}
56 changes: 56 additions & 0 deletions src/User/AccountActivationMailerTrait.php
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 374189d

Please sign in to comment.