Skip to content

Commit 2dc9dc0

Browse files
authored
Merge pull request #10 from dicoding-dev/backport/migrate-swiftmailer-to-symfony-mailer
Refactor swiftmailer to symfony mailer
2 parents 88f0d42 + f8645d6 commit 2dc9dc0

File tree

15 files changed

+906
-1011
lines changed

15 files changed

+906
-1011
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"pda/pheanstalk": "~4.0",
2222
"phpseclib/phpseclib": "~2.0",
2323
"predis/predis": "^1.1",
24-
"swiftmailer/swiftmailer": "^6.0",
2524
"symfony/browser-kit": "~6.4",
2625
"symfony/console": "~6.4",
2726
"symfony/css-selector": "~6.4",
@@ -31,6 +30,7 @@
3130
"symfony/finder": "~6.4",
3231
"symfony/http-foundation": "~6.4",
3332
"symfony/http-kernel": "~6.4",
33+
"symfony/mailer": "^6.4",
3434
"symfony/mime": "~6.4",
3535
"symfony/process": "~6.4",
3636
"symfony/routing": "~6.4",

src/Illuminate/Auth/Reminders/PasswordBroker.php

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -46,49 +46,49 @@ class PasswordBroker {
4646
*
4747
* @var \Illuminate\Auth\Reminders\ReminderRepositoryInterface $reminders
4848
*/
49-
protected $reminders;
49+
protected ReminderRepositoryInterface $reminders;
5050

5151
/**
5252
* The user provider implementation.
5353
*
5454
* @var \Illuminate\Auth\UserProviderInterface
5555
*/
56-
protected $users;
56+
protected UserProviderInterface $users;
5757

5858
/**
5959
* The mailer instance.
6060
*
6161
* @var \Illuminate\Mail\Mailer
6262
*/
63-
protected $mailer;
63+
protected Mailer $mailer;
6464

6565
/**
6666
* The view of the password reminder e-mail.
6767
*
6868
* @var string
6969
*/
70-
protected $reminderView;
70+
protected string $reminderView;
7171

7272
/**
7373
* The custom password validator callback.
7474
*
7575
* @var \Closure
7676
*/
77-
protected $passwordValidator;
77+
protected Closure $passwordValidator;
7878

7979
/**
8080
* Create a new password broker instance.
8181
*
8282
* @param \Illuminate\Auth\Reminders\ReminderRepositoryInterface $reminders
8383
* @param \Illuminate\Auth\UserProviderInterface $users
8484
* @param \Illuminate\Mail\Mailer $mailer
85-
* @param string $reminderView
85+
* @param string $reminderView
8686
* @return void
8787
*/
8888
public function __construct(ReminderRepositoryInterface $reminders,
89-
UserProviderInterface $users,
90-
Mailer $mailer,
91-
$reminderView)
89+
UserProviderInterface $users,
90+
Mailer $mailer,
91+
string $reminderView)
9292
{
9393
$this->users = $users;
9494
$this->mailer = $mailer;
@@ -103,14 +103,14 @@ public function __construct(ReminderRepositoryInterface $reminders,
103103
* @param \Closure $callback
104104
* @return string
105105
*/
106-
public function remind(array $credentials, Closure $callback = null)
106+
public function remind(array $credentials, Closure $callback = null): string
107107
{
108108
// First we will check to see if we found a user at the given credentials and
109109
// if we did not we will redirect back to this current URI with a piece of
110110
// "flash" data in the session to indicate to the developers the errors.
111111
$user = $this->getUser($credentials);
112112

113-
if (is_null($user))
113+
if ($user === null)
114114
{
115115
return self::INVALID_USER;
116116
}
@@ -129,22 +129,24 @@ public function remind(array $credentials, Closure $callback = null)
129129
* Send the password reminder e-mail.
130130
*
131131
* @param \Illuminate\Auth\Reminders\RemindableInterface $user
132-
* @param string $token
133-
* @param \Closure $callback
134-
* @return int
132+
* @param string $token
133+
* @param Closure $callback
134+
* @return void
135135
*/
136-
public function sendReminder(RemindableInterface $user, $token, Closure $callback = null)
136+
public function sendReminder(RemindableInterface $user, string $token, Closure $callback = null): void
137137
{
138138
// We will use the reminder view that was given to the broker to display the
139139
// password reminder e-mail. We'll pass a "token" variable into the views
140140
// so that it may be displayed for an user to click for password reset.
141141
$view = $this->reminderView;
142142

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

147-
if ( ! is_null($callback)) call_user_func($callback, $m, $user, $token);
147+
if ($callback !== null) {
148+
call_user_func($callback, $m, $user, $token);
149+
}
148150
});
149151
}
150152

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

182-
/**
183-
* Validate a password reset for the given credentials.
184-
*
185-
* @param array $credentials
186-
* @return \Illuminate\Auth\Reminders\RemindableInterface
187-
*/
188-
protected function validateReset(array $credentials)
189-
{
190-
if (is_null($user = $this->getUser($credentials)))
184+
/**
185+
* Validate a password reset for the given credentials.
186+
*
187+
* @param array $credentials
188+
* @return RemindableInterface|int|string
189+
*/
190+
protected function validateReset(array $credentials): RemindableInterface|int|string
191+
{
192+
$user = $this->getUser($credentials);
193+
if ($user === null)
191194
{
192195
return self::INVALID_USER;
193196
}
194197

195-
if ( ! $this->validNewPasswords($credentials))
198+
if (!$this->validNewPasswords($credentials))
196199
{
197200
return self::INVALID_PASSWORD;
198201
}
199202

200-
if ( ! $this->reminders->exists($user, $credentials['token']))
203+
if (!$this->reminders->exists($user, $credentials['token']))
201204
{
202205
return self::INVALID_TOKEN;
203206
}
@@ -211,8 +214,8 @@ protected function validateReset(array $credentials)
211214
* @param \Closure $callback
212215
* @return void
213216
*/
214-
public function validator(Closure $callback)
215-
{
217+
public function validator(Closure $callback): void
218+
{
216219
$this->passwordValidator = $callback;
217220
}
218221

@@ -222,9 +225,9 @@ public function validator(Closure $callback)
222225
* @param array $credentials
223226
* @return bool
224227
*/
225-
protected function validNewPasswords(array $credentials)
226-
{
227-
list($password, $confirm) = array($credentials['password'], $credentials['password_confirmation']);
228+
protected function validNewPasswords(array $credentials): bool
229+
{
230+
list($password, $confirm) = [$credentials['password'], $credentials['password_confirmation']];
228231

229232
if (isset($this->passwordValidator))
230233
{
@@ -240,24 +243,23 @@ protected function validNewPasswords(array $credentials)
240243
* @param array $credentials
241244
* @return bool
242245
*/
243-
protected function validatePasswordWithDefaults(array $credentials)
244-
{
246+
protected function validatePasswordWithDefaults(array $credentials): bool
247+
{
245248
list($password, $confirm) = [$credentials['password'], $credentials['password_confirmation']];
246249

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

250-
/**
251-
* Get the user for the given credentials.
252-
*
253-
* @param array $credentials
254-
* @return \Illuminate\Auth\Reminders\RemindableInterface
255-
*
256-
* @throws \UnexpectedValueException
257-
*/
258-
public function getUser(array $credentials)
259-
{
260-
$credentials = array_except($credentials, array('token'));
253+
/**
254+
* Get the user for the given credentials.
255+
*
256+
* @param array $credentials
257+
* @return RemindableInterface|null
258+
* @throws \UnexpectedValueException
259+
*/
260+
public function getUser(array $credentials): ?RemindableInterface
261+
{
262+
$credentials = array_except($credentials, ['token']);
261263

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

@@ -274,8 +276,8 @@ public function getUser(array $credentials)
274276
*
275277
* @return \Illuminate\Auth\Reminders\ReminderRepositoryInterface
276278
*/
277-
protected function getRepository()
278-
{
279+
protected function getRepository(): ReminderRepositoryInterface
280+
{
279281
return $this->reminders;
280282
}
281283

0 commit comments

Comments
 (0)