Conditions | 27 |
Paths | > 20000 |
Total Lines | 180 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Tests | 84 |
CRAP Score | 27 |
Changes | 7 | ||
Bugs | 3 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
135 | public function sendEmail(&$failedRecipients, $subject, $from, $fromName, $to, $toName, $cc, $ccName, $bcc, $bccName, $replyTo, $replyToName, array $params, $template, $attachments = array(), $emailLocale = null, \Swift_Message &$message = null) |
||
136 | 7 | { |
|
137 | 7 | // create the message |
|
138 | if (null === $message) { |
||
139 | $message = new \Swift_Message(); |
||
140 | 7 | } |
|
141 | |||
142 | $message->setSubject($subject); |
||
143 | 7 | ||
144 | 2 | // set the from-Name & -Email to the default ones if not given |
|
145 | 2 | if (null === $from) { |
|
146 | 2 | $from = $this->noReplyEmail; |
|
147 | if (null === $fromName) { |
||
148 | $fromName = $this->noReplyName; |
||
149 | } |
||
150 | } |
||
151 | 7 | ||
152 | 7 | // add the from-email for the footer-text |
|
153 | 7 | if (!array_key_exists('fromEmail', $params)) { |
|
154 | $params['sendMailAccountName'] = $this->noReplyName; |
||
155 | $params['sendMailAccountAddress'] = $this->noReplyEmail; |
||
156 | } |
||
157 | 7 | ||
158 | // get the baseTemplate. => templateId without the ending. |
||
159 | $templateBaseId = substr($template, 0, strrpos($template, '.', -6)); |
||
160 | 7 | ||
161 | // check if this email should be stored for web-view |
||
162 | 4 | if ($this->templateProvider->saveWebViewFor($templateBaseId)) { |
|
163 | // keep a copy of the vars for the web-view |
||
164 | $webViewParams = $params; |
||
165 | 4 | ||
166 | // add the web-view token |
||
167 | 3 | $params[$this->templateProvider->getWebViewTokenId()] = SentEmail::getNewToken(); |
|
168 | } else { |
||
169 | $webViewParams = array(); |
||
170 | } |
||
171 | 7 | ||
172 | // recursively add all template-variables for the wrapper-templates and contentItems |
||
173 | $params = $this->templateProvider->addTemplateVariablesFor($templateBaseId, $params); |
||
174 | 7 | ||
175 | // recursively attach all messages in the array |
||
176 | $this->embedImages($message, $params); |
||
177 | 7 | ||
178 | 6 | // change the locale for the email-recipients |
|
179 | if (null !== $emailLocale && strlen($emailLocale) > 0) { |
||
180 | $currentUserLocale = $this->translator->getLocale(); |
||
181 | 6 | ||
182 | // change the router-context locale |
||
183 | $this->routerContext->setParameter('_locale', $emailLocale); |
||
184 | 6 | ||
185 | // change the translator locale |
||
186 | 1 | $this->translator->setLocale($emailLocale); |
|
187 | } else { |
||
188 | $emailLocale = $this->translator->getLocale(); |
||
189 | } |
||
190 | 7 | ||
191 | // recursively add snippets for the wrapper-templates and contentItems |
||
192 | $params = $this->templateProvider->addTemplateSnippetsWithImagesFor($templateBaseId, $params, $emailLocale); |
||
193 | 7 | ||
194 | // add the emailLocale (used for web-view) |
||
195 | $params['emailLocale'] = $emailLocale; |
||
196 | 7 | ||
197 | 7 | // render the email parts |
|
198 | 7 | $twigTemplate = $this->loadTemplate($template); |
|
199 | $textBody = $twigTemplate->renderBlock('body_text', $params); |
||
200 | 7 | $message->addPart($textBody, 'text/plain'); |
|
201 | |||
202 | 7 | $htmlBody = $twigTemplate->renderBlock('body_html', $params); |
|
203 | |||
204 | 7 | $campaignParams = $this->templateProvider->getCampaignParamsFor($templateBaseId, $params); |
|
205 | 5 | ||
206 | if (sizeof($campaignParams) > 0) { |
||
207 | $htmlBody = $this->emailTwigExtension->addCampaignParamsToAllUrls($htmlBody, $campaignParams); |
||
208 | } |
||
209 | 7 | ||
210 | // if email-tracking is enabled |
||
211 | 7 | if ($this->emailOpenTrackingCodeBuilder) { |
|
212 | 7 | // add an image at the end of the html tag with the tracking-params to track email-opens |
|
213 | 5 | $imgTrackingCode = $this->emailOpenTrackingCodeBuilder->getTrackingImgCode($templateBaseId, $campaignParams, $params, $message->getId(), $to, $cc, $bcc); |
|
214 | 5 | if ($imgTrackingCode && strlen($imgTrackingCode) > 0) { |
|
215 | $htmlCloseTagPosition = strpos($htmlBody, '</body>'); |
||
216 | $htmlBody = substr_replace($htmlBody, $imgTrackingCode, $htmlCloseTagPosition, 0); |
||
217 | } |
||
218 | 7 | } |
|
219 | |||
220 | $message->setBody($htmlBody, 'text/html'); |
||
221 | 7 | ||
222 | // remove unused/unreferenced embeded items from the message |
||
223 | $message = $this->removeUnreferecedEmbededItemsFromMessage($message, $params, $htmlBody); |
||
224 | 7 | ||
225 | 6 | // change the locale back to the users locale |
|
226 | 6 | if (isset($currentUserLocale) && null !== $currentUserLocale) { |
|
227 | $this->routerContext->setParameter('_locale', $currentUserLocale); |
||
228 | $this->translator->setLocale($currentUserLocale); |
||
229 | } |
||
230 | 7 | ||
231 | // add attachments |
||
232 | 2 | foreach ($attachments as $fileName => $file) { |
|
233 | // add attachment from existing file |
||
234 | 2 | if (is_string($file)) { |
|
235 | 1 | // check that the file really exists! |
|
236 | 1 | if (file_exists($file)) { |
|
237 | 1 | $attachment = \Swift_Attachment::fromPath($file); |
|
238 | if (strlen($fileName) >= 5) { |
||
239 | $attachment->setFilename($fileName); |
||
240 | 2 | } |
|
241 | } else { |
||
242 | throw new FileException('File not found: '.$file); |
||
243 | } |
||
244 | |||
245 | 1 | // add attachment from generated data |
|
246 | } else { |
||
247 | $attachment = new \Swift_Attachment($file, $fileName); |
||
248 | 1 | } |
|
249 | |||
250 | $message->attach($attachment); |
||
251 | } |
||
252 | 6 | ||
253 | 6 | // set the addresses |
|
254 | if ($from) { |
||
255 | 6 | $message->setFrom($from, $fromName); |
|
256 | 2 | } |
|
257 | 4 | if ($replyTo) { |
|
258 | 4 | $message->setReplyTo($replyTo, $replyToName); |
|
259 | } elseif ($from) { |
||
260 | 6 | $message->setReplyTo($from, $fromName); |
|
261 | 6 | } |
|
262 | if ($to) { |
||
263 | 6 | $message->setTo($to, $toName); |
|
264 | 2 | } |
|
265 | if ($cc) { |
||
266 | 6 | $message->setCc($cc, $ccName); |
|
267 | 2 | } |
|
268 | if ($bcc) { |
||
269 | $message->setBcc($bcc, $bccName); |
||
270 | } |
||
271 | 6 | ||
272 | // add custom headers |
||
273 | $this->templateProvider->addCustomHeaders($templateBaseId, $message, $params); |
||
274 | 6 | ||
275 | 6 | // send the message |
|
276 | $mailer = $this->getMailer($params); |
||
277 | $messagesSent = $mailer->send($message, $failedRecipients); |
||
278 | |||
279 | 6 | // if the message was successfully sent, |
|
280 | // and it should be made available in web-view |
||
281 | 2 | if ($messagesSent && array_key_exists($this->templateProvider->getWebViewTokenId(), $params)) { |
|
282 | 2 | // store the email |
|
283 | 2 | $sentEmail = new SentEmail(); |
|
284 | 2 | $sentEmail->setToken($params[$this->templateProvider->getWebViewTokenId()]); |
|
285 | $sentEmail->setTemplate($templateBaseId); |
||
286 | $sentEmail->setSent(new \DateTime()); |
||
287 | 2 | ||
288 | // recursively add all template-variables for the wrapper-templates and contentItems |
||
289 | $webViewParams = $this->templateProvider->addTemplateVariablesFor($template, $webViewParams); |
||
290 | 2 | ||
291 | // replace absolute image-paths with relative ones. |
||
292 | $webViewParams = $this->templateProvider->makeImagePathsWebRelative($webViewParams, $emailLocale); |
||
293 | 2 | ||
294 | // recursively add snippets for the wrapper-templates and contentItems |
||
295 | 2 | $webViewParams = $this->templateProvider->addTemplateSnippetsWithImagesFor($template, $webViewParams, $emailLocale, true); |
|
296 | |||
297 | $sentEmail->setVariables($webViewParams); |
||
298 | 2 | ||
299 | 2 | // save only successfull recipients |
|
300 | if (!is_array($to)) { |
||
301 | 2 | $to = array($to); |
|
302 | 2 | } |
|
303 | $successfulRecipients = array_diff($to, $failedRecipients); |
||
304 | $sentEmail->setRecipients($successfulRecipients); |
||
305 | 2 | ||
306 | 2 | // write to db |
|
307 | 2 | $em = $this->managerRegistry->getManager(); |
|
308 | 2 | $em->persist($sentEmail); |
|
309 | 2 | $em->flush($sentEmail); |
|
310 | $em->clear(); |
||
311 | gc_collect_cycles(); |
||
312 | 6 | } |
|
313 | |||
314 | return $messagesSent; |
||
315 | } |
||
547 |