Skip to content

Commit db0c787

Browse files
Fixes after review
1 parent 0f1c64b commit db0c787

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/Maker/MakeWebhook.php

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1515
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
1617
use Symfony\Bundle\MakerBundle\FileManager;
1718
use Symfony\Bundle\MakerBundle\Generator;
1819
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
@@ -51,11 +52,16 @@
5152

5253
/**
5354
* @author Maelan LE BORGNE <[email protected]>
55+
*
56+
* @internal
5457
*/
5558
final class MakeWebhook extends AbstractMaker implements InputAwareMakerInterface
5659
{
60+
/** @see https://regex101.com/r/S3BWkx/1 */
61+
public const WEBHOOK_NAME_PATTERN = '/^[a-zA-Z_.\-\x80-\xff][a-zA-Z0-9_.\-\x80-\xff]*$/u';
5762
private const WEBHOOK_CONFIG_PATH = 'config/packages/webhook.yaml';
5863
private YamlSourceManipulator $ysm;
64+
private ?string $name;
5965

6066
public function __construct(
6167
private FileManager $fileManager,
@@ -83,7 +89,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
8389
$inputConfig->setArgumentAsNonInteractive('name');
8490
}
8591

86-
public function configureDependencies(DependencyBuilder $dependencies, ?InputInterface $input = null)
92+
public function configureDependencies(DependencyBuilder $dependencies, ?InputInterface $input = null): void
8793
{
8894
$dependencies->addClassDependency(
8995
AbstractRequestParser::class,
@@ -101,9 +107,9 @@ public function configureDependencies(DependencyBuilder $dependencies, ?InputInt
101107

102108
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
103109
{
104-
if ($input->getArgument('name')) {
105-
if (!$this->verifyWebhookName($input->getArgument('name'))) {
106-
throw new \InvalidArgumentException('A webhook name can only have alphanumeric characters, underscores, dots, and dashes.');
110+
if ($this->name = $input->getArgument('name')) {
111+
if (!$this->verifyWebhookName($this->name)) {
112+
throw new RuntimeCommandException('A webhook name can only have alphanumeric characters, underscores, dots, and dashes.');
107113
}
108114

109115
return;
@@ -112,58 +118,56 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
112118
$argument = $command->getDefinition()->getArgument('name');
113119
$question = new Question($argument->getDescription());
114120
$question->setValidator(Validator::notBlank(...));
115-
$webhookName = $io->askQuestion($question);
116121

117-
while (!$this->verifyWebhookName($webhookName)) {
122+
$this->name = $io->askQuestion($question);
123+
while (!$this->verifyWebhookName($this->name)) {
118124
$io->error('A webhook name can only have alphanumeric characters, underscores, dots, and dashes.');
119-
$webhookName = $io->askQuestion($question);
125+
$this->name = $io->askQuestion($question);
120126
}
121-
122-
$input->setArgument('name', $webhookName);
123-
}
124-
125-
private function verifyWebhookName(string $entityName): bool
126-
{
127-
return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_.\-\x7f-\xff]*$/', $entityName);
128127
}
129128

130129
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
131130
{
132-
$webhookName = $input->getArgument('name');
133131
$requestParserDetails = $this->generator->createClassNameDetails(
134-
Str::asClassName($webhookName.'RequestParser'),
132+
Str::asClassName($this->name.'RequestParser'),
135133
'Webhook\\'
136134
);
137135
$remoteEventHandlerDetails = $this->generator->createClassNameDetails(
138-
Str::asClassName($webhookName.'WebhookHandler'),
136+
Str::asClassName($this->name.'WebhookHandler'),
139137
'RemoteEvent\\'
140138
);
141139

142-
$this->addToYamlConfig($webhookName, $requestParserDetails);
140+
$this->addToYamlConfig($this->name, $requestParserDetails);
143141

144142
$this->generateRequestParser($io, $requestParserDetails);
145143

146144
$this->generator->generateClass(
147145
$remoteEventHandlerDetails->getFullName(),
148146
'webhook/WebhookHandler.tpl.php',
149147
[
150-
'webhook_name' => $webhookName,
148+
'webhook_name' => $this->name,
151149
]
152150
);
153151

154152
$this->generator->writeChanges();
155153
$this->fileManager->dumpFile(self::WEBHOOK_CONFIG_PATH, $this->ysm->getContents());
156154
}
157155

156+
private function verifyWebhookName(string $entityName): bool
157+
{
158+
return preg_match(self::WEBHOOK_NAME_PATTERN, $entityName);
159+
}
160+
158161
private function addToYamlConfig(string $webhookName, ClassNameDetails $requestParserDetails): void
159162
{
160-
if (!$this->fileManager->fileExists(self::WEBHOOK_CONFIG_PATH)) {
161-
$yamlConfig = Yaml::dump(['framework' => ['webhook' => ['routing' => []]]], 4, 2);
162-
} else {
163+
$yamlConfig = Yaml::dump(['framework' => ['webhook' => ['routing' => []]]], 4, 2);
164+
if ($this->fileManager->fileExists(self::WEBHOOK_CONFIG_PATH)) {
163165
$yamlConfig = $this->fileManager->getFileContents(self::WEBHOOK_CONFIG_PATH);
164166
}
167+
165168
$this->ysm = new YamlSourceManipulator($yamlConfig);
166169
$arrayConfig = $this->ysm->getData();
170+
167171
if (\array_key_exists($webhookName, $arrayConfig['framework']['webhook']['routing'] ?? [])) {
168172
throw new \InvalidArgumentException('A webhook with this name already exists');
169173
}
@@ -180,7 +184,7 @@ private function addToYamlConfig(string $webhookName, ClassNameDetails $requestP
180184
/**
181185
* @throws \Exception
182186
*/
183-
public function generateRequestParser(ConsoleStyle $io, ClassNameDetails $requestParserDetails): void
187+
private function generateRequestParser(ConsoleStyle $io, ClassNameDetails $requestParserDetails): void
184188
{
185189
$useStatements = new UseStatementGenerator([
186190
JsonException::class,
@@ -191,6 +195,7 @@ public function generateRequestParser(ConsoleStyle $io, ClassNameDetails $reques
191195
RejectWebhookException::class,
192196
RequestMatcherInterface::class,
193197
]);
198+
194199
$requestMatchers = [];
195200
while (true) {
196201
$newRequestMatcher = $this->askForNextRequestMatcher($io, $requestMatchers, $requestParserDetails->getFullName(), empty($requestMatchers));
@@ -199,12 +204,14 @@ public function generateRequestParser(ConsoleStyle $io, ClassNameDetails $reques
199204
}
200205
$requestMatchers[] = $newRequestMatcher;
201206
}
202-
$useChainRequestsMatcher = false;
207+
203208
// Use a ChainRequestMatcher if multiple matchers have been added OR if none (will be printed with an empty array)
209+
$useChainRequestsMatcher = false;
204210
if (1 !== \count($requestMatchers)) {
205211
$useChainRequestsMatcher = true;
206212
$useStatements->addUseStatement(ChainRequestMatcher::class);
207213
}
214+
208215
$requestMatcherArguments = [];
209216
foreach ($requestMatchers as $requestMatcherClass) {
210217
$useStatements->addUseStatement($requestMatcherClass);
@@ -245,6 +252,7 @@ private function askForNextRequestMatcher(ConsoleStyle $io, array $addedMatchers
245252
$choices = array_diff($availableMatchers, $addedMatchers);
246253
$question = new ChoiceQuestion($questionText, array_values(['<skip>'] + $choices), 0);
247254
$matcherName = $io->askQuestion($question);
255+
248256
if ('<skip>' === $matcherName) {
249257
return null;
250258
}

tests/Maker/MakeWebhookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function getMakerClass(): string
2424

2525
public function getTestDetails(): \Generator
2626
{
27-
yield 'it_makes_webhook_whit_no_prior_config_file' => [$this->createMakerTest()
27+
yield 'it_makes_webhook_with_no_prior_config_file' => [$this->createMakerTest()
2828
->run(function (MakerTestRunner $runner) {
2929
$output = $runner->runMaker([
3030
// webhook name

0 commit comments

Comments
 (0)