#21 Adding 'invite:add' command.
This commit is contained in:
parent
387a44722e
commit
6790c7bdb1
60
src/Command/AddInvitesCommand.php
Normal file
60
src/Command/AddInvitesCommand.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -66,7 +66,7 @@ class AddUserCommand extends Command
|
|||
}
|
||||
|
||||
if (!$password) {
|
||||
$output->writeln('User password cannot be empty.');
|
||||
$output->writeln('<error>User password cannot be empty.</error>');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ class AddUserCommand extends Command
|
|||
|
||||
$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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue