| Total Complexity | 50 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EmailFinisher 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.
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 EmailFinisher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class EmailFinisher extends AbstractFinisher |
||
| 61 | { |
||
| 62 | const FORMAT_PLAINTEXT = 'plaintext'; |
||
| 63 | const FORMAT_HTML = 'html'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $defaultOptions = [ |
||
| 69 | 'recipientName' => '', |
||
| 70 | 'senderName' => '', |
||
| 71 | 'addHtmlPart' => true, |
||
| 72 | 'attachUploads' => true, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Executes this finisher |
||
| 77 | * @see AbstractFinisher::execute() |
||
| 78 | * |
||
| 79 | * @throws FinisherException |
||
| 80 | */ |
||
| 81 | protected function executeInternal() |
||
| 82 | { |
||
| 83 | $languageBackup = null; |
||
| 84 | // Flexform overrides write strings instead of integers so |
||
| 85 | // we need to cast the string '0' to false. |
||
| 86 | if ( |
||
| 87 | isset($this->options['addHtmlPart']) |
||
| 88 | && $this->options['addHtmlPart'] === '0' |
||
| 89 | ) { |
||
| 90 | $this->options['addHtmlPart'] = false; |
||
| 91 | } |
||
| 92 | |||
| 93 | $subject = $this->parseOption('subject'); |
||
| 94 | $recipients = $this->getRecipients('recipients'); |
||
| 95 | $senderAddress = $this->parseOption('senderAddress'); |
||
| 96 | $senderAddress = is_string($senderAddress) ? $senderAddress : ''; |
||
| 97 | $senderName = $this->parseOption('senderName'); |
||
| 98 | $senderName = is_string($senderName) ? $senderName : ''; |
||
| 99 | $replyToRecipients = $this->getRecipients('replyToRecipients'); |
||
| 100 | $carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients'); |
||
| 101 | $blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients'); |
||
| 102 | $addHtmlPart = $this->parseOption('addHtmlPart') ? true : false; |
||
| 103 | $attachUploads = $this->parseOption('attachUploads'); |
||
| 104 | $useFluidEmail = $this->parseOption('useFluidEmail'); |
||
| 105 | $title = $this->parseOption('title'); |
||
| 106 | $title = is_string($title) && $title !== '' ? $title : $subject; |
||
| 107 | |||
| 108 | if (empty($subject)) { |
||
| 109 | throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320); |
||
| 110 | } |
||
| 111 | if (empty($recipients)) { |
||
| 112 | throw new FinisherException('The option "recipients" must be set for the EmailFinisher.', 1327060200); |
||
| 113 | } |
||
| 114 | if (empty($senderAddress)) { |
||
| 115 | throw new FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210); |
||
| 116 | } |
||
| 117 | |||
| 118 | $formRuntime = $this->finisherContext->getFormRuntime(); |
||
| 119 | |||
| 120 | $translationService = TranslationService::getInstance(); |
||
| 121 | if (is_string($this->options['translation']['language'] ?? null) && $this->options['translation']['language'] !== '') { |
||
| 122 | $languageBackup = $translationService->getLanguage(); |
||
| 123 | $translationService->setLanguage($this->options['translation']['language']); |
||
| 124 | } |
||
| 125 | |||
| 126 | $mail = $useFluidEmail |
||
| 127 | ? $this |
||
| 128 | ->initializeFluidEmail($formRuntime) |
||
| 129 | ->format($addHtmlPart ? FluidEmail::FORMAT_BOTH : FluidEmail::FORMAT_PLAIN) |
||
| 130 | ->assign('title', $title) |
||
| 131 | : GeneralUtility::makeInstance(MailMessage::class); |
||
| 132 | |||
| 133 | |||
| 134 | ->from(new Address($senderAddress, $senderName)) |
||
| 135 | ->to(...$recipients) |
||
| 136 | ->subject($subject); |
||
|
|
|||
| 137 | |||
| 138 | if (!empty($replyToRecipients)) { |
||
| 139 | $mail->replyTo(...$replyToRecipients); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (!empty($carbonCopyRecipients)) { |
||
| 143 | $mail->cc(...$carbonCopyRecipients); |
||
| 144 | } |
||
| 145 | |||
| 146 | if (!empty($blindCarbonCopyRecipients)) { |
||
| 147 | $mail->bcc(...$blindCarbonCopyRecipients); |
||
| 148 | } |
||
| 149 | |||
| 150 | if (!$useFluidEmail) { |
||
| 151 | $parts = [ |
||
| 152 | [ |
||
| 153 | 'format' => 'Plaintext', |
||
| 154 | 'contentType' => 'text/plain', |
||
| 155 | ], |
||
| 156 | ]; |
||
| 157 | |||
| 158 | if ($addHtmlPart) { |
||
| 159 | $parts[] = [ |
||
| 160 | 'format' => 'Html', |
||
| 161 | 'contentType' => 'text/html', |
||
| 162 | ]; |
||
| 163 | } |
||
| 164 | |||
| 165 | foreach ($parts as $i => $part) { |
||
| 166 | $standaloneView = $this->initializeStandaloneView($formRuntime, $part['format']); |
||
| 167 | $message = $standaloneView->render(); |
||
| 168 | |||
| 169 | if ($part['contentType'] === 'text/plain') { |
||
| 170 | $mail->text($message); |
||
| 171 | } else { |
||
| 172 | $mail->html($message); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if (!empty($languageBackup)) { |
||
| 178 | $translationService->setLanguage($languageBackup); |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($attachUploads) { |
||
| 182 | foreach ($formRuntime->getFormDefinition()->getRenderablesRecursively() as $element) { |
||
| 183 | if (!$element instanceof FileUpload) { |
||
| 184 | continue; |
||
| 185 | } |
||
| 186 | $file = $formRuntime[$element->getIdentifier()]; |
||
| 187 | if ($file) { |
||
| 188 | if ($file instanceof FileReference) { |
||
| 189 | $file = $file->getOriginalResource(); |
||
| 190 | } |
||
| 191 | $mail->attach($file->getContents(), $file->getName(), $file->getMimeType()); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | $useFluidEmail ? GeneralUtility::makeInstance(Mailer::class)->send($mail) : $mail->send(); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param FormRuntime $formRuntime |
||
| 201 | * @param string $format |
||
| 202 | * @return StandaloneView |
||
| 203 | * @throws FinisherException |
||
| 204 | */ |
||
| 205 | protected function initializeStandaloneView(FormRuntime $formRuntime, string $format): StandaloneView |
||
| 206 | { |
||
| 207 | $standaloneView = GeneralUtility::makeInstance(StandaloneView::class); |
||
| 208 | |||
| 209 | if (isset($this->options['templatePathAndFilename'])) { |
||
| 210 | $this->options['templatePathAndFilename'] = strtr($this->options['templatePathAndFilename'], [ |
||
| 211 | '{@format}' => $format |
||
| 212 | ]); |
||
| 213 | $standaloneView->setTemplatePathAndFilename($this->options['templatePathAndFilename']); |
||
| 214 | } else { |
||
| 215 | if (!isset($this->options['templateName'])) { |
||
| 216 | throw new FinisherException('The option "templateName" must be set for the EmailFinisher.', 1327058829); |
||
| 217 | } |
||
| 218 | // Use local variable instead of augmenting the options to |
||
| 219 | // keep the format intact when sending multi-format mails |
||
| 220 | $templateName = strtr($this->options['templateName'], [ |
||
| 221 | '{@format}' => $format |
||
| 222 | ]); |
||
| 223 | $standaloneView->setTemplate($templateName); |
||
| 224 | } |
||
| 225 | |||
| 226 | $standaloneView->assign('finisherVariableProvider', $this->finisherContext->getFinisherVariableProvider()); |
||
| 227 | |||
| 228 | if (isset($this->options['templateRootPaths']) && is_array($this->options['templateRootPaths'])) { |
||
| 229 | $standaloneView->setTemplateRootPaths($this->options['templateRootPaths']); |
||
| 230 | } |
||
| 231 | |||
| 232 | if (isset($this->options['partialRootPaths']) && is_array($this->options['partialRootPaths'])) { |
||
| 233 | $standaloneView->setPartialRootPaths($this->options['partialRootPaths']); |
||
| 234 | } |
||
| 235 | |||
| 236 | if (isset($this->options['layoutRootPaths']) && is_array($this->options['layoutRootPaths'])) { |
||
| 237 | $standaloneView->setLayoutRootPaths($this->options['layoutRootPaths']); |
||
| 238 | } |
||
| 239 | |||
| 240 | if (is_array($this->options['variables'] ?? null)) { |
||
| 241 | $standaloneView->assignMultiple($this->options['variables']); |
||
| 242 | } |
||
| 243 | |||
| 244 | $standaloneView->assign('form', $formRuntime); |
||
| 245 | $standaloneView->getRenderingContext() |
||
| 246 | ->getViewHelperVariableContainer() |
||
| 247 | ->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime); |
||
| 248 | |||
| 249 | return $standaloneView; |
||
| 250 | } |
||
| 251 | |||
| 252 | protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail |
||
| 253 | { |
||
| 254 | $templateConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL']; |
||
| 255 | |||
| 256 | if (is_array($this->options['templateRootPaths'] ?? null)) { |
||
| 257 | $templateConfiguration['templateRootPaths'] = array_replace_recursive( |
||
| 258 | $templateConfiguration['templateRootPaths'], |
||
| 259 | $this->options['templateRootPaths'] |
||
| 260 | ); |
||
| 261 | ksort($templateConfiguration['templateRootPaths']); |
||
| 262 | } |
||
| 263 | |||
| 264 | if (is_array($this->options['partialRootPaths'] ?? null)) { |
||
| 265 | $templateConfiguration['partialRootPaths'] = array_replace_recursive( |
||
| 266 | $templateConfiguration['partialRootPaths'], |
||
| 267 | $this->options['partialRootPaths'] |
||
| 268 | ); |
||
| 269 | ksort($templateConfiguration['partialRootPaths']); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (is_array($this->options['layoutRootPaths'] ?? null)) { |
||
| 273 | $templateConfiguration['layoutRootPaths'] = array_replace_recursive( |
||
| 274 | $templateConfiguration['layoutRootPaths'], |
||
| 275 | $this->options['layoutRootPaths'] |
||
| 276 | ); |
||
| 277 | ksort($templateConfiguration['layoutRootPaths']); |
||
| 278 | } |
||
| 279 | |||
| 280 | $fluidEmail = GeneralUtility::makeInstance( |
||
| 281 | FluidEmail::class, |
||
| 282 | GeneralUtility::makeInstance(TemplatePaths::class, $templateConfiguration) |
||
| 283 | ); |
||
| 284 | |||
| 285 | if (!isset($this->options['templateName']) || $this->options['templateName'] === '') { |
||
| 286 | throw new FinisherException('The option "templateName" must be set to use FluidEmail.', 1599834020); |
||
| 287 | } |
||
| 288 | |||
| 289 | // Migrate old template name to default FluidEmail name |
||
| 290 | if ($this->options['templateName'] === '{@format}.html') { |
||
| 291 | $this->options['templateName'] = 'Default'; |
||
| 292 | } |
||
| 293 | |||
| 294 | $fluidEmail |
||
| 295 | ->setTemplate($this->options['templateName']) |
||
| 296 | ->assignMultiple([ |
||
| 297 | 'finisherVariableProvider' => $this->finisherContext->getFinisherVariableProvider(), |
||
| 298 | 'form' => $formRuntime |
||
| 299 | ]); |
||
| 300 | |||
| 301 | if (is_array($this->options['variables'] ?? null)) { |
||
| 302 | $fluidEmail->assignMultiple($this->options['variables']); |
||
| 303 | } |
||
| 304 | |||
| 305 | $fluidEmail |
||
| 306 | ->getViewHelperVariableContainer() |
||
| 307 | ->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime); |
||
| 308 | |||
| 309 | return $fluidEmail; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get mail recipients |
||
| 314 | * |
||
| 315 | * @param string $listOption List option name |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | protected function getRecipients(string $listOption): array |
||
| 330 | } |
||
| 331 | } |
||
| 332 |