Skip to content

Commit

Permalink
refactor password broker
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexzPurewoko committed Jul 18, 2024
1 parent 26098e3 commit 8f60758
Showing 1 changed file with 54 additions and 52 deletions.
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

0 comments on commit 8f60758

Please sign in to comment.