Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 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;
11
 
12
use Laminas\Log\LoggerInterface;
13
use LeadersLinked\Mapper\EmailMapper;
14
use PHPMailer\PHPMailer\PHPMailer;
15
use LeadersLinked\Model\Email;
16
use Laminas\Mvc\I18n\Translator;
17
use LeadersLinked\Cache\CacheInterface;
18
 
19
class ProcessQueueUserDeletedCommand extends Command
20
{
21
    /**
22
     *
23
     * @var \Laminas\Db\Adapter\AdapterInterface
24
     */
25
    private $adapter;
26
 
27
    /**
28
     *
29
     * @var \LeadersLinked\Cache\CacheInterface
30
     */
31
    private $cache;
32
 
33
 
34
    /**
35
     *
36
     * @var \Laminas\Log\LoggerInterface
37
     */
38
    private $logger;
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
45
 
46
 
47
    /**
48
     *
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
59
     * @param array $config
60
     * @param \Laminas\Mvc\I18n\Translator $translator
61
     */
62
    public function __construct($adapter, $cache, $logger, $config, $translator)
63
    {
64
        $this->adapter      = $adapter;
65
        $this->cache        = $cache;
66
        $this->logger       = $logger;
67
        $this->config       = $config;
68
        $this->translator   = $translator;
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
                $result = $phpMailer->send();
158
 
159
                if($result) {
160
                    $emailCompleted++;
161
                    $email->status = Email::STATUS_COMPLETED;
162
                } else {
163
                    if($email->tried == 2) {
164
                        $emailError++;
165
                        $email->status = Email::STATUS_ERROR;
166
                    }
167
                    $email->tried++;
168
 
169
                }
170
                $emailMapper->update($email);
171
 
172
 
173
            }
174
        }
175
 
176
        $output->writeln('Email con Errores descartados: ' . $emailError);
177
        $output->writeln('Email enviados correctamente:'  . $emailCompleted);
178
 
179
        $output->writeln('Fin del proceso de la cola de Email');
180
 
181
        return 0;
182
    }
183
 
184
 
185
 
186
}