Merged in feature/add_invites (pull request #52)

#21 Adding 'invite:add' command.
This commit is contained in:
Alexey Eschenko 2020-12-19 22:23:00 +00:00
commit d9b3610132
2 changed files with 62 additions and 2 deletions

View file

@ -0,0 +1,60 @@
<?php
namespace App\Command;
use App\Repository\UserRepository;
use App\User\InviteManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputArgument, InputInterface};
use Symfony\Component\Console\Output\OutputInterface;
class AddInvitesCommand extends Command
{
/** @var EntityManagerInterface */
private $em;
/** @var UserRepository */
private $userRepo;
/** @var InviteManager */
private $inviteManager;
public function __construct(EntityManagerInterface $em, UserRepository $userRepo, InviteManager $inviteManager)
{
parent::__construct();
$this->em = $em;
$this->userRepo = $userRepo;
$this->inviteManager = $inviteManager;
}
protected function configure()
{
$this
->setName('invite:add')
->addArgument('username', InputArgument::REQUIRED, 'Username')
->addArgument('number', InputArgument::REQUIRED, 'Number of invites')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$number = $input->getArgument('number');
if (null === $user = $this->userRepo->findOneBy(['username' => $username])) {
$output->writeln('<error>User not found.</error>');
return 1;
}
$this->inviteManager->createInvitesForUser($user, $number);
$this->em->flush();
$output->writeln(sprintf('<info>%d invites added to \'%s\'.</info>', $number, $user->getUsername()));
return 0;
}
}

View file

@ -66,7 +66,7 @@ class AddUserCommand extends Command
} }
if (!$password) { if (!$password) {
$output->writeln('User password cannot be empty.'); $output->writeln('<error>User password cannot be empty.</error>');
return 1; return 1;
} }
@ -86,7 +86,7 @@ class AddUserCommand extends Command
$this->em->flush(); $this->em->flush();
$output->writeln(sprintf('User \'%s\' registered, %d invites added.', $user->getUsername(), $invites)); $output->writeln(sprintf('<info>User \'%s\' registered, %d invites added.</info>', $user->getUsername(), $invites));
return 0; return 0;
} }