<?php
namespace w3des\NewsletterBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use w3des\AdminBundle\Entity\User;
use w3des\NewsletterBundle\Util;
use Symfony\Component\Console\Input\InputOption;
use w3des\NewsletterBundle\Entity\NewsletterContent;
use Symfony\Component\Console\Command\Command;
use Doctrine\ORM\EntityManagerInterface;
class SendCommand extends Command
{
/**
* @var string|null The default command name
*/
protected static $defaultName = 'admin:newsletter:send';
/**
* @var string|null The default command description
*/
protected static $defaultDescription = 'Send newsletter';
private EntityManagerInterface $em;
private Util $mailer;
public function __construct(EntityManagerInterface $em, Util $mailer)
{
parent::__construct();
$this->em = $em;
$this->mailer = $mailer;
}
protected function configure()
{
$this->addOption('content', null, InputOption::VALUE_OPTIONAL)
->addOption('test', null, InputOption::VALUE_OPTIONAL)
->addOption('all', null, InputOption::VALUE_NONE);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
ini_set("memory_limit", "-1");
set_time_limit(0);
if (! $input->getOption('test') && ! $input->getOption('all')) {
$output->writeln('<error>Podaj e-mail testowy</error>');
return self::FAILURE;
}
if ($input->getOption('content')) {
$content = $this->em->find(NewsletterContent::class, $input->getOption('content'));
if ($content) {
$this->process($content, $input->getOption('test'), $output);
} else {
$output->writeln('<error>Nie istnieje</error>');
}
} else {
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([
'send' => true
]) as $cnt) {
$this->process($cnt, $input->getOption('test'), $output);
}
}
return self::SUCCESS;
}
protected function process(NewsletterContent $content, $email = null, OutputInterface $output)
{
$output->writeln('<info>WysyĆka ' . $content->getTitle() . ':</info>');
$this->mailer
->send($content, $email, $output);
if ($email == null) {
$content->setSent(true);
$this->em->persist($content);
$this->em->flush();
}
}
}