Total Complexity | 51 |
Total Lines | 284 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
61 | class EmailFinisher extends AbstractFinisher |
||
62 | { |
||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $defaultOptions = [ |
||
67 | 'recipientName' => '', |
||
68 | 'senderName' => '', |
||
69 | 'addHtmlPart' => true, |
||
70 | 'attachUploads' => true, |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Executes this finisher |
||
75 | * @see AbstractFinisher::execute() |
||
76 | * |
||
77 | * @throws FinisherException |
||
78 | */ |
||
79 | protected function executeInternal() |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param FormRuntime $formRuntime |
||
199 | * @param string $format |
||
200 | * @return StandaloneView |
||
201 | * @throws FinisherException |
||
202 | * @deprecated since v11, will be removed in v12 |
||
203 | */ |
||
204 | protected function initializeStandaloneView(FormRuntime $formRuntime, string $format): StandaloneView |
||
205 | { |
||
206 | trigger_error( |
||
207 | 'Using StandaloneView for EmailFinisher \'' . $this->finisherIdentifier . '\' is deprecated and will be removed in v12. Please use FluidEmail in the finishers configuration instead.', |
||
208 | E_USER_DEPRECATED |
||
209 | ); |
||
210 | |||
211 | $standaloneView = GeneralUtility::makeInstance(StandaloneView::class); |
||
212 | |||
213 | if (isset($this->options['templatePathAndFilename'])) { |
||
214 | trigger_error( |
||
215 | 'The option \'templatePathAndFilename\' for EmailFinisher \'' . $this->finisherIdentifier . '\' is deprecated and will be removed in v12. Please use \'templateName\' and \'templateRootPaths\' in the finishers definition instead.', |
||
216 | E_USER_DEPRECATED |
||
217 | ); |
||
218 | // Use local variable instead of augmenting the options to |
||
219 | // keep the format intact when sending multi-format mails |
||
220 | $templatePathAndFilename = strtr($this->options['templatePathAndFilename'], [ |
||
221 | '{@format}' => $format |
||
222 | ]); |
||
223 | $standaloneView->setTemplatePathAndFilename($templatePathAndFilename); |
||
224 | } else { |
||
225 | if (!isset($this->options['templateName'])) { |
||
226 | throw new FinisherException('The option "templateName" must be set for the EmailFinisher.', 1327058829); |
||
227 | } |
||
228 | // Use local variable instead of augmenting the options to |
||
229 | // keep the format intact when sending multi-format mails |
||
230 | $templateName = strtr($this->options['templateName'], [ |
||
231 | '{@format}' => $format |
||
232 | ]); |
||
233 | $standaloneView->setTemplate($templateName); |
||
234 | } |
||
235 | |||
236 | $standaloneView->assign('finisherVariableProvider', $this->finisherContext->getFinisherVariableProvider()); |
||
237 | |||
238 | if (isset($this->options['templateRootPaths']) && is_array($this->options['templateRootPaths'])) { |
||
239 | $standaloneView->setTemplateRootPaths($this->options['templateRootPaths']); |
||
240 | } |
||
241 | |||
242 | if (isset($this->options['partialRootPaths']) && is_array($this->options['partialRootPaths'])) { |
||
243 | $standaloneView->setPartialRootPaths($this->options['partialRootPaths']); |
||
244 | } |
||
245 | |||
246 | if (isset($this->options['layoutRootPaths']) && is_array($this->options['layoutRootPaths'])) { |
||
247 | $standaloneView->setLayoutRootPaths($this->options['layoutRootPaths']); |
||
248 | } |
||
249 | |||
250 | if (is_array($this->options['variables'] ?? null)) { |
||
251 | $standaloneView->assignMultiple($this->options['variables']); |
||
252 | } |
||
253 | |||
254 | $standaloneView->assign('form', $formRuntime); |
||
255 | $standaloneView->getRenderingContext() |
||
256 | ->getViewHelperVariableContainer() |
||
257 | ->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime); |
||
258 | |||
259 | return $standaloneView; |
||
260 | } |
||
261 | |||
262 | protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail |
||
263 | { |
||
264 | $templateConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL']; |
||
265 | |||
266 | if (is_array($this->options['templateRootPaths'] ?? null)) { |
||
267 | $templateConfiguration['templateRootPaths'] = array_replace_recursive( |
||
268 | $templateConfiguration['templateRootPaths'], |
||
269 | $this->options['templateRootPaths'] |
||
270 | ); |
||
271 | ksort($templateConfiguration['templateRootPaths']); |
||
272 | } |
||
273 | |||
274 | if (is_array($this->options['partialRootPaths'] ?? null)) { |
||
275 | $templateConfiguration['partialRootPaths'] = array_replace_recursive( |
||
276 | $templateConfiguration['partialRootPaths'], |
||
277 | $this->options['partialRootPaths'] |
||
278 | ); |
||
279 | ksort($templateConfiguration['partialRootPaths']); |
||
280 | } |
||
281 | |||
282 | if (is_array($this->options['layoutRootPaths'] ?? null)) { |
||
283 | $templateConfiguration['layoutRootPaths'] = array_replace_recursive( |
||
284 | $templateConfiguration['layoutRootPaths'], |
||
285 | $this->options['layoutRootPaths'] |
||
286 | ); |
||
287 | ksort($templateConfiguration['layoutRootPaths']); |
||
288 | } |
||
289 | |||
290 | $fluidEmail = GeneralUtility::makeInstance( |
||
291 | FluidEmail::class, |
||
292 | GeneralUtility::makeInstance(TemplatePaths::class, $templateConfiguration) |
||
293 | ); |
||
294 | |||
295 | if (!isset($this->options['templateName']) || $this->options['templateName'] === '') { |
||
296 | throw new FinisherException('The option "templateName" must be set to use FluidEmail.', 1599834020); |
||
297 | } |
||
298 | |||
299 | // Migrate old template name to default FluidEmail name |
||
300 | if ($this->options['templateName'] === '{@format}.html') { |
||
301 | $this->options['templateName'] = 'Default'; |
||
302 | } |
||
303 | |||
304 | // Set the PSR-7 request object if available |
||
305 | if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) { |
||
306 | $fluidEmail->setRequest($GLOBALS['TYPO3_REQUEST']); |
||
307 | } |
||
308 | |||
309 | $fluidEmail |
||
310 | ->setTemplate($this->options['templateName']) |
||
311 | ->assignMultiple([ |
||
312 | 'finisherVariableProvider' => $this->finisherContext->getFinisherVariableProvider(), |
||
313 | 'form' => $formRuntime |
||
314 | ]); |
||
315 | |||
316 | if (is_array($this->options['variables'] ?? null)) { |
||
317 | $fluidEmail->assignMultiple($this->options['variables']); |
||
318 | } |
||
319 | |||
320 | $fluidEmail |
||
321 | ->getViewHelperVariableContainer() |
||
322 | ->addOrUpdate(RenderRenderableViewHelper::class, 'formRuntime', $formRuntime); |
||
323 | |||
324 | return $fluidEmail; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Get mail recipients |
||
329 | * |
||
330 | * @param string $listOption List option name |
||
331 | * @return array |
||
332 | */ |
||
333 | protected function getRecipients(string $listOption): array |
||
345 | } |
||
346 | } |
||
347 |