@@ -83,81 +83,3 @@ |
||
| 83 | 83 | $this |
| 84 | 84 | ->setDescription('Lists all the existing users') |
| 85 | 85 | ->setHelp(<<<'HELP' |
| 86 | - The <info>%command.name%</info> command lists all the users registered in the application: |
|
| 87 | - |
|
| 88 | - <info>php %command.full_name%</info> |
|
| 89 | - |
|
| 90 | - By default the command only displays the 50 most recent users. Set the number of |
|
| 91 | - results to display with the <comment>--max-results</comment> option: |
|
| 92 | - |
|
| 93 | - <info>php %command.full_name%</info> <comment>--max-results=2000</comment> |
|
| 94 | - |
|
| 95 | - In addition to displaying the user list, you can also send this information to |
|
| 96 | - the email address specified in the <comment>--send-to</comment> option: |
|
| 97 | - |
|
| 98 | - <info>php %command.full_name%</info> <comment>[email protected]</comment> |
|
| 99 | - |
|
| 100 | - HELP |
|
| 101 | - ) |
|
| 102 | - // commands can optionally define arguments and/or options (mandatory and optional) |
|
| 103 | - // see https://symfony.com/doc/current/components/console/console_arguments.html |
|
| 104 | - ->addOption('max-results', null, InputOption::VALUE_OPTIONAL, 'Limits the number of users listed', 50) |
|
| 105 | - ->addOption('send-to', null, InputOption::VALUE_OPTIONAL, 'If set, the result is sent to the given email address'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * This method is executed after initialize(). It usually contains the logic |
|
| 110 | - * to execute to complete this command task. |
|
| 111 | - */ |
|
| 112 | - protected function execute(InputInterface $input, OutputInterface $output): void |
|
| 113 | - { |
|
| 114 | - $maxResults = $input->getOption('max-results'); |
|
| 115 | - $allUsers = $this->userRepository->findAll(['id' => 'DESC'], $maxResults); |
|
| 116 | - |
|
| 117 | - // Doctrine query returns an array of objects and we need an array of plain arrays |
|
| 118 | - $usersAsPlainArrays = array_map(function (User $user) { |
|
| 119 | - return [ |
|
| 120 | - $user->getId(), |
|
| 121 | - $user->getFullName(), |
|
| 122 | - $user->getUsername(), |
|
| 123 | - $user->getEmail(), |
|
| 124 | - implode(', ', $user->getRoles()), |
|
| 125 | - ]; |
|
| 126 | - }, $allUsers); |
|
| 127 | - |
|
| 128 | - // In your console commands you should always use the regular output type, |
|
| 129 | - // which outputs contents directly in the console window. However, this |
|
| 130 | - // command uses the BufferedOutput type instead, to be able to get the output |
|
| 131 | - // contents before displaying them. This is needed because the command allows |
|
| 132 | - // to send the list of users via email with the '--send-to' option |
|
| 133 | - $bufferedOutput = new BufferedOutput(); |
|
| 134 | - $io = new SymfonyStyle($input, $bufferedOutput); |
|
| 135 | - $io->table( |
|
| 136 | - ['ID', 'Full Name', 'Username', 'Email', 'Roles'], |
|
| 137 | - $usersAsPlainArrays |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - // instead of just displaying the table of users, store its contents in a variable |
|
| 141 | - $usersAsATable = $bufferedOutput->fetch(); |
|
| 142 | - $output->write($usersAsATable); |
|
| 143 | - |
|
| 144 | - if (null !== $email = $input->getOption('send-to')) { |
|
| 145 | - $this->sendReport($usersAsATable, $email); |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Sends the given $contents to the $recipient email address. |
|
| 151 | - */ |
|
| 152 | - private function sendReport(string $contents, string $recipient): void |
|
| 153 | - { |
|
| 154 | - // See https://symfony.com/doc/current/cookbook/email/email.html |
|
| 155 | - $message = $this->mailer->createMessage() |
|
| 156 | - ->setSubject(sprintf('app:list-users report (%s)', date('Y-m-d H:i:s'))) |
|
| 157 | - ->setFrom($this->emailSender) |
|
| 158 | - ->setTo($recipient) |
|
| 159 | - ->setBody($contents, 'text/plain'); |
|
| 160 | - |
|
| 161 | - $this->mailer->send($message); |
|
| 162 | - } |
|
| 163 | -} |
|