@@ -54,7 +54,7 @@ |
||
| 54 | 54 | * to code readability. |
| 55 | 55 | * However, by using `__invoke()` we lose code completion, so in the end I prefer to use this `execute()` method. |
| 56 | 56 | * |
| 57 | - * @return LatestPostWithAuthorAndTagsDto[] |
|
| 57 | + * @return ResultCollectionInterface |
|
| 58 | 58 | */ |
| 59 | 59 | public function execute(int $limit = self::NUM_ITEMS): ResultCollectionInterface |
| 60 | 60 | { |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | /** |
| 47 | 47 | * @throws \Exception |
| 48 | 48 | * |
| 49 | - * @return PostsBySearchRequestDto[] |
|
| 49 | + * @return ResultCollectionInterface |
|
| 50 | 50 | */ |
| 51 | 51 | public function execute(string $queryString, int $limit = self::NUM_ITEMS): ResultCollectionInterface |
| 52 | 52 | { |
@@ -71,7 +71,7 @@ |
||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | - * @return User[] |
|
| 74 | + * @return ResultCollectionInterface |
|
| 75 | 75 | */ |
| 76 | 76 | public function findAll(array $orderByList = ['id' => 'DESC'], int $maxResults = null): ResultCollectionInterface |
| 77 | 77 | { |
@@ -37,6 +37,9 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | private $controller; |
| 39 | 39 | |
| 40 | + /** |
|
| 41 | + * @param callable $controller |
|
| 42 | + */ |
|
| 40 | 43 | public function setController(?callable $controller): void |
| 41 | 44 | { |
| 42 | 45 | $this->controller = $controller; |
@@ -272,6 +272,9 @@ discard block |
||
| 272 | 272 | : $this->tags->clear(); |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | + /** |
|
| 276 | + * @param Comment[] $list |
|
| 277 | + */ |
|
| 275 | 278 | private function contains($item, $list): bool |
| 276 | 279 | { |
| 277 | 280 | // we need to cast the list to array because it might just actually be a doctrine collection |
@@ -279,6 +282,7 @@ discard block |
||
| 279 | 282 | } |
| 280 | 283 | |
| 281 | 284 | /** |
| 285 | + * @param Comment[] $list |
|
| 282 | 286 | * @return false|int|string |
| 283 | 287 | */ |
| 284 | 288 | private function getKey($item, $list) |
@@ -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 | -} |
|
@@ -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,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 | -} |
|
@@ -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 | -} |
|