Skip to content

Commit ced11a7

Browse files
committed
minor #1123 [make:reset-password] cleanup legacy code (jrushlow)
This PR was merged into the 1.0-dev branch. Discussion ---------- [make:reset-password] cleanup legacy code Commits ------- cf2f19e [make:reset-password] cleanup legacy code
2 parents cabeea3 + cf2f19e commit ced11a7

File tree

3 files changed

+17
-35
lines changed

3 files changed

+17
-35
lines changed

src/Maker/MakeResetPassword.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
use Symfony\Component\OptionsResolver\OptionsResolver;
5151
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
5252
use Symfony\Component\Routing\Annotation\Route;
53-
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
5453
use Symfony\Component\Translation\Translator;
5554
use Symfony\Component\Validator\Constraints\Length;
5655
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -214,16 +213,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
214213
'Form\\'
215214
);
216215

217-
/*
218-
* @legacy Conditional can be removed when MakerBundle no longer
219-
* supports Symfony < 5.2
220-
*/
221-
$passwordHasher = UserPasswordEncoderInterface::class;
222-
223-
if (interface_exists(UserPasswordHasherInterface::class)) {
224-
$passwordHasher = UserPasswordHasherInterface::class;
225-
}
226-
227216
$useStatements = new UseStatementGenerator([
228217
AbstractController::class,
229218
$userClassNameDetails->getFullName(),
@@ -239,7 +228,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
239228
ResetPasswordControllerTrait::class,
240229
ResetPasswordExceptionInterface::class,
241230
ResetPasswordHelperInterface::class,
242-
$passwordHasher,
231+
UserPasswordHasherInterface::class,
243232
EntityManagerInterface::class,
244233
]);
245234

@@ -269,9 +258,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
269258
'from_email_name' => $this->fromEmailName,
270259
'email_getter' => $this->emailGetterMethodName,
271260
'email_field' => $this->emailPropertyName,
272-
'password_hasher_class_details' => ($passwordClassDetails = $generator->createClassNameDetails($passwordHasher, '\\')),
273-
'password_hasher_variable_name' => str_replace('Interface', '', sprintf('$%s', lcfirst($passwordClassDetails->getShortName()))), // @legacy see passwordHasher conditional above
274-
'use_password_hasher' => UserPasswordHasherInterface::class === $passwordHasher, // @legacy see passwordHasher conditional above
275261
'problem_validate_message_or_constant' => $problemValidateMessageOrConstant,
276262
'problem_handle_message_or_constant' => $problemHandleMessageOrConstant,
277263
'translator_available' => $isTranslatorAvailable,

src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function checkEmail(): Response
5858
* Validates and process the reset URL that the user clicked in their email.
5959
*/
6060
#[Route('/reset/{token}', name: 'app_reset_password')]
61-
public function reset(Request $request, <?= $password_hasher_class_details->getShortName() ?> <?= $password_hasher_variable_name ?><?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?>, string $token = null): Response
61+
public function reset(Request $request, UserPasswordHasherInterface $passwordHasher<?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?>, string $token = null): Response
6262
{
6363
if ($token) {
6464
// We store the token in session and remove it from the URL, to avoid the URL being
@@ -94,7 +94,7 @@ public function reset(Request $request, <?= $password_hasher_class_details->getS
9494
$this->resetPasswordHelper->removeResetRequest($token);
9595

9696
// Encode(hash) the plain password, and set it.
97-
$encodedPassword = <?= $password_hasher_variable_name ?>-><?= $use_password_hasher ? 'hashPassword' : 'encodePassword' ?>(
97+
$encodedPassword = $passwordHasher->hashPassword(
9898
$user,
9999
$form->get('plainPassword')->getData()
100100
);

tests/Maker/MakeResetPasswordTest.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,19 @@ public function getTestDetails(): \Generator
6464

6565
$this->assertSame('App\Repository\ResetPasswordRequestRepository', $resetPasswordConfig['symfonycasts_reset_password']['request_password_repository']);
6666

67-
$this->runResetPasswordTest($runner, 'it_generates_with_normal_setup.php');
67+
$runner->writeFile(
68+
'config/packages/mailer.yaml',
69+
Yaml::dump(['framework' => [
70+
'mailer' => ['dsn' => 'null://null'],
71+
]])
72+
);
73+
74+
$runner->copy(
75+
'make-reset-password/tests/it_generates_with_normal_setup.php',
76+
'tests/ResetPasswordFunctionalTest.php'
77+
);
78+
79+
$runner->runTests();
6880
}),
6981
];
7082

@@ -105,6 +117,7 @@ public function getTestDetails(): \Generator
105117

106118
$this->assertStringContainsString('Success', $output);
107119

120+
// @legacy - conditional can be removed when PHPUnit 9.x is no longer supported.
108121
if (method_exists($this, 'assertFileDoesNotExist')) {
109122
$this->assertFileDoesNotExist($runner->getPath('config/packages/reset_password.yaml'));
110123
} else {
@@ -184,23 +197,6 @@ public function getTestDetails(): \Generator
184197
];
185198
}
186199

187-
private function runResetPasswordTest(MakerTestRunner $runner, string $filename): void
188-
{
189-
$runner->writeFile(
190-
'config/packages/mailer.yaml',
191-
Yaml::dump(['framework' => [
192-
'mailer' => ['dsn' => 'null://null'],
193-
]])
194-
);
195-
196-
$runner->copy(
197-
'make-reset-password/tests/'.$filename,
198-
'tests/ResetPasswordFunctionalTest.php'
199-
);
200-
201-
$runner->runTests();
202-
}
203-
204200
private function makeUser(MakerTestRunner $runner, string $identifier = 'email', string $userClass = 'User', bool $checkPassword = true): void
205201
{
206202
$runner->runConsole('make:user', [

0 commit comments

Comments
 (0)