1
|
|
|
<?php |
2
|
|
|
namespace AcMailer\Controller\Plugin; |
3
|
|
|
|
4
|
|
|
use AcMailer\Result\ResultInterface; |
5
|
|
|
use AcMailer\Service\MailServiceAwareInterface; |
6
|
|
|
use AcMailer\Service\MailServiceInterface; |
7
|
|
|
use Zend\Mvc\Controller\Plugin\AbstractPlugin; |
8
|
|
|
use Zend\View\Model\ViewModel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SendMailPlugin |
12
|
|
|
* @author Alejandro Celaya Alastrué |
13
|
|
|
* @link http://www.alejandrocelaya.com |
14
|
|
|
*/ |
15
|
|
|
class SendMailPlugin extends AbstractPlugin implements MailServiceAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var MailServiceInterface |
19
|
|
|
*/ |
20
|
|
|
protected $mailService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The list of possible arguments in the order they should be provided |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $argumentsMapping = [ |
27
|
|
|
0 => 'body', |
28
|
|
|
1 => 'subject', |
29
|
|
|
2 => 'to', |
30
|
|
|
3 => 'from', |
31
|
|
|
4 => 'cc', |
32
|
|
|
5 => 'bcc', |
33
|
|
|
6 => 'attachments', |
34
|
|
|
7 => 'replyTo', |
35
|
|
|
]; |
36
|
|
|
|
37
|
8 |
|
public function __construct(MailServiceInterface $mailService) |
38
|
|
|
{ |
39
|
8 |
|
$this->mailService = $mailService; |
40
|
8 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* If no arguments are provided, the mail service is returned. |
44
|
|
|
* If any argument is provided, they will be used to configure the MailService and send an email. |
45
|
|
|
* The result object will be returned in that case |
46
|
|
|
* |
47
|
|
|
* @param null|string|ViewModel|array $bodyOrConfig |
48
|
|
|
* @param null|string $subject |
49
|
|
|
* @param null|array $to |
50
|
|
|
* @param null|string|array $from |
51
|
|
|
* @param null|array $cc |
52
|
|
|
* @param null|array $bcc |
53
|
|
|
* @param null|array $attachments |
54
|
|
|
* @param null|array $replyTo |
55
|
|
|
* @return MailServiceInterface|ResultInterface |
56
|
|
|
*/ |
57
|
6 |
|
public function __invoke( |
58
|
|
|
$bodyOrConfig = null, |
59
|
|
|
$subject = null, |
60
|
|
|
$to = null, |
61
|
|
|
$from = null, |
62
|
|
|
$cc = null, |
63
|
|
|
$bcc = null, |
64
|
|
|
$attachments = null, |
65
|
|
|
$replyTo = null |
66
|
|
|
) { |
67
|
6 |
|
$args = func_get_args(); |
68
|
6 |
|
if (empty($args)) { |
69
|
1 |
|
return $this->mailService; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
$args = $this->normalizeMailArgs($args); |
73
|
5 |
|
$this->applyArgsToMailService($args); |
74
|
5 |
|
return $this->mailService->send(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Normalizes the arguments passed when invoking this plugin so that they can be treated in a consistent way |
79
|
|
|
* |
80
|
|
|
* @param array $args |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
5 |
|
protected function normalizeMailArgs(array $args) |
84
|
|
|
{ |
85
|
|
|
// If the first argument is an array, use it as the mail configuration |
86
|
5 |
|
if (is_array($args[0])) { |
87
|
1 |
|
return $args[0]; |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
$result = []; |
91
|
4 |
|
$length = count($args); |
92
|
|
|
// FIXME This is a weak way to handle the arguments, since a change in the order will break it |
93
|
4 |
|
for ($i = 0; $i < $length; $i++) { |
94
|
4 |
|
$result[$this->argumentsMapping[$i]] = $args[$i]; |
95
|
4 |
|
} |
96
|
|
|
|
97
|
4 |
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Applies the arguments provided while invoking this plugin to the MailService, |
102
|
|
|
* discarding any previous configuration |
103
|
|
|
* |
104
|
|
|
* @param array $args |
105
|
|
|
*/ |
106
|
5 |
|
protected function applyArgsToMailService(array $args) |
107
|
|
|
{ |
108
|
5 |
|
if (isset($args['body'])) { |
109
|
5 |
|
$body = $args['body']; |
110
|
|
|
|
111
|
5 |
|
if (is_string($body)) { |
112
|
4 |
|
$this->mailService->setBody($body); |
113
|
4 |
|
} else { |
114
|
1 |
|
$this->mailService->setTemplate($body); |
115
|
|
|
} |
116
|
5 |
|
} |
117
|
|
|
|
118
|
5 |
|
if (isset($args['subject'])) { |
119
|
4 |
|
$this->mailService->setSubject($args['subject']); |
|
|
|
|
120
|
4 |
|
} |
121
|
|
|
|
122
|
5 |
|
if (isset($args['to'])) { |
123
|
3 |
|
$this->mailService->getMessage()->setTo($args['to']); |
124
|
3 |
|
} |
125
|
|
|
|
126
|
5 |
|
if (isset($args['cc'])) { |
127
|
1 |
|
$this->mailService->getMessage()->setCc($args['cc']); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
5 |
|
if (isset($args['bcc'])) { |
131
|
1 |
|
$this->mailService->getMessage()->setBcc($args['bcc']); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
5 |
|
if (isset($args['attachments'])) { |
135
|
1 |
|
$this->mailService->setAttachments($args['attachments']); |
136
|
1 |
|
} |
137
|
|
|
|
138
|
5 |
|
$this->applyArrayArgs($args, 'from'); |
139
|
5 |
|
$this->applyArrayArgs($args, 'replyTo'); |
140
|
5 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $args |
144
|
|
|
* @param string $key |
145
|
|
|
*/ |
146
|
5 |
|
protected function applyArrayArgs(array $args, $key) |
147
|
|
|
{ |
148
|
5 |
|
if (!isset($args[$key])) { |
149
|
4 |
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
3 |
|
$arg = $args[$key]; |
153
|
3 |
|
$setter = 'set'.ucfirst($key); |
154
|
|
|
|
155
|
3 |
|
if (is_array($arg)) { |
156
|
1 |
|
$this->mailService->getMessage()->{$setter}(array_keys($arg)[0], |
157
|
1 |
|
array_values($arg)[0]); |
158
|
1 |
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
$this->mailService->getMessage()->{$setter}($arg); |
162
|
2 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param MailServiceInterface $mailService |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
1 |
|
public function setMailService(MailServiceInterface $mailService) |
169
|
|
|
{ |
170
|
1 |
|
$this->mailService = $mailService; |
171
|
1 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return MailServiceInterface |
176
|
|
|
*/ |
177
|
1 |
|
public function getMailService() |
178
|
|
|
{ |
179
|
1 |
|
return $this->mailService; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.