<commit_before><?php
namespace Vidal\MainBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class DigestCommand extends ContainerAwareCommand
{
	protected function configure()
	{
		$this->setName('vidal:digest')
			->setDescription('Send digest to users')
			->addOption('test', null, InputOption::VALUE_NONE, 'Send digest to manager e-mails')
			->addOption('stop', null, InputOption::VALUE_NONE, 'Stop sending digests')
			->addOption('clean', null, InputOption::VALUE_NONE, 'Clean log app/logs/digest_sent.txt')
			->addOption('all', null, InputOption::VALUE_NONE, 'Send digest to every subscribed user')
			->addOption('me', null, InputOption::VALUE_NONE, 'Send digest to <EMAIL>')
			->addOption('local', null, InputOption::VALUE_NONE, 'Send digest from <EMAIL>');
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		# снимаем ограничение времени выполнения скрипта (в safe-mode не работает)
		set_time_limit(0);

		# опции не указаны - выводим мануал
		if (!$input->getOption('test') && !$input->getOption('clean') && !$input->getOption('all') && !$input->getOption('me') && !$input->getOption('me')) {
			$output->writeln('=> Error: uncorrect syntax. READ BELOW');
			$output->writeln('$ php app/console evrika:digest --test');
			$output->writeln('$ php app/console evrika:digest --stop');
			$output->writeln('$ php app/console evrika:digest --clean');
			$output->writeln('$ php app/console evrika:digest --all');
			$output->writeln('$ php app/console evrika:digest --me');
			$output->writeln('$ php app/console evrika:digest --me --local');
			return false;
		}

		$container = $this->getContainer();
		$em        = $container->get('doctrine')->getManager();
		$digest    = $em->getRepository('VidalMainBundle:Digest')->get();

		# --stop   остановка рассылки дайджеста
		if ($input->getOption('stop')) {
			$digest->setProgress(false);
			$em->flush();
			$output->writeln('=> digest STOPPED');
			return true;
		}

		# если рассылка уже идет - возвращаем false
		if ($digest->getProgress()) {
			$output->writeln("=> ERROR: digest IN PROGRESS");
			return false;
		}

		# рассылка всем подписанным врачам
		if ($input->getOption('all')) {
			$output->writeln("Sending: in progress to ALL subscribed users...");
			$digest->setProgress(true);
			$this->sendToAll();
		}

		# рассылка нашим менеджерам
		if ($input->getOption('test')) {
			$raw      = explode(';', $digest->getEmails());
			$emails[] = array();

			foreach ($raw as $email) {
				$emails[] = trim($email);
			}

			$output->writeln("Sending: in progress to managers: " . implode(', ', $emails));
			$this->sendTo($emails);
		}

		# отправить самому себе
		if ($input->getOption('me')) {
			$output->writeln("Sending: in progress to <EMAIL>");
			$this->sendTo(array('<EMAIL>'), $input->getOption('local'));
		}

		if ($input->getOption('clean')) {
			$em->createQuery('UPDATE VidalMainBundle:User u SET u.send=0 WHERE u.send=1')->execute();
			$output->writeln('Cleaned sent flag of users!');
		}

		return true;
	}

