-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJangoMail.php
More file actions
266 lines (227 loc) · 5.71 KB
/
JangoMail.php
File metadata and controls
266 lines (227 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
namespace Netpeople\JangoMailBundle;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManager;
use Netpeople\JangoMailBundle\CampaignSending;
use Netpeople\JangoMailBundle\Emails\EmailInterface;
use Netpeople\JangoMailBundle\Entity\EmailLogs;
use Netpeople\JangoMailBundle\Exception\JangoMailException;
use Netpeople\JangoMailBundle\TransactionalSending;
use Psr\Log\LoggerInterface;
use SoapClient;
/**
* Description of JangoMail
*
* @author manuel
*/
class JangoMail
{
/**
*
* @var array
*/
protected $config = array();
/**
*
* @var CampaignSending
*/
protected $campaignSending;
/**
*
* @var TransactionalSending
*/
protected $transactionalSending;
/**
*
* @var SoapClient
*/
protected $jangoClient;
/**
*
* @var string
*/
protected $error;
/**
*
* @var Registry
*/
protected $registry;
/**
* @var LoggerInterface
*/
protected $logger;
public function __construct($config, Registry $registry, LoggerInterface $logger = null)
{
$this->setConfig($config)->registry = $registry;
$this->logger = $logger;
}
/**
*
* @return SoapClient
*/
public function getJangoInstance()
{
if (!($this->jangoClient instanceof SoapClient)) {
$this->jangoClient = new SoapClient($this->config['url_jango']);
//var_dump($this->jangoClient,$this->config['url_jango']);die;
}
return $this->jangoClient;
}
/**
*
* @return EntityManager
*/
public function getManager()
{
return $this->registry->getManager();
}
/**
*
* @param array $config
*
* @return \Netpeople\JangoMailBundle\JangoMail
*/
public function setConfig($config)
{
$this->config = $config;
return $this;
}
/**
*
* @return array
*/
public function getConfig($name = null)
{
if ($name) {
return array_key_exists($name, $this->config) ?
$this->config[$name] : null;
}
return $this->config;
}
/**
*
* @return CampaignSending
*/
public function getCampaign()
{
return $this->campaignSending;
}
/**
*
* @param CampaignSending $campaign
*
* @return \Netpeople\JangoMailBundle\JangoMail
*/
public function setCampaign(CampaignSending $campaign)
{
$this->campaignSending = $campaign;
return $this;
}
/**
*
* @return TransactionalSending
*/
public function getTransactional()
{
return $this->transactionalSending;
}
public function setTransactional(TransactionalSending $transactional)
{
$this->transactionalSending = $transactional;
return $this;
}
/**
*
* @param Emails\EmailInterface $email
*
* @return boolean
*/
public function send(Emails\EmailInterface $email)
{
/*
* primero verificamos si tiene grupos establecidos, si es así
* enviamos el correo como campaña
*/
if (count($email->getGroups())) {
return $this->getCampaign()->setEmail($email)->send();
}
/*
* Si no tiene grupos y tiene recipientes, enviamos el correo como transactional
*/
if (count($email->getRecipients())) {
return $this->getTransactional()->setEmail($email)->send();
}
/*
* Si llegamos acá es porque el email no tiene ni grupos ni recipientes.
*/
throw new JangoMailException("Estas Intentando enviar un correo que no tiene ni grupos ni recipientes asignados.");
}
/**
*
* @param string $error
*
* @return \Netpeople\JangoMailBundle\JangoMail
*/
public function setError($error)
{
$this->error = $error;
return $this;
}
/**
*
* @return string
*/
public function getError()
{
return $this->error;
}
public function addEmailLog(EmailInterface $email, $result)
{
if ($this->getConfig('enable_log') == true) {
$log = new EmailLogs();
$log->setEmail($email)->setResult($result)
->setError($this->getError())
->setDatetime(new DateTime('now'));
$eManager = $this->getManager();
$eManager->persist($log);
$eManager->flush();
}
return $this;
}
public function getOptionsString(EmailInterface $email, array $options = array())
{
$options = ((array) $email->getOptions()) + $options;
$opts = array();
foreach ($options as $index => $value) {
if ($value) {
$opts[] = "$index=$value";
}
}
return join(',', $opts);
}
public function prepare(array $arguments = array())
{
return array(
'Username' => $this->getConfig('username'),
'Password' => $this->getConfig('password'),
) + $arguments;
}
public function call($method, array $arguments = array())
{
if ($this->logger) {
$this->logger->debug(sprintf("[Jango] Calling to %s", $method), $arguments);
}
try {
$result = $this->getJangoInstance()
->{$method}($this->prepare($arguments));
} catch (\Exception $e) {
if ($this->logger) {
$this->logger->error(sprintf("[Jango ERROR] Calling to %s, with errror: %s"
, $method, $e->getMessage()), $arguments);
}
throw $e;
}
return $result;
}
}