Complex classes like AzineTwigSwiftMailer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AzineTwigSwiftMailer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class AzineTwigSwiftMailer extends TwigSwiftMailer implements TemplateTwigSwiftMailerInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var Translator |
||
| 22 | */ |
||
| 23 | protected $translator; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Logger |
||
| 27 | */ |
||
| 28 | protected $logger; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var TemplateProviderInterface |
||
| 32 | */ |
||
| 33 | protected $templateProvider; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ManagerRegistry |
||
| 37 | */ |
||
| 38 | protected $managerRegistry; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @var RequestContext |
||
| 43 | */ |
||
| 44 | protected $routerContext; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var email to use for "no-reply" |
||
| 48 | */ |
||
| 49 | protected $noReplyEmail; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var name to use for "no-reply" |
||
| 53 | */ |
||
| 54 | protected $noReplyName; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The Swift_Mailer to be used for sending emails immediately |
||
| 58 | * @var \Swift_Mailer |
||
| 59 | */ |
||
| 60 | private $immediateMailer; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var EmailOpenTrackingCodeBuilderInterface |
||
| 64 | */ |
||
| 65 | private $emailOpenTrackingCodeBuilder; |
||
| 66 | |||
| 67 | private $encodedItemIdPattern; |
||
| 68 | private $currentHost; |
||
| 69 | private $templateCache = array(); |
||
| 70 | private $imageCache = array(); |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * |
||
| 75 | * @param \Swift_Mailer $mailer |
||
| 76 | * @param UrlGeneratorInterface $router |
||
| 77 | * @param \Twig_Environment $twig |
||
| 78 | * @param Logger $logger |
||
| 79 | * @param Translator $translator |
||
| 80 | * @param array $parameters |
||
| 81 | * @param \Swift_Mailer $immediateMailer |
||
| 82 | */ |
||
| 83 | 7 | public function __construct( \Swift_Mailer $mailer, |
|
| 84 | UrlGeneratorInterface $router, |
||
| 85 | \Twig_Environment $twig, |
||
| 86 | Logger $logger, |
||
| 87 | Translator $translator, |
||
| 88 | TemplateProviderInterface $templateProvider, |
||
| 89 | ManagerRegistry $managerRegistry, |
||
| 90 | EmailOpenTrackingCodeBuilderInterface $emailOpenTrackingCodeBuilder, |
||
| 91 | AzineEmailTwigExtension $emailTwigExtension, |
||
| 92 | array $parameters, |
||
| 93 | \Swift_Mailer $immediateMailer = null) |
||
| 94 | { |
||
| 95 | 7 | parent::__construct($mailer, $router, $twig, $parameters); |
|
| 96 | 7 | $this->immediateMailer = $immediateMailer; |
|
| 97 | 7 | $this->logger = $logger; |
|
| 98 | 7 | $this->translator = $translator; |
|
| 99 | 7 | $this->templateProvider = $templateProvider; |
|
| 100 | 7 | $this->managerRegistry = $managerRegistry; |
|
| 101 | 7 | $this->noReplyEmail = $parameters[AzineEmailExtension::NO_REPLY][AzineEmailExtension::NO_REPLY_EMAIL_ADDRESS]; |
|
| 102 | 7 | $this->noReplyName = $parameters[AzineEmailExtension::NO_REPLY][AzineEmailExtension::NO_REPLY_EMAIL_NAME]; |
|
| 103 | 7 | $this->emailOpenTrackingCodeBuilder = $emailOpenTrackingCodeBuilder; |
|
| 104 | 7 | $this->routerContext = $router->getContext(); |
|
| 105 | 7 | $this->currentHost = $this->routerContext->getHost(); |
|
| 106 | 7 | $this->encodedItemIdPattern = "/^cid:.*@/"; |
|
| 107 | 7 | $this->emailTwigExtension = $emailTwigExtension; |
|
|
|
|||
| 108 | 7 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * (non-PHPdoc) |
||
| 112 | * @see Azine\EmailBundle\Services.TemplateTwigSwiftMailerInterface::sendEmail() |
||
| 113 | */ |
||
| 114 | 7 | 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) |
|
| 115 | { |
||
| 116 | // create the message |
||
| 117 | 7 | if ($message == null) { |
|
| 118 | 7 | $message = \Swift_Message::newInstance(); |
|
| 119 | 7 | } |
|
| 120 | |||
| 121 | 7 | $message->setSubject($subject); |
|
| 122 | |||
| 123 | // set the from-Name & -Emali to the default ones if not given |
||
| 124 | 7 | if ($from == null) { |
|
| 125 | 4 | $from = $this->noReplyEmail; |
|
| 126 | 4 | } |
|
| 127 | 7 | if ($fromName == null) { |
|
| 128 | 4 | $fromName = $this->noReplyName; |
|
| 129 | 4 | } |
|
| 130 | |||
| 131 | // add the from-email for the footer-text |
||
| 132 | 7 | if (!array_key_exists('fromEmail', $params)) { |
|
| 133 | 7 | $params['sendMailAccountName'] = $this->noReplyName; |
|
| 134 | 7 | $params['sendMailAccountAddress'] = $this->noReplyEmail; |
|
| 135 | 7 | } |
|
| 136 | |||
| 137 | // get the baseTemplate. => templateId without the ending. |
||
| 138 | 7 | $templateBaseId = substr($template, 0, strrpos($template, ".", -6)); |
|
| 139 | |||
| 140 | // check if this email should be stored for web-view |
||
| 141 | 7 | if ($this->templateProvider->saveWebViewFor($templateBaseId)) { |
|
| 142 | // keep a copy of the vars for the web-view |
||
| 143 | 4 | $webViewParams = $params; |
|
| 144 | |||
| 145 | // add the web-view token |
||
| 146 | 4 | $params[$this->templateProvider->getWebViewTokenId()] = SentEmail::getNewToken(); |
|
| 147 | 4 | } else { |
|
| 148 | 3 | $webViewParams = array(); |
|
| 149 | } |
||
| 150 | |||
| 151 | // recursively add all template-variables for the wrapper-templates and contentItems |
||
| 152 | 7 | $params = $this->templateProvider->addTemplateVariablesFor($templateBaseId, $params); |
|
| 153 | |||
| 154 | // recursively attach all messages in the array |
||
| 155 | 7 | $this->embedImages($message, $params); |
|
| 156 | |||
| 157 | // change the locale for the email-recipients |
||
| 158 | 7 | if ($emailLocale !== null && strlen($emailLocale) > 0) { |
|
| 159 | 6 | $currentUserLocale = $this->translator->getLocale(); |
|
| 160 | |||
| 161 | // change the router-context locale |
||
| 162 | 6 | $this->routerContext->setParameter("_locale", $emailLocale); |
|
| 163 | |||
| 164 | // change the translator locale |
||
| 165 | 6 | $this->translator->setLocale($emailLocale); |
|
| 166 | 6 | } else { |
|
| 167 | 1 | $emailLocale = $this->translator->getLocale(); |
|
| 168 | } |
||
| 169 | |||
| 170 | // recursively add snippets for the wrapper-templates and contentItems |
||
| 171 | 7 | $params = $this->templateProvider->addTemplateSnippetsWithImagesFor($templateBaseId, $params, $emailLocale); |
|
| 172 | |||
| 173 | // add the emailLocale (used for web-view) |
||
| 174 | 7 | $params['emailLocale'] = $emailLocale; |
|
| 175 | |||
| 176 | // render the email parts |
||
| 177 | 7 | $twigTemplate = $this->loadTemplate($template); |
|
| 178 | 7 | $textBody = $twigTemplate->renderBlock('body_text', $params); |
|
| 179 | 7 | $message->addPart($textBody, 'text/plain'); |
|
| 180 | |||
| 181 | 7 | $htmlBody = $twigTemplate->renderBlock('body_html', $params); |
|
| 182 | |||
| 183 | 7 | $campaignParams = $this->templateProvider->getCampaignParamsFor($templateBaseId, $params); |
|
| 184 | |||
| 185 | 7 | if (sizeof($campaignParams) > 0) { |
|
| 186 | 5 | $htmlBody = $this->emailTwigExtension->addCampaignParamsToAllUrls($htmlBody, $campaignParams); |
|
| 187 | 5 | } |
|
| 188 | |||
| 189 | // if email-tracking is enabled |
||
| 190 | 7 | if($this->emailOpenTrackingCodeBuilder){ |
|
| 191 | // add an image at the end of the html tag with the tracking-params to track email-opens |
||
| 192 | 7 | $imgTrackingCode = $this->emailOpenTrackingCodeBuilder->getTrackingImgCode($templateBaseId, $campaignParams, $params, $message->getId(), $to, $cc, $bcc); |
|
| 193 | 7 | if($imgTrackingCode && strlen($imgTrackingCode) > 0) { |
|
| 194 | 5 | $htmlCloseTagPosition = strpos($htmlBody, "</body>"); |
|
| 195 | 5 | $htmlBody = substr_replace($htmlBody, $imgTrackingCode, $htmlCloseTagPosition, 0); |
|
| 196 | 5 | } |
|
| 197 | 7 | } |
|
| 198 | |||
| 199 | 7 | $message->setBody($htmlBody, 'text/html'); |
|
| 200 | |||
| 201 | // remove unused/unreferenced embeded items from the message |
||
| 202 | 7 | $message = $this->removeUnreferecedEmbededItemsFromMessage($message, $params, $htmlBody); |
|
| 203 | |||
| 204 | // change the locale back to the users locale |
||
| 205 | 7 | if (isset($currentUserLocale) && $currentUserLocale != null) { |
|
| 206 | 6 | $this->routerContext->setParameter("_locale", $currentUserLocale); |
|
| 207 | 6 | $this->translator->setLocale($currentUserLocale); |
|
| 208 | 6 | } |
|
| 209 | |||
| 210 | // add attachments |
||
| 211 | 7 | foreach ($attachments as $fileName => $file) { |
|
| 212 | |||
| 213 | // add attachment from existing file |
||
| 214 | 2 | if (is_string($file)) { |
|
| 215 | |||
| 216 | // check that the file really exists! |
||
| 217 | 2 | if (file_exists($file)) { |
|
| 218 | 1 | $attachment = \Swift_Attachment::fromPath($file); |
|
| 219 | 1 | if (strlen($fileName) >= 5 ) { |
|
| 220 | 1 | $attachment->setFilename($fileName); |
|
| 221 | 1 | } |
|
| 222 | 1 | } else { |
|
| 223 | 1 | throw new FileException("File not found: ".$file); |
|
| 224 | } |
||
| 225 | |||
| 226 | // add attachment from generated data |
||
| 227 | 1 | } else { |
|
| 228 | 1 | $attachment = \Swift_Attachment::newInstance($file, $fileName); |
|
| 229 | } |
||
| 230 | |||
| 231 | 1 | $message->attach($attachment); |
|
| 232 | 6 | } |
|
| 233 | |||
| 234 | // set the addresses |
||
| 235 | 6 | if ($from) { |
|
| 236 | 6 | $message->setFrom($from, $fromName); |
|
| 237 | 6 | } |
|
| 238 | 6 | if ($replyTo) { |
|
| 239 | 2 | $message->setReplyTo($replyTo, $replyToName); |
|
| 240 | 6 | } elseif ($from) { |
|
| 241 | 4 | $message->setReplyTo($from, $fromName); |
|
| 242 | 4 | } |
|
| 243 | 6 | if ($to) { |
|
| 244 | 6 | $message->setTo($to, $toName); |
|
| 245 | 6 | } |
|
| 246 | 6 | if ($cc) { |
|
| 247 | 2 | $message->setCc($cc, $ccName); |
|
| 248 | 2 | } |
|
| 249 | 6 | if ($bcc) { |
|
| 250 | 2 | $message->setBcc($bcc, $bccName); |
|
| 251 | 2 | } |
|
| 252 | |||
| 253 | // add custom headers |
||
| 254 | 6 | $this->templateProvider->addCustomHeaders($templateBaseId, $message, $params); |
|
| 255 | |||
| 256 | // send the message |
||
| 257 | 6 | $mailer = $this->getMailer($params); |
|
| 258 | 6 | $messagesSent = $mailer->send($message, $failedRecipients); |
|
| 259 | |||
| 260 | // if the message was successfully sent, |
||
| 261 | // and it should be made available in web-view |
||
| 262 | 6 | if ($messagesSent && array_key_exists($this->templateProvider->getWebViewTokenId(), $params)) { |
|
| 263 | |||
| 264 | // store the email |
||
| 265 | 2 | $sentEmail = new SentEmail(); |
|
| 266 | 2 | $sentEmail->setToken($params[$this->templateProvider->getWebViewTokenId()]); |
|
| 267 | 2 | $sentEmail->setTemplate($templateBaseId); |
|
| 268 | 2 | $sentEmail->setSent(new \DateTime()); |
|
| 269 | |||
| 270 | // recursively add all template-variables for the wrapper-templates and contentItems |
||
| 271 | 2 | $webViewParams = $this->templateProvider->addTemplateVariablesFor($template, $webViewParams); |
|
| 272 | |||
| 273 | // replace absolute image-paths with relative ones. |
||
| 274 | 2 | $webViewParams = $this->templateProvider->makeImagePathsWebRelative($webViewParams, $emailLocale); |
|
| 275 | |||
| 276 | // recursively add snippets for the wrapper-templates and contentItems |
||
| 277 | 2 | $webViewParams = $this->templateProvider->addTemplateSnippetsWithImagesFor($template, $webViewParams, $emailLocale, true); |
|
| 278 | |||
| 279 | 2 | $sentEmail->setVariables($webViewParams); |
|
| 280 | |||
| 281 | // save only successfull recipients |
||
| 282 | 2 | if (!is_array($to)) { |
|
| 283 | 2 | $to = array($to); |
|
| 284 | 2 | } |
|
| 285 | 2 | $successfulRecipients = array_diff($to, $failedRecipients); |
|
| 286 | 2 | $sentEmail->setRecipients($successfulRecipients); |
|
| 287 | |||
| 288 | // write to db |
||
| 289 | 2 | $this->managerRegistry->getManager()->persist($sentEmail); |
|
| 290 | 2 | $this->managerRegistry->getManager()->flush($sentEmail); |
|
| 291 | 2 | } |
|
| 292 | |||
| 293 | 6 | return $messagesSent; |
|
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Remove all Embeded Attachments that are not referenced in the html-body from the message |
||
| 298 | * to avoid using unneccary bandwidth. |
||
| 299 | * |
||
| 300 | * @param \Swift_Message $message |
||
| 301 | * @param array $params the parameters used to render the html |
||
| 302 | * @param string $htmlBody |
||
| 303 | * @return \Swift_Message |
||
| 304 | */ |
||
| 305 | 7 | private function removeUnreferecedEmbededItemsFromMessage(\Swift_Message $message, $params, $htmlBody) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get the template from the cache if it was loaded already |
||
| 338 | * @param string $template |
||
| 339 | * @return \Twig_Template |
||
| 340 | */ |
||
| 341 | 7 | private function loadTemplate($template) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Recursively embed all images in the array into the message |
||
| 352 | * @param \Swift_Message $message |
||
| 353 | * @param array $params |
||
| 354 | * @return array $params |
||
| 355 | */ |
||
| 356 | 7 | private function embedImages(&$message, &$params) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get the Swift_Image for the file. |
||
| 415 | * @param string $filePath |
||
| 416 | * @return \Swift_Image|null |
||
| 417 | */ |
||
| 418 | 7 | private function cachedEmbedImage($filePath) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * (non-PHPdoc) |
||
| 450 | * @see Azine\EmailBundle\Services.TemplateTwigSwiftMailerInterface::sendSingleEmail() |
||
| 451 | */ |
||
| 452 | 4 | public function sendSingleEmail($to, $toName, $subject, array $params, $template, $emailLocale, $from = null, $fromName = null, \Swift_Message &$message = null) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Override the fosuserbundles original sendMessage, to embed template variables etc. into html-emails. |
||
| 462 | * @param string $templateName |
||
| 463 | * @param array $context |
||
| 464 | * @param string $fromEmail |
||
| 465 | * @param string $toEmail |
||
| 466 | * @return boolean true if the mail was sent successfully, else false |
||
| 467 | */ |
||
| 468 | 2 | protected function sendMessage($templateName, $context, $fromEmail, $toEmail) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Return the Swift_Mailer to be used for sending mails immediately (e.g. instead of spooling them) if it is configured |
||
| 480 | * @param $params |
||
| 481 | * @return \Swift_Mailer |
||
| 482 | */ |
||
| 483 | 6 | private function getMailer($params){ |
|
| 493 | } |
||
| 494 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: