Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | | Comparar con el anterior | Ultima modificación | Ver Log |

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