1
|
|
|
<?php |
2
|
|
|
namespace AcMailer\Options; |
3
|
|
|
|
4
|
|
|
use AcMailer\Exception\InvalidArgumentException; |
5
|
|
|
use Zend\Mail\Transport\FileOptions; |
6
|
|
|
use Zend\Mail\Transport\SmtpOptions; |
7
|
|
|
use Zend\Mail\Transport\TransportInterface; |
8
|
|
|
use Zend\Stdlib\AbstractOptions; |
9
|
|
|
use Zend\View\Renderer\RendererInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Module options |
13
|
|
|
* @author Alejandro Celaya Alastrué |
14
|
|
|
* @link http://www.alejandrocelaya.com |
15
|
|
|
*/ |
16
|
|
|
class MailOptions extends AbstractOptions |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Standard adapters aliasses |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $adapterMap = [ |
23
|
|
|
'sendmail' => ['\Zend\Mail\Transport\Sendmail'], |
24
|
|
|
'smtp' => ['\Zend\Mail\Transport\Smtp'], |
25
|
|
|
'in_memory' => ['\Zend\Mail\Transport\InMemory', '\Zend\Mail\Transport\Null'], |
26
|
|
|
'file' => ['\Zend\Mail\Transport\File'], |
27
|
|
|
'null' => ['\Zend\Mail\Transport\InMemory', '\Zend\Mail\Transport\Null'], |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var TransportInterface|string |
32
|
|
|
*/ |
33
|
|
|
private $mailAdapter = '\Zend\Mail\Transport\Sendmail'; |
34
|
|
|
/** |
35
|
|
|
* @var MessageOptions; |
36
|
|
|
*/ |
37
|
|
|
private $messageOptions; |
38
|
|
|
/** |
39
|
|
|
* @var SmtpOptions; |
40
|
|
|
*/ |
41
|
|
|
private $smtpOptions; |
42
|
|
|
/** |
43
|
|
|
* @var FileOptions |
44
|
|
|
*/ |
45
|
|
|
private $fileOptions; |
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $mailListeners = []; |
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $renderer = 'mailviewrenderer'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return TransportInterface|string |
57
|
|
|
*/ |
58
|
16 |
|
public function getMailAdapter() |
59
|
|
|
{ |
60
|
16 |
|
return $this->mailAdapter; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string|TransportInterface $mailAdapter |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
8 |
|
public function setMailAdapter($mailAdapter) |
68
|
|
|
{ |
69
|
|
|
// Map adapter aliases to the real class name |
70
|
8 |
|
if (is_string($mailAdapter) && array_key_exists(strtolower($mailAdapter), $this->adapterMap)) { |
71
|
3 |
|
$mailAdapter = $this->adapterMap[strtolower($mailAdapter)]; |
72
|
3 |
|
foreach ($mailAdapter as $class) { |
73
|
3 |
|
if (class_exists($class)) { |
74
|
3 |
|
$mailAdapter = $class; |
75
|
3 |
|
break; |
76
|
|
|
} |
77
|
3 |
|
} |
78
|
3 |
|
} |
79
|
|
|
|
80
|
8 |
|
$this->mailAdapter = $mailAdapter; |
81
|
8 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Alias for method getMailAdapter |
86
|
|
|
* @return string|TransportInterface |
87
|
|
|
*/ |
88
|
2 |
|
public function getTransport() |
89
|
|
|
{ |
90
|
2 |
|
return $this->getMailAdapter(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Alias for method setMailAdapter |
95
|
|
|
* @param string|TransportInterface $transport |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
1 |
|
public function setTransport($transport) |
99
|
|
|
{ |
100
|
1 |
|
return $this->setMailAdapter($transport); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return MessageOptions |
105
|
|
|
*/ |
106
|
18 |
|
public function getMessageOptions() |
107
|
|
|
{ |
108
|
18 |
|
if (! isset($this->messageOptions)) { |
109
|
10 |
|
$this->setMessageOptions([]); |
110
|
10 |
|
} |
111
|
|
|
|
112
|
18 |
|
return $this->messageOptions; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param MessageOptions|array $messageOptions |
117
|
|
|
* @return $this |
118
|
|
|
* @throws \AcMailer\Exception\InvalidArgumentException |
119
|
|
|
*/ |
120
|
20 |
View Code Duplication |
public function setMessageOptions($messageOptions) |
|
|
|
|
121
|
|
|
{ |
122
|
20 |
|
if (is_array($messageOptions)) { |
123
|
19 |
|
$this->messageOptions = new MessageOptions($messageOptions); |
124
|
20 |
|
} elseif ($messageOptions instanceof MessageOptions) { |
125
|
1 |
|
$this->messageOptions = $messageOptions; |
126
|
1 |
|
} else { |
127
|
1 |
|
throw new InvalidArgumentException(sprintf( |
128
|
1 |
|
'MessageOptions should be an array or an AcMailer\Options\MessageOptions object. %s provided.', |
129
|
1 |
|
is_object($messageOptions) ? get_class($messageOptions) : gettype($messageOptions) |
130
|
1 |
|
)); |
131
|
|
|
} |
132
|
|
|
|
133
|
19 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return SmtpOptions |
138
|
|
|
*/ |
139
|
3 |
|
public function getSmtpOptions() |
140
|
|
|
{ |
141
|
3 |
|
if (! isset($this->smtpOptions)) { |
142
|
1 |
|
$this->setSmtpOptions([]); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
3 |
|
return $this->smtpOptions; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param SmtpOptions|array $smtpOptions |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
4 |
View Code Duplication |
public function setSmtpOptions($smtpOptions) |
|
|
|
|
153
|
|
|
{ |
154
|
4 |
|
if (is_array($smtpOptions)) { |
155
|
3 |
|
$this->smtpOptions = new SmtpOptions($smtpOptions); |
156
|
4 |
|
} elseif ($smtpOptions instanceof SmtpOptions) { |
157
|
1 |
|
$this->smtpOptions = $smtpOptions; |
158
|
1 |
|
} else { |
159
|
1 |
|
throw new InvalidArgumentException(sprintf( |
160
|
1 |
|
'SmtpOptions should be an array or an Zend\Mail\Transport\SmtpOptions object. %s provided.', |
161
|
1 |
|
is_object($smtpOptions) ? get_class($smtpOptions) : gettype($smtpOptions) |
162
|
1 |
|
)); |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return FileOptions |
170
|
|
|
*/ |
171
|
3 |
|
public function getFileOptions() |
172
|
|
|
{ |
173
|
3 |
|
if (! isset($this->fileOptions)) { |
174
|
1 |
|
$this->setFileOptions([]); |
175
|
1 |
|
} |
176
|
|
|
|
177
|
3 |
|
return $this->fileOptions; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param FileOptions|array $fileOptions |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
4 |
View Code Duplication |
public function setFileOptions($fileOptions) |
|
|
|
|
185
|
|
|
{ |
186
|
4 |
|
if (is_array($fileOptions)) { |
187
|
3 |
|
$this->fileOptions = new FileOptions($fileOptions); |
188
|
4 |
|
} elseif ($fileOptions instanceof FileOptions) { |
189
|
1 |
|
$this->fileOptions = $fileOptions; |
190
|
1 |
|
} else { |
191
|
1 |
|
throw new InvalidArgumentException(sprintf( |
192
|
1 |
|
'FileOptions should be an array or an Zend\Mail\Transport\FileOptions object. %s provided.', |
193
|
1 |
|
is_object($fileOptions) ? get_class($fileOptions) : gettype($fileOptions) |
194
|
1 |
|
)); |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
13 |
|
public function getMailListeners() |
204
|
|
|
{ |
205
|
13 |
|
return $this->mailListeners; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param array $mailListeners |
210
|
|
|
* @return $this |
211
|
|
|
*/ |
212
|
3 |
|
public function setMailListeners($mailListeners) |
213
|
|
|
{ |
214
|
3 |
|
$this->mailListeners = (array) $mailListeners; |
215
|
3 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
12 |
|
public function getRenderer() |
222
|
|
|
{ |
223
|
12 |
|
return $this->renderer; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $renderer |
228
|
|
|
* @return $this |
229
|
|
|
*/ |
230
|
1 |
|
public function setRenderer($renderer) |
231
|
|
|
{ |
232
|
1 |
|
$this->renderer = $renderer; |
233
|
1 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.