Skip to content

Commit 15a6435

Browse files
committed
update with review
1 parent add9455 commit 15a6435

File tree

2 files changed

+22
-46
lines changed

2 files changed

+22
-46
lines changed

src/Maker/Common/CanGenerateTestsTrait.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function configureCommandWithTestsOption(Command $command): Command
3232
$help = $command->getHelp()."\n".$testsHelp;
3333

3434
$command
35-
->addOption(name: 'with-tests', mode: InputOption::VALUE_NONE, description: 'Generate PHPUnit Tests')
35+
->addOption(name: 'with-tests', mode: InputOption::VALUE_NEGATABLE, description: 'Generate PHPUnit Tests')
3636
->setHelp($help)
3737
;
3838

@@ -46,12 +46,7 @@ public function interactSetGenerateTests(InputInterface $input, ConsoleStyle $io
4646
throw new RuntimeCommandException('Whoops! "--with-tests" option does not exist. Call "addWithTestsOptions()" in the makers "configureCommand().');
4747
}
4848

49-
$this->generateTests = $input->getOption('with-tests');
50-
51-
if (!$this->generateTests) {
52-
$this->generateTests = $io->confirm('Do you want to generate PHPUnit tests? [Experimental]', false);
53-
$input->setOption('with-tests', $this->generateTests);
54-
}
49+
$this->generateTests = $input->getOption('with-tests') ?? $io->confirm('Do you want to generate PHPUnit tests? [Experimental]', false);
5550
}
5651

5752
public function shouldGenerateTests(): bool

src/Maker/Security/MakeFormLogin.php

+20-39
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ final class MakeFormLogin extends AbstractMaker
5959

6060
private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml';
6161
private YamlSourceManipulator $ysm;
62+
private string $controllerName;
6263
private string $firewallToUpdate;
6364
private string $userClass;
6465
private string $userNameField;
65-
/** @var ?array<string, mixed> */
66-
private ?array $securityData = null;
66+
private bool $willLogout;
6767

6868
public function __construct(
6969
private FileManager $fileManager,
@@ -80,7 +80,7 @@ public static function getCommandName(): string
8080
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
8181
{
8282
$command->addArgument('controllerName', InputArgument::OPTIONAL, 'The class name of the Controller (e.g. <fg=yellow>SecurityController</>)')
83-
->addOption('will-logout', null, InputOption::VALUE_NONE, 'Will generate a \'/logout\' URL? ')
83+
->addOption('will-logout', null, InputOption::VALUE_NEGATABLE, 'Will generate a \'/logout\' URL? ')
8484
->setHelp($this->getHelpFileContents('security/MakeFormLogin.txt'));
8585

8686
$this->configureCommandWithTestsOption($command);
@@ -116,44 +116,38 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
116116
throw new RuntimeCommandException(\sprintf('The file "%s" does not exist. PHP & XML configuration formats are currently not supported.', self::SECURITY_CONFIG_PATH));
117117
}
118118

119-
$securityData = $this->getSecurityData();
119+
$this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH));
120+
$securityData = $this->ysm->getData();
120121

121122
if (!isset($securityData['security']['providers']) || !$securityData['security']['providers']) {
122123
throw new RuntimeCommandException('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml".');
123124
}
124125

125-
if (null === $input->getArgument('controllerName')) {
126-
$input->setArgument(
127-
'controllerName', $io->ask(
128-
'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>)',
129-
'SecurityController',
130-
Validator::validateClassName(...)
131-
));
132-
}
126+
$this->controllerName = $input->getArgument('controllerName') ?? $io->ask(
127+
'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>)',
128+
'SecurityController',
129+
Validator::validateClassName(...)
130+
);
133131

134-
if (false === $input->getOption('will-logout')) {
135-
$input->setOption('will-logout', $io->confirm('Do you want to generate a \'/logout\' URL?'));
136-
}
132+
$securityHelper = new InteractiveSecurityHelper();
133+
$this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData);
134+
$this->userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']);
135+
$this->userNameField = $securityHelper->guessUserNameField($io, $this->userClass, $securityData['security']['providers']);
136+
$this->willLogout = $input->getOption('will-logout') ?? $io->confirm('Do you want to generate a \'/logout\' URL?');
137137

138138
$this->interactSetGenerateTests($input, $io);
139139
}
140140

141141
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
142142
{
143-
$securityData = $this->getSecurityData();
144-
$securityHelper = new InteractiveSecurityHelper();
145-
$this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData);
146-
$this->userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']);
147-
$this->userNameField = $securityHelper->guessUserNameField($io, $this->userClass, $securityData['security']['providers']);
148-
149143
$useStatements = new UseStatementGenerator([
150144
AbstractController::class,
151145
Response::class,
152146
Route::class,
153147
AuthenticationUtils::class,
154148
]);
155149

156-
$controllerNameDetails = $generator->createClassNameDetails($input->getArgument('controllerName'), 'Controller\\', 'Controller');
150+
$controllerNameDetails = $generator->createClassNameDetails($this->controllerName, 'Controller\\', 'Controller');
157151
$templatePath = strtolower($controllerNameDetails->getRelativeNameWithoutSuffix());
158152

159153
$controllerPath = $generator->generateController(
@@ -166,7 +160,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
166160
]
167161
);
168162

169-
if ($input->getOption('will-logout')) {
163+
if ($this->willLogout) {
170164
$manipulator = new ClassSourceManipulator($generator->getFileContentsForPendingOperation($controllerPath));
171165

172166
$this->securityControllerBuilder->addLogoutMethod($manipulator);
@@ -178,19 +172,19 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
178172
\sprintf('%s/login.html.twig', $templatePath),
179173
'security/formLogin/login_form.tpl.php',
180174
[
181-
'logout_setup' => $input->getOption('will-logout'),
175+
'logout_setup' => $this->willLogout,
182176
'username_label' => Str::asHumanWords($this->userNameField),
183177
'username_is_email' => false !== stripos($this->userNameField, 'email'),
184178
]
185179
);
186180

187181
$securityData = $this->securityConfigUpdater->updateForFormLogin($this->ysm->getContents(), $this->firewallToUpdate, 'app_login', 'app_login');
188182

189-
if ($input->getOption('will-logout')) {
183+
if ($this->willLogout) {
190184
$securityData = $this->securityConfigUpdater->updateForLogout($securityData, $this->firewallToUpdate);
191185
}
192186

193-
if ($input->getOption('with-tests')) {
187+
if ($this->shouldGenerateTests()) {
194188
$userClassNameDetails = $generator->createClassNameDetails(
195189
'\\'.$this->userClass,
196190
'Entity\\'
@@ -234,17 +228,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
234228
\sprintf('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs.', $templatePath),
235229
]);
236230
}
237-
238-
/**
239-
* @return array<string, mixed> $items
240-
*/
241-
private function getSecurityData(): array
242-
{
243-
if (null === $this->securityData) {
244-
$this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH));
245-
$this->securityData = $this->ysm->getData();
246-
}
247-
248-
return $this->securityData;
249-
}
250231
}

0 commit comments

Comments
 (0)