Skip to content

Commit d745c87

Browse files
committed
Invokable Commands and Input Attributes
1 parent 79355ed commit d745c87

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

src/Command/User/CreateCommand.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
namespace App\Command\User;
66

77
use App\Message\User\CreateUser;
8+
use Symfony\Component\Console\Attribute\Argument;
89
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Command\Command;
10-
use Symfony\Component\Console\Input\InputArgument;
11-
use Symfony\Component\Console\Input\InputInterface;
12-
use Symfony\Component\Console\Output\OutputInterface;
1311
use Symfony\Component\Console\Style\SymfonyStyle;
1412
use Symfony\Component\Messenger\MessageBusInterface;
1513
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -18,19 +16,20 @@
1816
class CreateCommand extends Command
1917
{
2018
public function __construct(
21-
private MessageBusInterface $messageBus,
19+
private readonly MessageBusInterface $messageBus,
2220
private readonly ValidatorInterface $validator,
2321
) {
2422
parent::__construct();
2523
}
2624

27-
protected function execute(InputInterface $input, OutputInterface $output): int
28-
{
29-
$io = new SymfonyStyle($input, $output);
30-
25+
public function __invoke(
26+
SymfonyStyle $io,
27+
#[Argument(description: 'The email of the user')] string $email,
28+
#[Argument(description: 'The roles of the user')] array $roles = ['ROLE_USER'],
29+
): int {
3130
$message = new CreateUser();
32-
$message->email = $input->getArgument('email');
33-
$message->roles = $input->getArgument('roles');
31+
$message->email = $email;
32+
$message->roles = $roles;
3433

3534
$constraints = $this->validator->validate($message);
3635
if ($constraints->count() > 0) {
@@ -47,19 +46,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4746

4847
return Command::SUCCESS;
4948
}
50-
51-
protected function configure(): void
52-
{
53-
$this->addArgument(
54-
'email',
55-
InputArgument::REQUIRED,
56-
'The email of the user'
57-
);
58-
$this->addArgument(
59-
'roles',
60-
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
61-
'The roles of the user',
62-
['ROLE_USER']
63-
);
64-
}
6549
}

0 commit comments

Comments
 (0)