bundle/w3des/NewsletterBundle/src/Command/SendCommand.php line 71

Open in your IDE?
  1. <?php
  2. namespace w3des\NewsletterBundle\Command;
  3. use Symfony\Component\Console\Input\InputArgument;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use w3des\AdminBundle\Entity\User;
  7. use w3des\NewsletterBundle\Util;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use w3des\NewsletterBundle\Entity\NewsletterContent;
  10. use Symfony\Component\Console\Command\Command;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. class SendCommand extends Command
  13. {
  14. /**
  15. * @var string|null The default command name
  16. */
  17. protected static $defaultName = 'admin:newsletter:send';
  18. /**
  19. * @var string|null The default command description
  20. */
  21. protected static $defaultDescription = 'Send newsletter';
  22. private EntityManagerInterface $em;
  23. private Util $mailer;
  24. public function __construct(EntityManagerInterface $em, Util $mailer)
  25. {
  26. parent::__construct();
  27. $this->em = $em;
  28. $this->mailer = $mailer;
  29. }
  30. protected function configure()
  31. {
  32. $this->addOption('content', null, InputOption::VALUE_OPTIONAL)
  33. ->addOption('test', null, InputOption::VALUE_OPTIONAL)
  34. ->addOption('all', null, InputOption::VALUE_NONE);
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output)
  37. {
  38. ini_set("memory_limit", "-1");
  39. set_time_limit(0);
  40. if (! $input->getOption('test') && ! $input->getOption('all')) {
  41. $output->writeln('<error>Podaj e-mail testowy</error>');
  42. return self::FAILURE;
  43. }
  44. if ($input->getOption('content')) {
  45. $content = $this->em->find(NewsletterContent::class, $input->getOption('content'));
  46. if ($content) {
  47. $this->process($content, $input->getOption('test'), $output);
  48. } else {
  49. $output->writeln('<error>Nie istnieje</error>');
  50. }
  51. } else {
  52. foreach ($this->em->createQuery('select c from w3desNewsletterBundle:NewsletterContent c where c.sendAt is not null and (c.sent != :send or c.sent is null)')->execute([
  53. 'send' => true
  54. ]) as $cnt) {
  55. $this->process($cnt, $input->getOption('test'), $output);
  56. }
  57. }
  58. return self::SUCCESS;
  59. }
  60. protected function process(NewsletterContent $content, $email = null, OutputInterface $output)
  61. {
  62. $output->writeln('<info>WysyƂka ' . $content->getTitle() . ':</info>');
  63. $this->mailer
  64. ->send($content, $email, $output);
  65. if ($email == null) {
  66. $content->setSent(true);
  67. $this->em->persist($content);
  68. $this->em->flush();
  69. }
  70. }
  71. }