1 | <?php |
||
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 | ]; |
||
35 | |||
36 | 7 | public function __construct(MailServiceInterface $mailService) |
|
40 | |||
41 | /** |
||
42 | * If no arguments are provided, the mail service is returned. |
||
43 | * If any argument is provided, they will be used to configure the MailService and send an email. |
||
44 | * The result object will be returned in that case |
||
45 | * |
||
46 | * @param null|string|ViewModel|array $bodyOrConfig |
||
47 | * @param null|string $subject |
||
48 | * @param null|array $to |
||
49 | * @param null|string|array $from |
||
50 | * @param null|array $cc |
||
51 | * @param null|array $bcc |
||
52 | * @param null|array $attachments |
||
53 | * @return MailServiceInterface|ResultInterface |
||
54 | */ |
||
55 | 5 | public function __invoke( |
|
56 | $bodyOrConfig = null, |
||
|
|||
57 | $subject = null, |
||
58 | $to = null, |
||
59 | $from = null, |
||
60 | $cc = null, |
||
61 | $bcc = null, |
||
62 | $attachments = null |
||
63 | ) { |
||
64 | 5 | $args = func_get_args(); |
|
65 | 5 | if (empty($args)) { |
|
66 | 1 | return $this->mailService; |
|
67 | } |
||
68 | |||
69 | 4 | $args = $this->normalizeMailArgs($args); |
|
70 | 4 | $this->applyArgsToMailService($args); |
|
71 | 4 | return $this->mailService->send(); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Normalizes the arguments passed when invoking this plugin so that they can be treated in a consistent way |
||
76 | * |
||
77 | * @param array $args |
||
78 | * @return array |
||
79 | */ |
||
80 | 4 | protected function normalizeMailArgs(array $args) |
|
81 | { |
||
82 | // If the first argument is an array, use it as the mail configuration |
||
83 | 4 | if (is_array($args[0])) { |
|
84 | 1 | return $args[0]; |
|
85 | } |
||
86 | |||
87 | 3 | $result = []; |
|
88 | 3 | $length = count($args); |
|
89 | // FIXME This is a weak way to handle the arguments, since a change in the order will break it |
||
90 | 3 | for ($i = 0; $i < $length; $i++) { |
|
91 | 3 | $result[$this->argumentsMapping[$i]] = $args[$i]; |
|
92 | 3 | } |
|
93 | |||
94 | 3 | return $result; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Applies the arguments provided while invoking this plugin to the MailService, |
||
99 | * discarding any previous configuration |
||
100 | * |
||
101 | * @param array $args |
||
102 | */ |
||
103 | 4 | protected function applyArgsToMailService(array $args) |
|
104 | { |
||
105 | 4 | if (isset($args['body'])) { |
|
106 | 4 | $body = $args['body']; |
|
107 | |||
108 | 4 | if (is_string($body)) { |
|
109 | 3 | $this->mailService->setBody($body); |
|
110 | 3 | } else { |
|
111 | 1 | $this->mailService->setTemplate($body); |
|
112 | } |
||
113 | 4 | } |
|
114 | |||
115 | 4 | if (isset($args['subject'])) { |
|
116 | 3 | $this->mailService->setSubject($args['subject']); |
|
117 | 3 | } |
|
118 | |||
119 | 4 | if (isset($args['to'])) { |
|
120 | 2 | $this->mailService->getMessage()->setTo($args['to']); |
|
121 | 2 | } |
|
122 | |||
123 | 4 | if (isset($args['from'])) { |
|
124 | 2 | $from = $args['from']; |
|
125 | |||
126 | 2 | if (is_array($from)) { |
|
127 | 1 | $fromAddress = array_keys($from); |
|
128 | 1 | $fromName = array_values($from); |
|
129 | 1 | $this->mailService->getMessage()->setFrom($fromAddress[0], $fromName[0]); |
|
130 | 1 | } else { |
|
131 | 1 | $this->mailService->getMessage()->setFrom($from); |
|
132 | } |
||
133 | 2 | } |
|
134 | |||
135 | 4 | if (isset($args['cc'])) { |
|
136 | 1 | $this->mailService->getMessage()->setCc($args['cc']); |
|
137 | 1 | } |
|
138 | |||
139 | 4 | if (isset($args['bcc'])) { |
|
140 | 1 | $this->mailService->getMessage()->setBcc($args['bcc']); |
|
141 | 1 | } |
|
142 | |||
143 | 4 | if (isset($args['attachments'])) { |
|
144 | 1 | $this->mailService->setAttachments($args['attachments']); |
|
145 | 1 | } |
|
146 | 4 | } |
|
147 | |||
148 | /** |
||
149 | * @param MailServiceInterface $mailService |
||
150 | * @return $this |
||
151 | */ |
||
152 | 1 | public function setMailService(MailServiceInterface $mailService) |
|
153 | { |
||
154 | 1 | $this->mailService = $mailService; |
|
155 | 1 | return $this; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return MailServiceInterface |
||
160 | */ |
||
161 | 1 | public function getMailService() |
|
165 | } |
||
166 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.