@@ -238,27 +238,3 @@ |
||
| 238 | 238 | private function getCommandHelp(): string |
| 239 | 239 | { |
| 240 | 240 | return <<<'HELP' |
| 241 | - The <info>%command.name%</info> command creates new users and saves them in the database: |
|
| 242 | - |
|
| 243 | - <info>php %command.full_name%</info> <comment>username password email</comment> |
|
| 244 | - |
|
| 245 | - By default the command creates regular users. To create administrator users, |
|
| 246 | - add the <comment>--admin</comment> option: |
|
| 247 | - |
|
| 248 | - <info>php %command.full_name%</info> username password email mobile <comment>--admin</comment> |
|
| 249 | - |
|
| 250 | - If you omit any of the three required arguments, the command will ask you to |
|
| 251 | - provide the missing values: |
|
| 252 | - |
|
| 253 | - # command will ask you for the email |
|
| 254 | - <info>php %command.full_name%</info> <comment>username password</comment> |
|
| 255 | - |
|
| 256 | - # command will ask you for the email and password |
|
| 257 | - <info>php %command.full_name%</info> <comment>username</comment> |
|
| 258 | - |
|
| 259 | - # command will ask you for all arguments |
|
| 260 | - <info>php %command.full_name%</info> |
|
| 261 | - |
|
| 262 | - HELP; |
|
| 263 | - } |
|
| 264 | -} |
|
@@ -83,55 +83,3 @@ |
||
| 83 | 83 | ->setDescription('Deletes users from the database') |
| 84 | 84 | ->addArgument('username', InputArgument::REQUIRED, 'The username of an existing user') |
| 85 | 85 | ->setHelp(<<<'HELP' |
| 86 | - The <info>%command.name%</info> command deletes users from the database: |
|
| 87 | - |
|
| 88 | - <info>php %command.full_name%</info> <comment>username</comment> |
|
| 89 | - |
|
| 90 | - If you omit the argument, the command will ask you to |
|
| 91 | - provide the missing value: |
|
| 92 | - |
|
| 93 | - <info>php %command.full_name%</info> |
|
| 94 | - HELP |
|
| 95 | - ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - protected function initialize(InputInterface $input, OutputInterface $output): void |
|
| 99 | - { |
|
| 100 | - // SymfonyStyle is an optional feature that Symfony provides so you can |
|
| 101 | - // apply a consistent look to the commands of your application. |
|
| 102 | - // See https://symfony.com/doc/current/console/style.html |
|
| 103 | - $this->io = new SymfonyStyle($input, $output); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - protected function interact(InputInterface $input, OutputInterface $output): void |
|
| 107 | - { |
|
| 108 | - if ($input->getArgument('username') !== null) { |
|
| 109 | - return; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $this->io->title('Delete User Command Interactive Wizard'); |
|
| 113 | - $this->io->text([ |
|
| 114 | - 'If you prefer to not use this interactive wizard, provide the', |
|
| 115 | - 'arguments required by this command as follows:', |
|
| 116 | - '', |
|
| 117 | - ' $ php bin/console app:delete-user username', |
|
| 118 | - '', |
|
| 119 | - 'Now we\'ll ask you for the value of all the missing command arguments.', |
|
| 120 | - '', |
|
| 121 | - ]); |
|
| 122 | - |
|
| 123 | - $username = $this->io->ask('Username', null, [$this->validator, 'validateUsername']); |
|
| 124 | - $input->setArgument('username', $username); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - protected function execute(InputInterface $input, OutputInterface $output): void |
|
| 128 | - { |
|
| 129 | - $username = $input->getArgument('username'); |
|
| 130 | - |
|
| 131 | - $this->userService->deleteUser($username); |
|
| 132 | - |
|
| 133 | - $this->entityManager->flush(); |
|
| 134 | - |
|
| 135 | - $this->io->success(sprintf('User "%s" was successfully deleted.', $username)); |
|
| 136 | - } |
|
| 137 | -} |
|
@@ -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 | -} |
|