Skip to content

Commit

Permalink
Merge pull request #29 from sumocoders/create-user-command
Browse files Browse the repository at this point in the history
Add console command to add user
  • Loading branch information
tijsverkoyen authored Oct 9, 2024
2 parents 3b2fcec + 9064039 commit d034738
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/Command/User/CreateUserCommand.php
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']
);
}
}

0 comments on commit d034738

Please sign in to comment.