<?php
/**
* Created by PhpStorm.
* User: Daniel
* Date: 1/21/2015
* Time: 11:12 PM
*/
namespace Admedix\SysadminBundle\Command;
use Doctrine\ORM\EntityRepository;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ListUserCommand extends ContainerAwareCommand
{
/**
* @var EntityRepository
*/
private $userRepository;
/**
* ListUserCommand constructor.
* @param EntityRepository $userRepository
*/
public function __construct(EntityRepository $userRepository)
{
$this->userRepository = $userRepository;
parent::__construct(null) ;
}
protected function configure()
{
$this
->setName('admedix:sysadmin:list')
->setDescription('List sysadmin users');
}
/**
* @return \Doctrine\Common\Persistence\ObjectRepository
*/
protected function getUserAdminRepository()
{
return $this->userRepository;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$users = $this->getUserAdminRepository()->findAll();
$output->writeln('Total Users: '.count($users));
$this->printTable($users, $output);
}
/**
* @param User[] $users
*/
protected function printTable($users, OutputInterface $output)
{
if (!count($users)) {
return;
}
$table = new Table($output) ;
$table
->setHeaders(array('Username', 'Date Created', 'Status'));
foreach ($users as $user) {
$table->addRow(
array(
$user->getUsername(),
$user->getCreatedAt()->format('Y-m-d'),
$user->getActive() ? 'active' : 'inactive'
)
);
}
$table->render($output);
}
}