Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor swiftmailer to symfony mailer #10

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"symfony/finder": "~6.4",
"symfony/http-foundation": "~6.4",
"symfony/http-kernel": "~6.4",
"symfony/mailer": "^6.4",
"symfony/mime": "~6.4",
"symfony/process": "~6.4",
"symfony/routing": "~6.4",
Expand Down
106 changes: 54 additions & 52 deletions src/Illuminate/Auth/Reminders/PasswordBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,49 @@ class PasswordBroker {
*
* @var \Illuminate\Auth\Reminders\ReminderRepositoryInterface $reminders
*/
protected $reminders;
protected ReminderRepositoryInterface $reminders;

/**
* The user provider implementation.
*
* @var \Illuminate\Auth\UserProviderInterface
*/
protected $users;
protected UserProviderInterface $users;

/**
* The mailer instance.
*
* @var \Illuminate\Mail\Mailer
*/
protected $mailer;
protected Mailer $mailer;

/**
* The view of the password reminder e-mail.
*
* @var string
*/
protected $reminderView;
protected string $reminderView;

/**
* The custom password validator callback.
*
* @var \Closure
*/
protected $passwordValidator;
protected Closure $passwordValidator;

/**
* Create a new password broker instance.
*
* @param \Illuminate\Auth\Reminders\ReminderRepositoryInterface $reminders
* @param \Illuminate\Auth\UserProviderInterface $users
* @param \Illuminate\Mail\Mailer $mailer
* @param string $reminderView
* @param string $reminderView
* @return void
*/
public function __construct(ReminderRepositoryInterface $reminders,
UserProviderInterface $users,
Mailer $mailer,
$reminderView)
UserProviderInterface $users,
Mailer $mailer,
string $reminderView)
{
$this->users = $users;
$this->mailer = $mailer;
Expand All @@ -103,14 +103,14 @@ public function __construct(ReminderRepositoryInterface $reminders,
* @param \Closure $callback
* @return string
*/
public function remind(array $credentials, Closure $callback = null)
public function remind(array $credentials, Closure $callback = null): string
{
// First we will check to see if we found a user at the given credentials and
// if we did not we will redirect back to this current URI with a piece of
// "flash" data in the session to indicate to the developers the errors.
$user = $this->getUser($credentials);

if (is_null($user))
if ($user === null)
{
return self::INVALID_USER;
}
Expand All @@ -129,22 +129,24 @@ public function remind(array $credentials, Closure $callback = null)
* Send the password reminder e-mail.
*
* @param \Illuminate\Auth\Reminders\RemindableInterface $user
* @param string $token
* @param \Closure $callback
* @return int
* @param string $token
* @param Closure $callback
* @return void
*/
public function sendReminder(RemindableInterface $user, $token, Closure $callback = null)
public function sendReminder(RemindableInterface $user, string $token, Closure $callback = null): void
{
// We will use the reminder view that was given to the broker to display the
// password reminder e-mail. We'll pass a "token" variable into the views
// so that it may be displayed for an user to click for password reset.
$view = $this->reminderView;

return $this->mailer->send($view, compact('token', 'user'), function($m) use ($user, $token, $callback)
$this->mailer->send($view, compact('token', 'user'), function($m) use ($user, $token, $callback)
{
$m->to($user->getReminderEmail());

if ( ! is_null($callback)) call_user_func($callback, $m, $user, $token);
if ($callback !== null) {
call_user_func($callback, $m, $user, $token);
}
});
}

Expand All @@ -153,10 +155,10 @@ public function sendReminder(RemindableInterface $user, $token, Closure $callbac
*
* @param array $credentials
* @param \Closure $callback
* @return mixed
*/
public function reset(array $credentials, Closure $callback)
{
* @return RemindableInterface|string|int
*/
public function reset(array $credentials, Closure $callback): RemindableInterface|string|int
{
// If the responses from the validate method is not a user instance, we will
// assume that it is a redirect and simply return it from this method and
// the user is properly redirected having an error message on the post.
Expand All @@ -179,25 +181,26 @@ public function reset(array $credentials, Closure $callback)
return self::PASSWORD_RESET;
}

/**
* Validate a password reset for the given credentials.
*
* @param array $credentials
* @return \Illuminate\Auth\Reminders\RemindableInterface
*/
protected function validateReset(array $credentials)
{
if (is_null($user = $this->getUser($credentials)))
/**
* Validate a password reset for the given credentials.
*
* @param array $credentials
* @return RemindableInterface|int|string
*/
protected function validateReset(array $credentials): RemindableInterface|int|string
{
$user = $this->getUser($credentials);
if ($user === null)
{
return self::INVALID_USER;
}

if ( ! $this->validNewPasswords($credentials))
if (!$this->validNewPasswords($credentials))
{
return self::INVALID_PASSWORD;
}

if ( ! $this->reminders->exists($user, $credentials['token']))
if (!$this->reminders->exists($user, $credentials['token']))
{
return self::INVALID_TOKEN;
}
Expand All @@ -211,8 +214,8 @@ protected function validateReset(array $credentials)
* @param \Closure $callback
* @return void
*/
public function validator(Closure $callback)
{
public function validator(Closure $callback): void
{
$this->passwordValidator = $callback;
}

Expand All @@ -222,9 +225,9 @@ public function validator(Closure $callback)
* @param array $credentials
* @return bool
*/
protected function validNewPasswords(array $credentials)
{
list($password, $confirm) = array($credentials['password'], $credentials['password_confirmation']);
protected function validNewPasswords(array $credentials): bool
{
list($password, $confirm) = [$credentials['password'], $credentials['password_confirmation']];

if (isset($this->passwordValidator))
{
Expand All @@ -240,24 +243,23 @@ protected function validNewPasswords(array $credentials)
* @param array $credentials
* @return bool
*/
protected function validatePasswordWithDefaults(array $credentials)
{
protected function validatePasswordWithDefaults(array $credentials): bool
{
list($password, $confirm) = [$credentials['password'], $credentials['password_confirmation']];

return $password === $confirm && mb_strlen((string) $password) >= 6;
}

/**
* Get the user for the given credentials.
*
* @param array $credentials
* @return \Illuminate\Auth\Reminders\RemindableInterface
*
* @throws \UnexpectedValueException
*/
public function getUser(array $credentials)
{
$credentials = array_except($credentials, array('token'));
/**
* Get the user for the given credentials.
*
* @param array $credentials
* @return RemindableInterface|null
*
*/
public function getUser(array $credentials): ?RemindableInterface
{
$credentials = array_except($credentials, ['token']);

$user = $this->users->retrieveByCredentials($credentials);

Expand All @@ -274,8 +276,8 @@ public function getUser(array $credentials)
*
* @return \Illuminate\Auth\Reminders\ReminderRepositoryInterface
*/
protected function getRepository()
{
protected function getRepository(): ReminderRepositoryInterface
{
return $this->reminders;
}

Expand Down
Loading
Loading