| 1979 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           namespace LeadersLinked\Command;
  | 
        
        
            | 
            | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           use Symfony\Component\Console\Command\Command;
  | 
        
        
            | 
            | 
           8 | 
           use Symfony\Component\Console\Input\InputInterface;
  | 
        
        
            | 
            | 
           9 | 
           use Symfony\Component\Console\Output\OutputInterface;
  | 
        
        
            | 
            | 
           10 | 
           use Laminas\Db\Adapter\AdapterInterface;
  | 
        
        
           | 6849 | 
           efrain | 
           11 | 
              | 
        
        
           | 1979 | 
           efrain | 
           12 | 
           use Laminas\Log\LoggerInterface;
  | 
        
        
            | 
            | 
           13 | 
           use LeadersLinked\Mapper\EmailMapper;
  | 
        
        
            | 
            | 
           14 | 
           use PHPMailer\PHPMailer\PHPMailer;
  | 
        
        
            | 
            | 
           15 | 
           use LeadersLinked\Model\Email;
  | 
        
        
           | 6866 | 
           efrain | 
           16 | 
           use Laminas\Mvc\I18n\Translator;
  | 
        
        
            | 
            | 
           17 | 
           use LeadersLinked\Cache\CacheInterface;
  | 
        
        
           | 1979 | 
           efrain | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           class ProcessQueueUserDeletedCommand extends Command
  | 
        
        
            | 
            | 
           20 | 
           {
  | 
        
        
            | 
            | 
           21 | 
               /**
  | 
        
        
            | 
            | 
           22 | 
                *
  | 
        
        
           | 6866 | 
           efrain | 
           23 | 
                * @var \Laminas\Db\Adapter\AdapterInterface
  | 
        
        
           | 1979 | 
           efrain | 
           24 | 
                */
  | 
        
        
            | 
            | 
           25 | 
               private $adapter;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
               /**
  | 
        
        
            | 
            | 
           28 | 
                *
  | 
        
        
           | 6866 | 
           efrain | 
           29 | 
                * @var \LeadersLinked\Cache\CacheInterface
  | 
        
        
           | 1979 | 
           efrain | 
           30 | 
                */
  | 
        
        
           | 6866 | 
           efrain | 
           31 | 
               private $cache;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
               /**
  | 
        
        
            | 
            | 
           35 | 
                *
  | 
        
        
            | 
            | 
           36 | 
                * @var \Laminas\Log\LoggerInterface
  | 
        
        
            | 
            | 
           37 | 
                */
  | 
        
        
           | 1979 | 
           efrain | 
           38 | 
               private $logger;
  | 
        
        
           | 6866 | 
           efrain | 
           39 | 
              | 
        
        
           | 1979 | 
           efrain | 
           40 | 
               /**
  | 
        
        
            | 
            | 
           41 | 
                *
  | 
        
        
            | 
            | 
           42 | 
                * @var array
  | 
        
        
            | 
            | 
           43 | 
                */
  | 
        
        
            | 
            | 
           44 | 
               private $config;
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
               /**
  | 
        
        
            | 
            | 
           48 | 
                *
  | 
        
        
           | 6866 | 
           efrain | 
           49 | 
                * @var \Laminas\Mvc\I18n\Translator
  | 
        
        
            | 
            | 
           50 | 
                */
  | 
        
        
            | 
            | 
           51 | 
               private $translator;
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
               /**
  | 
        
        
            | 
            | 
           55 | 
                *
  | 
        
        
            | 
            | 
           56 | 
                * @param \Laminas\Db\Adapter\AdapterInterface $adapter
  | 
        
        
            | 
            | 
           57 | 
                * @param \LeadersLinked\Cache\CacheInterface $cache
  | 
        
        
            | 
            | 
           58 | 
                * @param \Laminas\Log\LoggerInterface
  | 
        
        
           | 1979 | 
           efrain | 
           59 | 
                * @param array $config
  | 
        
        
           | 6866 | 
           efrain | 
           60 | 
                * @param \Laminas\Mvc\I18n\Translator $translator
  | 
        
        
           | 1979 | 
           efrain | 
           61 | 
                */
  | 
        
        
           | 6866 | 
           efrain | 
           62 | 
               public function __construct($adapter, $cache, $logger, $config, $translator)
  | 
        
        
           | 1979 | 
           efrain | 
           63 | 
               {
  | 
        
        
            | 
            | 
           64 | 
                   $this->adapter      = $adapter;
  | 
        
        
           | 6866 | 
           efrain | 
           65 | 
                   $this->cache        = $cache;
  | 
        
        
           | 1979 | 
           efrain | 
           66 | 
                   $this->logger       = $logger;
  | 
        
        
            | 
            | 
           67 | 
                   $this->config       = $config;
  | 
        
        
           | 6866 | 
           efrain | 
           68 | 
                   $this->translator   = $translator;
  | 
        
        
           | 1979 | 
           efrain | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
                   parent::__construct();
  | 
        
        
            | 
            | 
           71 | 
               }
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
               protected function execute(InputInterface $input, OutputInterface $output) : int
  | 
        
        
            | 
            | 
           75 | 
               {
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   $sandbox = $this->config['leaderslinked.runmode.sandbox'];
  | 
        
        
            | 
            | 
           78 | 
                   if($sandbox) {
  | 
        
        
            | 
            | 
           79 | 
                       $batch_size     = $this->config['leaderslinked.email.sandbox_batch_size'];
  | 
        
        
            | 
            | 
           80 | 
                       $from_address   = $this->config['leaderslinked.email.sandbox_from_address'];
  | 
        
        
            | 
            | 
           81 | 
                       $from_name      = $this->config['leaderslinked.email.sandbox_from_name'];
  | 
        
        
            | 
            | 
           82 | 
                       $host           = $this->config['leaderslinked.email.sandbox_host'];
  | 
        
        
            | 
            | 
           83 | 
                       $port           = $this->config['leaderslinked.email.sandbox_port'];
  | 
        
        
            | 
            | 
           84 | 
                       $username       = $this->config['leaderslinked.email.sandbox_username'];
  | 
        
        
            | 
            | 
           85 | 
                       $password       = $this->config['leaderslinked.email.sandbox_password'];
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                   } else {
  | 
        
        
            | 
            | 
           88 | 
                       $batch_size     = $this->config['leaderslinked.email.production_batch_size'];
  | 
        
        
            | 
            | 
           89 | 
                       $from_address   = $this->config['leaderslinked.email.production_from_address'];
  | 
        
        
            | 
            | 
           90 | 
                       $from_name      = $this->config['leaderslinked.email.production_from_name'];
  | 
        
        
            | 
            | 
           91 | 
                       $host           = $this->config['leaderslinked.email.production_host'];
  | 
        
        
            | 
            | 
           92 | 
                       $port           = $this->config['leaderslinked.email.production_port'];
  | 
        
        
            | 
            | 
           93 | 
                       $username       = $this->config['leaderslinked.email.production_username'];
  | 
        
        
            | 
            | 
           94 | 
                       $password       = $this->config['leaderslinked.email.production_password'];
  | 
        
        
            | 
            | 
           95 | 
                   }
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   echo 'Username : ' . $username . PHP_EOL;
  | 
        
        
            | 
            | 
           98 | 
                   echo 'Password : ' . $password . PHP_EOL;
  | 
        
        
            | 
            | 
           99 | 
                   echo 'Host : ' . $host . PHP_EOL;
  | 
        
        
            | 
            | 
           100 | 
                   echo 'Port : ' . $port . PHP_EOL;
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                   $output->writeln('Inicio del proceso de la cola de Email');
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   $emailCompleted = 0;
  | 
        
        
            | 
            | 
           107 | 
                   $emailError = 0;
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                   $emailMapper = EmailMapper::getInstance($this->adapter);
  | 
        
        
            | 
            | 
           110 | 
                   $emails = $emailMapper->fetchBatch($batch_size);
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
                   if($emails) {
  | 
        
        
            | 
            | 
           115 | 
                       foreach($emails as $email)
  | 
        
        
            | 
            | 
           116 | 
                       {
  | 
        
        
            | 
            | 
           117 | 
                           $content = json_decode($email->content, true);
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
                           $to_address = $content['to_address'];
  | 
        
        
            | 
            | 
           120 | 
                           $to_name    = $content['to_name'];
  | 
        
        
            | 
            | 
           121 | 
                           $cc         = $content['cc'];
  | 
        
        
            | 
            | 
           122 | 
                           $bcc        = $content['bcc'];
  | 
        
        
            | 
            | 
           123 | 
                           $subject    = $content['subject'];
  | 
        
        
            | 
            | 
           124 | 
                           $message    = $content['message'];
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                           $phpMailer = new PHPMailer();
  | 
        
        
            | 
            | 
           129 | 
                           $phpMailer->isSMTP();
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
                           $phpMailer->addAddress($to_address, $to_name);
  | 
        
        
            | 
            | 
           132 | 
                           if($cc) {
  | 
        
        
            | 
            | 
           133 | 
                               foreach($cc as $address => $name) {
  | 
        
        
            | 
            | 
           134 | 
                                   $phpMailer->addCC($address, $name);
  | 
        
        
            | 
            | 
           135 | 
                               }
  | 
        
        
            | 
            | 
           136 | 
                           }
  | 
        
        
            | 
            | 
           137 | 
                           if($bcc) {
  | 
        
        
            | 
            | 
           138 | 
                               foreach($bcc as $address => $name) {
  | 
        
        
            | 
            | 
           139 | 
                                   $phpMailer->addBCC($address, $name);
  | 
        
        
            | 
            | 
           140 | 
                               }
  | 
        
        
            | 
            | 
           141 | 
                           }
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
                           $phpMailer->setFrom($from_address, $from_name);
  | 
        
        
            | 
            | 
           144 | 
                           $phpMailer->SMTPDebug    = false;
  | 
        
        
            | 
            | 
           145 | 
                           $phpMailer->Host         = $host;
  | 
        
        
            | 
            | 
           146 | 
                           $phpMailer->Port         = $port;
  | 
        
        
            | 
            | 
           147 | 
                           $phpMailer->IsHTML(true);
  | 
        
        
            | 
            | 
           148 | 
                           $phpMailer->SMTPAuth    = true;
  | 
        
        
            | 
            | 
           149 | 
                           $phpMailer->SMTPSecure   = 'tls';
  | 
        
        
            | 
            | 
           150 | 
                           $phpMailer->SMTPAuth     = true;
  | 
        
        
            | 
            | 
           151 | 
                           $phpMailer->Username     = $username;
  | 
        
        
            | 
            | 
           152 | 
                           $phpMailer->Password     = $password;
  | 
        
        
            | 
            | 
           153 | 
                           $phpMailer->Subject      = $subject;
  | 
        
        
            | 
            | 
           154 | 
                           $phpMailer->Body         = $message;
  | 
        
        
            | 
            | 
           155 | 
                           $phpMailer->AltBody      = $message;
  | 
        
        
            | 
            | 
           156 | 
              | 
        
        
            | 
            | 
           157 | 
                           /*
  | 
        
        
            | 
            | 
           158 | 
                           $mail->SMTPOptions = [
  | 
        
        
            | 
            | 
           159 | 
                               'ssl' => [
  | 
        
        
            | 
            | 
           160 | 
                                   'verify_peer' => false,
  | 
        
        
            | 
            | 
           161 | 
                                   'verify_peer_name' => false,
  | 
        
        
            | 
            | 
           162 | 
                                   'allow_self_signed' => true
  | 
        
        
            | 
            | 
           163 | 
                               ]
  | 
        
        
            | 
            | 
           164 | 
                           ];
  | 
        
        
            | 
            | 
           165 | 
                           */
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
                           //print_r($phpMailer);
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
                           $result = $phpMailer->send();
  | 
        
        
            | 
            | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
                           if($result) {
  | 
        
        
            | 
            | 
           172 | 
                               $emailCompleted++;
  | 
        
        
            | 
            | 
           173 | 
                               $email->status = Email::STATUS_COMPLETED;
  | 
        
        
            | 
            | 
           174 | 
                           } else {
  | 
        
        
            | 
            | 
           175 | 
                               if($email->tried == 2) {
  | 
        
        
            | 
            | 
           176 | 
                                   $emailError++;
  | 
        
        
            | 
            | 
           177 | 
                                   $email->status = Email::STATUS_ERROR;
  | 
        
        
            | 
            | 
           178 | 
                               }
  | 
        
        
            | 
            | 
           179 | 
                               $email->tried++;
  | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
                           }
  | 
        
        
            | 
            | 
           182 | 
                           $emailMapper->update($email);
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
              | 
        
        
            | 
            | 
           185 | 
                       }
  | 
        
        
            | 
            | 
           186 | 
                   }
  | 
        
        
            | 
            | 
           187 | 
              | 
        
        
            | 
            | 
           188 | 
                   $output->writeln('Email con Errores descartados: ' . $emailError);
  | 
        
        
            | 
            | 
           189 | 
                   $output->writeln('Email enviados correctamente:'  . $emailCompleted);
  | 
        
        
            | 
            | 
           190 | 
              | 
        
        
            | 
            | 
           191 | 
                   $output->writeln('Fin del proceso de la cola de Email');
  | 
        
        
            | 
            | 
           192 | 
              | 
        
        
            | 
            | 
           193 | 
                   return 0;
  | 
        
        
            | 
            | 
           194 | 
               }
  | 
        
        
            | 
            | 
           195 | 
              | 
        
        
            | 
            | 
           196 | 
              | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
           }
  |