dev-think-one /
laravel-email-change-verification
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EmailChangeVerification\Broker; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use EmailChangeVerification\Token\TokenRepositoryInterface; |
||
| 7 | use EmailChangeVerification\User\HasEmailChangeVerification as HasEmailChangeVerificationContract; |
||
| 8 | use Illuminate\Contracts\Auth\UserProvider; |
||
| 9 | use Illuminate\Support\Arr; |
||
| 10 | use UnexpectedValueException; |
||
| 11 | |||
| 12 | class Broker implements BrokerInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The email change token repository. |
||
| 16 | */ |
||
| 17 | protected TokenRepositoryInterface $tokens; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The user provider implementation. |
||
| 21 | */ |
||
| 22 | protected UserProvider $users; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new email change broker instance. |
||
| 26 | * |
||
| 27 | * @param TokenRepositoryInterface $tokens |
||
| 28 | * @param \Illuminate\Contracts\Auth\UserProvider $users |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | 20 | public function __construct(TokenRepositoryInterface $tokens, UserProvider $users) |
|
| 32 | { |
||
| 33 | 20 | $this->users = $users; |
|
| 34 | 20 | $this->tokens = $tokens; |
|
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Send a verification link to a user. |
||
| 39 | * |
||
| 40 | * @param array $credentials |
||
| 41 | * @param string $newEmail |
||
| 42 | * @param Closure|null $callback |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | 12 | public function sendVerificationLink(array $credentials, string $newEmail, Closure $callback = null) |
|
| 47 | { |
||
| 48 | // First we will check to see if we found a user at the given credentials and |
||
| 49 | // if we did not we will redirect back to this current URI with a piece of |
||
| 50 | // "flash" data in the session to indicate to the developers the errors. |
||
| 51 | 12 | $user = $this->getUser($credentials); |
|
|
0 ignored issues
–
show
|
|||
| 52 | |||
| 53 | 11 | if (is_null($user)) { |
|
|
0 ignored issues
–
show
|
|||
| 54 | 1 | return static::INVALID_USER; |
|
| 55 | } |
||
| 56 | |||
| 57 | 10 | if ($this->tokens->recentlyCreatedToken($user)) { |
|
| 58 | 1 | return static::EMAIL_THROTTLED; |
|
| 59 | } |
||
| 60 | |||
| 61 | 10 | $token = $this->tokens->create($user, $newEmail); |
|
| 62 | |||
| 63 | 10 | if ($callback) { |
|
| 64 | 4 | $callback($user, $token, $newEmail); |
|
| 65 | } else { |
||
| 66 | // Once we have the change token, we are ready to send the message out to this |
||
| 67 | // user with a link to change their email. We will then redirect back to |
||
| 68 | // the current URI having nothing set in the session to indicate errors. |
||
| 69 | 6 | $user->sendEmailChangeNotification($token, $newEmail); |
|
| 70 | } |
||
| 71 | |||
| 72 | 10 | return static::VERIFICATION_LINK_SENT; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Verify new email for the given token. |
||
| 77 | * |
||
| 78 | * @param array $credentials |
||
| 79 | * @param \Closure $callback |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | 3 | public function verify(array $credentials, Closure $callback) |
|
| 83 | { |
||
| 84 | 3 | $user = $this->validateChanges($credentials); |
|
| 85 | |||
| 86 | // If the responses from the validate method is not a user instance, we will |
||
| 87 | // assume that it is a redirect and simply return it from this method and |
||
| 88 | // the user is properly redirected having an error message on the post. |
||
| 89 | 3 | if (!$user instanceof HasEmailChangeVerificationContract) { |
|
|
0 ignored issues
–
show
|
|||
| 90 | 2 | return $user; |
|
| 91 | } |
||
| 92 | |||
| 93 | 1 | $newEmail = $credentials['new_email']; |
|
| 94 | |||
| 95 | // Once the change has been validated, we'll call the given callback with the |
||
| 96 | // new password. This gives the user an opportunity to store the password |
||
| 97 | // in their persistent storage. Then we'll delete the token and return. |
||
| 98 | 1 | $callback($user, $newEmail); |
|
| 99 | |||
| 100 | 1 | $this->tokens->delete($user); |
|
| 101 | |||
| 102 | 1 | return static::EMAIL_CHANGED; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Validate a email change for the given credentials. |
||
| 107 | * |
||
| 108 | * @param array $credentials |
||
| 109 | * @return HasEmailChangeVerificationContract|string |
||
| 110 | */ |
||
| 111 | 3 | protected function validateChanges(array $credentials) |
|
| 112 | { |
||
| 113 | 3 | if (is_null($user = $this->getUser($credentials))) { |
|
|
0 ignored issues
–
show
Are you sure the assignment to
$user is correct as $this->getUser($credentials) targeting EmailChangeVerification\Broker\Broker::getUser() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 114 | 1 | return static::INVALID_USER; |
|
| 115 | } |
||
| 116 | |||
| 117 | 2 | if (!$this->tokens->exists($user, $credentials['token'], $credentials['new_email'])) { |
|
| 118 | 1 | return static::INVALID_TOKEN; |
|
| 119 | } |
||
| 120 | |||
| 121 | 1 | return $user; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the user for the given credentials. |
||
| 126 | * |
||
| 127 | * @param array $credentials |
||
| 128 | * @return HasEmailChangeVerificationContract|null |
||
| 129 | * |
||
| 130 | * @throws \UnexpectedValueException |
||
| 131 | */ |
||
| 132 | 12 | public function getUser(array $credentials) |
|
| 133 | { |
||
| 134 | 12 | $credentials = Arr::except($credentials, ['token', 'new_email']); |
|
| 135 | |||
| 136 | 12 | $user = $this->users->retrieveByCredentials($credentials); |
|
| 137 | |||
| 138 | 12 | if ($user && !($user instanceof HasEmailChangeVerificationContract)) { |
|
| 139 | 1 | throw new UnexpectedValueException('User must implement HasEmailChangeVerificationContract interface.'); |
|
| 140 | } |
||
| 141 | |||
| 142 | 11 | return $user; |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Create a new email change token for the given user. |
||
| 147 | * |
||
| 148 | * @param HasEmailChangeVerificationContract $user |
||
| 149 | * @param string $newEmail |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 4 | public function createToken(HasEmailChangeVerificationContract $user, string $newEmail) |
|
| 154 | { |
||
| 155 | 4 | return $this->tokens->create($user, $newEmail); |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Delete password change tokens of the given user. |
||
| 160 | * |
||
| 161 | * @param HasEmailChangeVerificationContract $user |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | 1 | public function deleteToken(HasEmailChangeVerificationContract $user) |
|
| 165 | { |
||
| 166 | 1 | $this->tokens->delete($user); |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Validate the given password cjamhe token. |
||
| 171 | * |
||
| 172 | * @param HasEmailChangeVerificationContract $user |
||
| 173 | * @param string $token |
||
| 174 | * @param string $newEmail |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 3 | public function tokenExists(HasEmailChangeVerificationContract $user, string $token, string $newEmail) |
|
| 179 | { |
||
| 180 | 3 | return $this->tokens->exists($user, $token, $newEmail); |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the password change token repository implementation. |
||
| 185 | * |
||
| 186 | * @return TokenRepositoryInterface |
||
| 187 | */ |
||
| 188 | 6 | public function getRepository() |
|
| 189 | { |
||
| 190 | 6 | return $this->tokens; |
|
| 191 | } |
||
| 192 | } |
||
| 193 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.