-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from sumocoders/create-user-command
Add console command to add user
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Command\User; | ||
|
||
use App\Message\User\CreateUser; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
#[AsCommand(name: 'app:create-user', description: 'Hello PhpStorm')] | ||
class CreateUserCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly MessageBusInterface $messageBus, | ||
private readonly ValidatorInterface $validator, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$message = new CreateUser(); | ||
$message->email = $input->getArgument('email'); | ||
$message->roles = $input->getArgument('roles'); | ||
|
||
$constraints = $this->validator->validate($message); | ||
if ($constraints->count() > 0) { | ||
foreach ($constraints as $constraint) { | ||
$io->error($constraint->getPropertyPath() . ': ' . $constraint->getMessage()); | ||
} | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$this->messageBus->dispatch($message); | ||
|
||
$io->success('User created, an email has been sent to the user.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addArgument( | ||
'email', | ||
InputArgument::REQUIRED, | ||
'The email of the user' | ||
); | ||
$this->addArgument( | ||
'roles', | ||
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, | ||
'The roles of the user', | ||
['ROLE_USER'] | ||
); | ||
} | ||
} |