src/Application/Internit/LeadBundle/Service/ResendLeadWorkerCommand.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Application\Internit\LeadBundle\Service;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use App\Application\Internit\LeadBundle\Entity\LeadErrorLog;
  7. class ResendLeadWorkerCommand extends ContainerAwareCommand
  8. {
  9.     protected function configure()
  10.     {
  11.         $this
  12.             ->setName('resend:lead:worker')
  13.             ->setDescription('Consome fila de leads e envia e-mail');
  14.     }
  15.     protected function execute(InputInterface $inputOutputInterface $output)
  16.     {
  17.         $conn $this->getContainer()->get('doctrine')->getConnection();
  18.         $entityManager $this->getContainer()->get('doctrine')->getManager();
  19.         $rows $conn->fetchAll("SELECT * FROM lead_queue WHERE status = 'pendente'");
  20.         foreach ($rows as $row) {
  21.             $mailer1 $this->getContainer()->get('admin.mail.service');
  22.             $mailer2 $this->getContainer()->get('admin.mail.service2');
  23.             $lead $entityManager->getRepository('ApplicationInternitLeadBundle:Lead')->find($row['lead_id']);
  24.             $copyEmails explode(','$row['copy_emails']);
  25.             try {
  26.                 if (!$lead) {
  27.                     $conn->update('lead_queue', ['status' => 'falhou''updated_at' => date('Y-m-d H:i:s')], ['id' => $row['id']]);
  28.                     continue;
  29.                 }
  30.                 $html $this->getContainer()->get('templating')->render(
  31.                     '@ApplicationInternit/SettingBundle/Resources/views/Mail/emailLead.html.twig',
  32.                     ['data' => $lead'corretor' => $row['broker_name']]
  33.                 );
  34.                 $enviado false;
  35.                 // Primeira tentativa
  36.                 try {
  37.                     $mailer1->setMessage()
  38.                         ->setTo($row['broker_email'] ?? $lead->getProduct()->getEmail())
  39.                         ->setCC($copyEmails)
  40.                         ->setSubject("[leadcalper] " $lead->getProduct()->getName() .  ' - ' $lead->getGroup() . ' - ' $row['broker_name'])
  41.                         ->setBody(
  42.                             $html,
  43.                             'text/html'
  44.                         );
  45.                     $mailer1->send();
  46.                     $lead->setSendStatus('Sim 1');
  47.                     $entityManager->persist($lead);
  48.                     $entityManager->flush();
  49.                     $enviado true;
  50.                 } catch (\Exception $e1) {
  51.                     // Segunda tentativa
  52.                     try {
  53.                         $mailer2->setMessage()
  54.                             ->setTo($row['broker_email'] ?? $lead->getProduct()->getEmail())
  55.                             ->setCc($copyEmails)
  56.                             ->setSubject("[leadcalper] " $lead->getProduct()->getName() . ' - ' $lead->getGroup() . ' - ' $row['broker_name'])
  57.                             ->setBody($html'text/html')
  58.                             ->send();
  59.                         $lead->setSendStatus('Sim 2');
  60.                         $entityManager->persist($lead);
  61.                         $entityManager->flush();
  62.                         $enviado true;
  63.                     } catch (\Exception $e2) {
  64.                         // Ambos falharam
  65.                         $lead->setSendStatus('Não');
  66.                         $conn->update('lead_queue', [
  67.                             'status' => 'falhou',
  68.                             'updated_at' => date('Y-m-d H:i:s')
  69.                         ], ['id' => $row['id']]);
  70.                             $log = new LeadErrorLog();
  71.                             $log->setLead($lead ?? null);
  72.                             $entityManager->persist($lead);
  73.                             $log->setErrorMessage('Falha na tentativa de reenvio de lead, os 2 emails falharam.');
  74.                             $log->setErrorType('Erro de envio de email');
  75.                             $log->setCreatedAt(new \DateTime());
  76.                             $entityManager->persist($log);
  77.                             $entityManager->flush();
  78.                         $output->writeln("Lead {$row['lead_id']} falhou nos dois envios.");
  79.                         continue;
  80.                     }
  81.                 }
  82.                 if ($enviado) {
  83.                     $conn->delete('lead_queue', ['id' => $row['id']]);
  84.                     $output->writeln("Lead {$row['lead_id']} enviado com sucesso.");
  85.                 }
  86.             } catch (\Exception $e) {
  87.                 $lead->setSendStatus('Não');
  88.                 $entityManager->persist($lead);
  89.                 $entityManager->flush();
  90.                 $conn->update('lead_queue', ['status' => 'falhou''updated_at' => date('Y-m-d H:i:s')], ['id' => $row['id']]);
  91.                 $output->writeln("Erro inesperado no lead ID {$row['lead_id']}");
  92.             }
  93.         }
  94.         $output->writeln("Processamento finalizado.");
  95.     }
  96. }