Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php

declare(strict_types=1);

namespace LeadersLinked\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Laminas\Db\Adapter\AdapterInterface;

use Laminas\Log\LoggerInterface;
use LeadersLinked\Mapper\EmailMapper;
use PHPMailer\PHPMailer\PHPMailer;
use LeadersLinked\Model\Email;
use Laminas\Mvc\I18n\Translator;
use LeadersLinked\Cache\CacheInterface;

class ProcessQueueUserDeletedCommand extends Command
{
    /**
     *
     * @var \Laminas\Db\Adapter\AdapterInterface
     */
    private $adapter;
    
    /**
     *
     * @var \LeadersLinked\Cache\CacheInterface
     */
    private $cache;
    
    
    /**
     *
     * @var \Laminas\Log\LoggerInterface
     */
    private $logger;
    
    /**
     *
     * @var array
     */
    private $config;
    
    
    /**
     *
     * @var \Laminas\Mvc\I18n\Translator
     */
    private $translator;
    
    
    /**
     *
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
     * @param \LeadersLinked\Cache\CacheInterface $cache
     * @param \Laminas\Log\LoggerInterface
     * @param array $config
     * @param \Laminas\Mvc\I18n\Translator $translator
     */
    public function __construct($adapter, $cache, $logger, $config, $translator)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
        $this->translator   = $translator;
        
        parent::__construct();
    }

    
    protected function execute(InputInterface $input, OutputInterface $output) : int
    {
        
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
        if($sandbox) {
            $batch_size     = $this->config['leaderslinked.email.sandbox_batch_size'];
            $from_address   = $this->config['leaderslinked.email.sandbox_from_address'];
            $from_name      = $this->config['leaderslinked.email.sandbox_from_name'];
            $host           = $this->config['leaderslinked.email.sandbox_host'];
            $port           = $this->config['leaderslinked.email.sandbox_port'];
            $username       = $this->config['leaderslinked.email.sandbox_username'];
            $password       = $this->config['leaderslinked.email.sandbox_password'];
            
        } else {
            $batch_size     = $this->config['leaderslinked.email.production_batch_size'];
            $from_address   = $this->config['leaderslinked.email.production_from_address'];
            $from_name      = $this->config['leaderslinked.email.production_from_name'];
            $host           = $this->config['leaderslinked.email.production_host'];
            $port           = $this->config['leaderslinked.email.production_port'];
            $username       = $this->config['leaderslinked.email.production_username'];
            $password       = $this->config['leaderslinked.email.production_password'];
        }

        echo 'Username : ' . $username . PHP_EOL;
        echo 'Password : ' . $password . PHP_EOL;
        echo 'Host : ' . $host . PHP_EOL;
        echo 'Port : ' . $port . PHP_EOL;

        
        
        $output->writeln('Inicio del proceso de la cola de Email');

        $emailCompleted = 0;
        $emailError = 0;
        
        $emailMapper = EmailMapper::getInstance($this->adapter);
        $emails = $emailMapper->fetchBatch($batch_size);

      
        
        if($emails) {
            foreach($emails as $email)
            {
                $content = json_decode($email->content, true);
                
                $to_address = $content['to_address'];
                $to_name    = $content['to_name'];
                $cc         = $content['cc'];
                $bcc        = $content['bcc'];
                $subject    = $content['subject'];
                $message    = $content['message'];
                

                
                $phpMailer = new PHPMailer();
                $phpMailer->isSMTP();
                
                $phpMailer->addAddress($to_address, $to_name);
                if($cc) {
                    foreach($cc as $address => $name) {
                        $phpMailer->addCC($address, $name);
                    }
                }
                if($bcc) {
                    foreach($bcc as $address => $name) {
                        $phpMailer->addBCC($address, $name);
                    }
                }
                
                $phpMailer->setFrom($from_address, $from_name);
                $phpMailer->SMTPDebug    = false; 
                $phpMailer->Host         = $host;
                $phpMailer->Port         = $port;
                $phpMailer->IsHTML(true);
                $phpMailer->SMTPAuth    = true;
                $phpMailer->SMTPSecure   = 'tls';
                $phpMailer->SMTPAuth     = true;
                $phpMailer->Username     = $username;
                $phpMailer->Password     = $password;
                $phpMailer->Subject      = $subject;
                $phpMailer->Body         = $message;
                $phpMailer->AltBody      = $message;
                
                /*
                $mail->SMTPOptions = [
                    'ssl' => [
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    ]
                ];
                */
                
                //print_r($phpMailer);
                
                $result = $phpMailer->send();
                
                if($result) {
                    $emailCompleted++;
                    $email->status = Email::STATUS_COMPLETED;
                } else {
                    if($email->tried == 2) {
                        $emailError++;
                        $email->status = Email::STATUS_ERROR;
                    }
                    $email->tried++;
                    
                }
                $emailMapper->update($email);
                
                
            }
        }
        
        $output->writeln('Email con Errores descartados: ' . $emailError);
        $output->writeln('Email enviados correctamente:'  . $emailCompleted);
        
        $output->writeln('Fin del proceso de la cola de Email');
        
        return 0;
    }
    
    
    
}