	private function sendToAll()
	{
		$container  = $this->getContainer();
		$em         = $container->get('doctrine')->getManager();
		$templating = $container->get('templating');
		$digest     = $em->getRepository('VidalMainBundle:Digest')->get();

		$users = $em->createQuery("
			SELECT u.username, u.id, DATE_FORMAT(u.created, '%Y-%m-%d_%H:%i:%s') as created, u.firstName
			FROM VidalMainBundle:User u
			ORDER BY u.id ASC
		")->getResult();

		$subject     = $digest->getSubject();
		$template1   = $templating->render('VidalMainBundle:Digest:template1.html.twig', array('digest' => $digest));
		$updateQuery = $em->createQuery('UPDATE VidalMainBundle:User u SET u.send=1 WHERE u.id = :id');

		# рассылка
		$step = 40;

		for ($i = 0, $c = count($users); $i < $c; $i = $i + $step) {
			$users100 = array_slice($users, $i, $step);

			foreach ($users100 as $user) {
				$template2 = $templating->render('VidalMainBundle:Digest:template2.html.twig', array('user' => $user));
				$template  = $template1 . $template2;

				$this->send($user['username'], $user['firstName'], $template, $subject);
				$updateQuery->setParameter('id', $user['id'])->execute();
			}

			sleep(60);
		}
	}

	/**
	 * Рассылка по массиву почтовых адресов без логирования
	 *
	 * @param array $emails
	 */
	private function sendTo(array $emails, $local = false)
	{
		$container  = $this->getContainer();
		$em         = $container->get('doctrine')->getManager();
		$templating = $container->get('templating');
		$digest     = $em->getRepository('VidalMainBundle:Digest')->get();

		$users = $em->createQuery("
			SELECT u.username, u.id, DATE_FORMAT(u.created, '%Y-%m-%d_%H:%i:%s') as created, u.firstName
			FROM VidalMainBundle:User u
			WHERE u.username IN (:emails)
		")->setParameter('emails', $emails)
			->getResult();

		$subject   = $digest->getSubject();
		$template1 = $templating->render('VidalMainBundle:Digest:template1.html.twig', array('digest' => $digest));

		foreach ($users as $user) {
			$template2 = $templating->render('VidalMainBundle:Digest:template2.html.twig', array('user' => $user));
			$template  = $template1 . $template2;

			$this->send($user['username'], $user['firstName'], $template, $subject, $local);
		}
	}

	public function send($email, $to, $body, $subject, $local = false)
	{
		$mail = new \PHPMailer();

		$mail->isSMTP();
		$mail->isHTML(true);
		$mail->CharSet  = 'UTF-8';
		$mail->From     = '<EMAIL>';
		$mail->FromName = 'Портал «Evrika.ru»';
		$mail->Subject  = $subject;
		$mail->Host     = '127.0.0.1';
		$mail->Body     = $body;
		$mail->addAddress($email, $to);
		$mail->addCustomHeader('Precedence', 'bulk');

		if ($local) {
			$mail->Host       = 'smtp.gmail.com';
			$mail->From       = '<EMAIL>';
			$mail->SMTPSecure = 'ssl';
			$mail->Port       = 465;
			$mail->SMTPAuth   = true;
			$mail->Username   = '<EMAIL>';
			$mail->Password   = '<PASSWORD>';
		}

		$result = $mail->send();<commit_msg>updates @ delivery<commit_after><?php
namespace Vidal\MainBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class DigestCommand extends ContainerAwareCommand
{
	protected function configure()
	{
		$this->setName('vidal:digest')
			->setDescription('Send digest to users')
			->addOption('test', null, InputOption::VALUE_NONE, 'Send digest to manager e-mails')
			->addOption('stop', null, InputOption::VALUE_NONE, 'Stop sending digests')
			->addOption('clean', null, InputOption::VALUE_NONE, 'Clean log app/logs/digest_sent.txt')
			->addOption('all', null, InputOption::VALUE_NONE, 'Send digest to every subscribed user')
			->addOption('me', null, InputOption::VALUE_NONE, 'Send digest to <EMAIL>')
			->addOption('local', null, InputOption::VALUE_NONE, 'Send digest from <EMAIL>');
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		# снимаем ограничение времени выполнения скрипта (в safe-mode не работает)
		set_time_limit(0);

		# опции не указаны - выводим мануал
		if (!$input->getOption('test') && !$input->getOption('clean') && !$input->getOption('all') && !$input->getOption('me') && !$input->getOption('me')) {
			$output->writeln('=> Error: uncorrect syntax. READ BELOW');
			$output->writeln('$ php app/console evrika:digest --test');
			$output->writeln('$ php app/console evrika:digest --stop');
			$output->writeln('$ php app/console evrika:digest --clean');
			$output->writeln('$ php app/console evrika:digest --all');
			$output->writeln('$ php app/console evrika:digest --me');
			$output->writeln('$ php app/console evrika:digest --me --local');
			return false;
		}

		$container = $this->getContainer();
		$em        = $container->get('doctrine')->getManager();
		$digest    = $em->getRepository('VidalMainBundle:Digest')->get();

		# --stop   остановка рассылки дайджеста
		if ($input->getOption('stop')) {
			$digest->setProgress(false);
			$em->flush();
			$output->writeln('=> digest STOPPED');
			return true;
		}

		# если рассылка уже идет - возвращаем false
		if ($digest->getProgress()) {
			$output->writeln("=> ERROR: digest IN PROGRESS");
			return false;
		}

		# рассылка всем подписанным врачам
		if ($input->getOption('all')) {
			$output->writeln("Sending: in progress to ALL subscribed users...");
			$digest->setProgress(true);
			$this->sendToAll();
		}

		# рассылка нашим менеджерам
		if ($input->getOption('test')) {
			$raw      = explode(';', $digest->getEmails());
			$emails[] = array();

			foreach ($raw as $email) {
				$emails[] = trim($email);
			}

			$output->writeln("Sending: in progress to managers: " . implode(', ', $emails));
			$this->sendTo($emails);
		}

		# отправить самому себе
		if ($input->getOption('me')) {
			$output->writeln("Sending: in progress to <EMAIL>");
			$this->sendTo(array('<EMAIL>'), $input->getOption('local'));
		}

		if ($input->getOption('clean')) {
			$em->createQuery('UPDATE VidalMainBundle:User u SET u.send=0 WHERE u.send=1')->execute();
			$output->writeln('Cleaned sent flag of users!');
		}

		return true;
	}

	private function sendToAll()
	{
		$container  = $this->getContainer();
		$em         = $container->get('doctrine')->getManager();
		$templating = $container->get('templating');
		$digest     = $em->getRepository('VidalMainBundle:Digest')->get();

		$users = $em->createQuery("
			SELECT u.username, u.id, DATE_FORMAT(u.created, '%Y-%m-%d_%H:%i:%s') as created, u.firstName
			FROM VidalMainBundle:User u
			ORDER BY u.id ASC
		")->getResult();

		$subject     = $digest->getSubject();
		$template1   = $templating->render('VidalMainBundle:Digest:template1.html.twig', array('digest' => $digest));
		$updateQuery = $em->createQuery('UPDATE VidalMainBundle:User u SET u.send=1 WHERE u.id = :id');

		# рассылка
		$step = 40;

		for ($i = 0, $c = count($users); $i < $c; $i = $i + $step) {
			$users100 = array_slice($users, $i, $step);

			foreach ($users100 as $user) {
				$template2 = $templating->render('VidalMainBundle:Digest:template2.html.twig', array('user' => $user));
				$template  = $template1 . $template2;

				$this->send($user['username'], $user['firstName'], $template, $subject);
				$updateQuery->setParameter('id', $user['id'])->execute();
			}

			sleep(60);
		}
	}

	/**
	 * Рассылка по массиву почтовых адресов без логирования
	 *
	 * @param array $emails
	 */
	private function sendTo(array $emails, $local = false)
	{
		$container  = $this->getContainer();
		$em         = $container->get('doctrine')->getManager();
		$templating = $container->get('templating');
		$digest     = $em->getRepository('VidalMainBundle:Digest')->get();

		$users = $em->createQuery("
			SELECT u.username, u.id, DATE_FORMAT(u.created, '%Y-%m-%d_%H:%i:%s') as created, u.firstName
			FROM VidalMainBundle:User u
			WHERE u.username IN (:emails)
		")->setParameter('emails', $emails)
			->getResult();

		$subject   = $digest->getSubject();
		$template1 = $templating->render('VidalMainBundle:Digest:template1.html.twig', array('digest' => $digest));

		foreach ($users as $user) {
			$template2 = $templating->render('VidalMainBundle:Digest:template2.html.twig', array('user' => $user));
			$template  = $template1 . $template2;

			$this->send($user['username'], $user['firstName'], $template, $subject, $local);
		}
	}

	public function send($email, $to, $body, $subject, $local = false)
	{
		$mail = new \PHPMailer();

		$mail->isSMTP();
		$mail->isHTML(true);
		$mail->CharSet  = 'UTF-8';
		$mail->From     = '<EMAIL>';
		$mail->FromName = 'Портал «Vidal.ru»';
		$mail->Subject  = $subject;
		$mail->Host     = '127.0.0.1';
		$mail->Body     = $body;
		$mail->addAddress($email, $to);
		$mail->addCustomHeader('Precedence', 'bulk');

		if ($local) {
			$mail->Host       = 'smtp.gmail.com';
			$mail->From       = '<EMAIL>';
			$mail->SMTPSecure = 'ssl';
			$mail->Port       = 465;
			$mail->SMTPAuth   = true;
			$mail->Username   = '<EMAIL>';
			$mail->Password   = '<PASSWORD>';
		}

		$result = $mail->send();
{'fraction_non_alphanumeric': 0.19368642015178428, 'fraction_numerical': 0.005974487324398515, 'mean_word_length': 4.228788518362178, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 219, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6978681', 'n_tokens_mistral': 4796, 'n_tokens_neox': 4321, 'n_words': 860}