1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Command; |
6
|
|
|
|
7
|
|
|
use App\Entity\User; |
8
|
|
|
use App\Repository\UserRepository; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
|
16
|
|
|
final class ListUsersCommand extends Command |
17
|
|
|
{ |
18
|
|
|
protected static $defaultName = 'app:list-users'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var UserRepository |
22
|
|
|
*/ |
23
|
|
|
private $users; |
24
|
|
|
|
25
|
|
|
public function __construct(UserRepository $users) |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
$this->users = $users; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function configure() |
32
|
|
|
{ |
33
|
|
|
$this |
34
|
|
|
->setDescription('Lists all the existing users') |
35
|
|
|
->addOption( |
36
|
|
|
'limit', |
37
|
|
|
null, |
38
|
|
|
InputOption::VALUE_OPTIONAL, |
39
|
|
|
'Limits the number of users listed', |
40
|
|
|
50 |
41
|
|
|
) |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
46
|
|
|
{ |
47
|
|
|
$limit = $input->getOption('limit'); |
48
|
|
|
|
49
|
|
|
$allUsers = $this->users->findBy([], ['id' => 'DESC'], $limit); |
50
|
|
|
|
51
|
|
|
$usersAsPlainArrays = array_map(function (User $user) { |
52
|
|
|
return [ |
53
|
|
|
$user->getId(), |
54
|
|
|
$user->getFullName(), |
55
|
|
|
$user->getUsername(), |
56
|
|
|
$user->getEmail(), |
57
|
|
|
implode(', ', $user->getRoles()), |
58
|
|
|
]; |
59
|
|
|
}, $allUsers); |
60
|
|
|
|
61
|
|
|
$bufferedOutput = new BufferedOutput(); |
62
|
|
|
$io = new SymfonyStyle($input, $bufferedOutput); |
63
|
|
|
$io->table( |
64
|
|
|
['ID', 'Full Name', 'Username', 'Email', 'Roles'], |
65
|
|
|
$usersAsPlainArrays |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$usersAsATable = $bufferedOutput->fetch(); |
69
|
|
|
$output->write($usersAsATable); |
70
|
|
|
|
71
|
|
|
return 0; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|