Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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