cgk-rooster-streamen/src/Command/AppUserAddCommand.php

61 lines
1.7 KiB
PHP

<?php
namespace App\Command;
use App\Entity\User;
use App\Repository\UserRepository;
use Ramsey\Uuid\Uuid;
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;
#[AsCommand(
name: 'app:user:add',
description: 'Add a new user to the system.',
)]
class AppUserAddCommand extends Command
{
public function __construct(
private readonly UserRepository $userRepository
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('fullName', InputArgument::OPTIONAL, 'The full name of the user')
->addArgument('email', InputArgument::OPTIONAL, 'The email of the user');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$fullName = $input->getArgument('fullName');
$email = $input->getArgument('email');
if (!$fullName) {
$fullName = $io->ask('Enter the full name of the user');
}
if (!$email) {
$email = $io->ask('Enter the email of the user');
}
$token = Uuid::uuid4()->toString();
$user = new User();
$user->setFullName($fullName);
$user->setEmail($email);
$user->setToken($token);
$this->userRepository->save($user);
$io->success(sprintf('User "%s" with email "%s" has been added', $fullName, $email));
return Command::SUCCESS;
}
}