mirror of
https://github.com/flarum/core.git
synced 2025-10-28 14:06:30 +01:00
Move command classes to domain namespaces
They will probably be refactored away at a later stage (when we get rid of the command bus). Until then, this lets us remove the Flarum\Core namespace and actually feels quite clean.
This commit is contained in:
123
src/User/Command/RequestPasswordResetHandler.php
Normal file
123
src/User/Command/RequestPasswordResetHandler.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\User\Command;
|
||||
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\PasswordToken;
|
||||
use Flarum\User\UserRepository;
|
||||
use Illuminate\Contracts\Mail\Mailer;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Illuminate\Contracts\Validation\ValidationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Mail\Message;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class RequestPasswordResetHandler
|
||||
{
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var Mailer
|
||||
*/
|
||||
protected $mailer;
|
||||
|
||||
/**
|
||||
* @var UrlGenerator
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $validatorFactory;
|
||||
|
||||
/**
|
||||
* @param \Flarum\User\UserRepository $users
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param Mailer $mailer
|
||||
* @param UrlGenerator $url
|
||||
* @param TranslatorInterface $translator
|
||||
* @param Factory $validatorFactory
|
||||
*/
|
||||
public function __construct(
|
||||
UserRepository $users,
|
||||
SettingsRepositoryInterface $settings,
|
||||
Mailer $mailer,
|
||||
UrlGenerator $url,
|
||||
TranslatorInterface $translator,
|
||||
Factory $validatorFactory
|
||||
) {
|
||||
$this->users = $users;
|
||||
$this->settings = $settings;
|
||||
$this->mailer = $mailer;
|
||||
$this->url = $url;
|
||||
$this->translator = $translator;
|
||||
$this->validatorFactory = $validatorFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RequestPasswordReset $command
|
||||
* @return \Flarum\User\User
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function handle(RequestPasswordReset $command)
|
||||
{
|
||||
$email = $command->email;
|
||||
|
||||
$validation = $this->validatorFactory->make(
|
||||
compact('email'),
|
||||
['email' => 'required|email']
|
||||
);
|
||||
|
||||
if ($validation->fails()) {
|
||||
throw new ValidationException($validation);
|
||||
}
|
||||
|
||||
$user = $this->users->findByEmail($email);
|
||||
|
||||
if (! $user) {
|
||||
throw new ModelNotFoundException;
|
||||
}
|
||||
|
||||
$token = PasswordToken::generate($user->id);
|
||||
$token->save();
|
||||
|
||||
$data = [
|
||||
'{username}' => $user->username,
|
||||
'{url}' => $this->url->toRoute('resetPassword', ['token' => $token->id]),
|
||||
'{forum}' => $this->settings->get('forum_title'),
|
||||
];
|
||||
|
||||
$body = $this->translator->trans('core.email.reset_password.body', $data);
|
||||
|
||||
$this->mailer->raw($body, function (Message $message) use ($user, $data) {
|
||||
$message->to($user->email);
|
||||
$message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.reset_password.subject'));
|
||||
});
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user