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
|
|
|
private UserRepository $users; |
21
|
|
|
|
22
|
|
|
public function __construct(UserRepository $users) |
23
|
|
|
{ |
24
|
|
|
parent::__construct(); |
25
|
|
|
$this->users = $users; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function configure(): void |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
|
|
->setDescription('Lists all the existing users') |
32
|
|
|
->addOption( |
33
|
|
|
'limit', |
34
|
|
|
null, |
35
|
|
|
InputOption::VALUE_OPTIONAL, |
36
|
|
|
'Limits the number of users listed', |
37
|
|
|
50 |
38
|
|
|
) |
39
|
|
|
; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
43
|
|
|
{ |
44
|
|
|
$limit = $input->getOption('limit'); |
45
|
|
|
|
46
|
|
|
$allUsers = $this->users->findBy([], ['id' => 'DESC'], $limit); |
47
|
|
|
|
48
|
|
|
$usersAsPlainArrays = array_map(function (User $user) { |
49
|
|
|
return [ |
50
|
|
|
$user->getId(), |
51
|
|
|
$user->getProfile()->getFullName(), |
52
|
|
|
$user->getUsername(), |
53
|
|
|
$user->getEmail(), |
54
|
|
|
implode(', ', $user->getRoles()), |
55
|
|
|
]; |
56
|
|
|
}, $allUsers); |
57
|
|
|
|
58
|
|
|
$bufferedOutput = new BufferedOutput(); |
59
|
|
|
$io = new SymfonyStyle($input, $bufferedOutput); |
60
|
|
|
$io->table( |
61
|
|
|
['ID', 'Full Name', 'Username', 'Email', 'Roles'], |
62
|
|
|
$usersAsPlainArrays |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$usersAsATable = $bufferedOutput->fetch(); |
66
|
|
|
$output->write($usersAsATable); |
67
|
|
|
|
68
|
|
|
return Command::SUCCESS; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|