rvr-nextgen/src/Cli/AddUserCommand.php

54 lines
1.7 KiB
PHP
Raw Normal View History

2023-04-08 00:42:55 +02:00
<?php namespace RVR\Cli;
use DateTime;
use RVR\PersistentData\Model\User;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class AddUserCommand extends Command
{
public function configure(): void
{
$this->setName('user:add')
->setDescription('Adding of user.')
->addArgument('email', InputArgument::REQUIRED, 'Email of user')
->addArgument('password', InputArgument::REQUIRED, 'Password of user')
->addArgument('type', InputArgument::OPTIONAL, 'Type of user');;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
2023-04-09 01:35:40 +02:00
if (!filter_var($input->getArgument('email'), FILTER_VALIDATE_EMAIL)) {
$output->writeln('<error>Please provide a valid email address.</error>');
return 1;
}
2023-04-08 00:42:55 +02:00
$user = new User();
$user->setEmail($input->getArgument('email'));
$user->setPlainPassword($input->getArgument('password'));
$user->setCreatedDate(new DateTime());
if ($input->hasArgument('type') && $input->getArgument('type') !== null) {
$user->setType($input->getArgument('type'));
}
try {
2023-04-19 23:19:22 +02:00
\Container::$persistentDataManager->saveToDb($user);
2023-04-08 00:42:55 +02:00
} catch (\Exception $e) {
$output->writeln('<error>Adding user failed!</error>');
$output->writeln('');
$output->writeln((string) $e);
$output->writeln('');
return 1;
}
$output->writeln('<info>User was successfully added!</info>');
return 0;
}
}