You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FetchEmailsCommand.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Command;
  3. use App\Service\MailFetcher;
  4. use Symfony\Component\Console\Attribute\AsCommand;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. #[AsCommand(
  12. name: 'app:fetch-emails',
  13. description: 'Fetch emails and process them',
  14. )]
  15. class FetchEmailsCommand extends Command
  16. {
  17. private MailFetcher $mailFetcher;
  18. public function __construct(MailFetcher $mailFetcher)
  19. {
  20. parent::__construct();
  21. $this->mailFetcher = $mailFetcher;
  22. }
  23. protected function configure(): void
  24. {
  25. $this
  26. ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
  27. ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description');
  28. }
  29. protected function execute(InputInterface $input, OutputInterface $output): int
  30. {
  31. $io = new SymfonyStyle($input, $output);
  32. $arg1 = $input->getArgument('arg1');
  33. if ($arg1) {
  34. $io->note(sprintf('You passed an argument: %s', $arg1));
  35. }
  36. if ($input->getOption('option1')) {
  37. // ...
  38. }
  39. $this->mailFetcher->fetchNewEmails();
  40. $io->success('You have a new command! Now make it your own! Pass --help to see your options.');
  41. return Command::SUCCESS;
  42. }
  43. }