Complex classes like AzineEmailTemplateController 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 AzineEmailTemplateController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class AzineEmailTemplateController extends Controller |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * Show a set of options to view html- and text-versions of email in the browser and send them as emails to test-accounts |
||
37 | */ |
||
38 | 1 | public function indexAction(Request $request) |
|
52 | |||
53 | /** |
||
54 | * Show a web-preview-version of an email-template, filled with dummy-content |
||
55 | * @param string $format |
||
56 | * @return Response |
||
57 | */ |
||
58 | 1 | public function webPreViewAction(Request $request, $template, $format = null) |
|
130 | |||
131 | /** |
||
132 | * Show a web-version of an email that has been sent to recipients and has been stored in the database. |
||
133 | * @param Request $request |
||
134 | * @param string $token |
||
135 | * @return Response |
||
136 | */ |
||
137 | 6 | public function webViewAction (Request $request, $token) |
|
|
|||
138 | { |
||
139 | // find email recipients, template & params |
||
140 | 6 | $sentEmail = $this->getSentEmailForToken($token); |
|
141 | |||
142 | // check if the sent email is available |
||
143 | 6 | if ($sentEmail !== null) { |
|
144 | |||
145 | // check if the current user is allowed to see the email |
||
146 | 5 | if ($this->userIsAllowedToSeeThisMail($sentEmail)) { |
|
147 | |||
148 | 3 | $template = $sentEmail->getTemplate(); |
|
149 | 3 | $emailVars = $sentEmail->getVariables(); |
|
150 | |||
151 | // re-attach all entities to the EntityManager. |
||
152 | 3 | $this->reAttachAllEntities($emailVars); |
|
153 | |||
154 | // remove the web-view-token from the param-array |
||
155 | 3 | $templateProvider = $this->getTemplateProviderService(); |
|
156 | 3 | unset($emailVars[$templateProvider->getWebViewTokenId()]); |
|
157 | |||
158 | // render & return email |
||
159 | 3 | $response = $this->renderResponse("$template.html.twig", $emailVars); |
|
160 | |||
161 | 3 | $campaignParams = $templateProvider->getCampaignParamsFor($template, $emailVars); |
|
162 | |||
163 | 3 | if ($campaignParams != null && sizeof($campaignParams) > 0) { |
|
164 | 1 | $response->setContent($this->get("azine.email.bundle.twig.filters")->addCampaignParamsToAllUrls($response->getContent(), $campaignParams)); |
|
165 | 1 | } |
|
166 | |||
167 | 3 | return $response; |
|
168 | |||
169 | // if the user is not allowed to see this mail |
||
170 | } else { |
||
171 | 2 | $msg = $this->get('translator')->trans('web.pre.view.test.mail.access.denied'); |
|
172 | 2 | throw new AccessDeniedException($msg); |
|
173 | } |
||
174 | } |
||
175 | |||
176 | // the parameters-array is null => the email is not available in webView |
||
177 | 1 | $days = $this->getParameter("azine_email_web_view_retention"); |
|
178 | 1 | $response = $this->renderResponse("AzineEmailBundle:Webview:mail.not.available.html.twig", array('days' => $days)); |
|
179 | 1 | $response->setStatusCode(404); |
|
180 | |||
181 | 1 | return $response; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Check if the user is allowed to see the email. |
||
186 | * => the mail is public or the user is among the recipients or the user is an admin. |
||
187 | * |
||
188 | * @param SentEmail $mail |
||
189 | * @return boolean |
||
190 | */ |
||
191 | 5 | private function userIsAllowedToSeeThisMail(SentEmail $mail) |
|
236 | |||
237 | /** |
||
238 | * Replace all unmanaged Objects in the array (recursively) |
||
239 | * by managed Entities fetched via Doctrine EntityManager. |
||
240 | * |
||
241 | * It is assumed that managed objects can be identified |
||
242 | * by their id and implement the function getId() to get that id. |
||
243 | * |
||
244 | * @param array $vars passed by reference & manipulated but not returned. |
||
245 | * @return null |
||
246 | */ |
||
247 | 3 | private function reAttachAllEntities(array &$vars) |
|
265 | |||
266 | /** |
||
267 | * Serve the image from the templates-folder |
||
268 | * @param Request $request |
||
269 | * @param string $folderKey |
||
270 | * @param string $filename |
||
271 | * @return BinaryFileResponse |
||
272 | */ |
||
273 | 2 | public function serveImageAction(Request $request, $folderKey, $filename) |
|
287 | |||
288 | /** |
||
289 | * @return TemplateProviderInterface |
||
290 | */ |
||
291 | 6 | protected function getTemplateProviderService() |
|
295 | |||
296 | /** |
||
297 | * @param string $view |
||
298 | * @param array $parameters |
||
299 | * @param Response $response |
||
300 | * @return Response |
||
301 | */ |
||
302 | 5 | protected function renderResponse($view, array $parameters = array(), Response $response = null) |
|
306 | |||
307 | /** |
||
308 | * Get the sent email from the database |
||
309 | * @param string $token the token identifying the sent email |
||
310 | * @return SentEmail |
||
311 | */ |
||
312 | 6 | protected function getSentEmailForToken($token) |
|
318 | |||
319 | /** |
||
320 | * Send a test-mail for the template to the given email-address |
||
321 | * @param Request $request |
||
322 | * @param string $template templateId without ending => AzineEmailBundle::baseEmailLayout (without .txt.twig) |
||
323 | * @param string $email |
||
324 | * @return RedirectResponse |
||
325 | */ |
||
326 | public function sendTestEmailAction(Request $request, $template, $email) |
||
387 | |||
388 | /** |
||
389 | * Make an RESTful call to http://spamcheck.postmarkapp.com/filter to test the emails-spam-index. |
||
390 | * See http://spamcheck.postmarkapp.com/doc |
||
391 | * @return array TestResult array('success', 'message', 'curlHttpCode', 'curlError', ['score', 'report']) |
||
392 | */ |
||
393 | public function getSpamIndexReportForSwiftMessage(\Swift_Message $message, $report = 'long') |
||
397 | |||
398 | /** |
||
399 | * @param $msgString |
||
400 | * @param string $report |
||
401 | * @return mixed |
||
402 | */ |
||
403 | private function getSpamIndexReport($msgString, $report = 'long') |
||
404 | { |
||
405 | // check if cURL is loaded/available |
||
406 | if (!function_exists('curl_init')) { |
||
407 | // @codeCoverageIgnoreStart |
||
408 | return array( "success" => false, |
||
409 | "curlHttpCode" => "-", |
||
410 | "curlError" => "-", |
||
411 | "message" => "No Spam-Check done. cURL module is not available.", |
||
412 | ); |
||
413 | // @codeCoverageIgnoreEnd |
||
414 | } |
||
415 | |||
416 | $ch = curl_init("http://spamcheck.postmarkapp.com/filter"); |
||
417 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
418 | curl_setopt($ch, CURLOPT_POST, true); |
||
419 | $data = array("email" => $msgString, "options" => $report); |
||
420 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
||
421 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json")); |
||
422 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); // max wait for 5sec for reply |
||
423 | |||
424 | $result = json_decode(curl_exec($ch), true); |
||
425 | $error = curl_error($ch); |
||
426 | $result['curlHttpCode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
427 | curl_close($ch); |
||
428 | |||
429 | if (strlen($error) > 0) { |
||
430 | $result['curlError'] = $error; |
||
431 | } |
||
432 | |||
433 | if (!array_key_exists("message", $result)) { |
||
434 | $result['message'] = "-"; |
||
435 | } |
||
436 | |||
437 | if (!array_key_exists('success', $result)) { |
||
438 | $result['message'] = "Something went wrong! Here's the content of the curl-reply:\n\n".nl2br(print_r($result, true)); |
||
439 | |||
440 | } elseif (!$result['success'] && strpos($msgString, "Content-Transfer-Encoding: base64") !== false) { |
||
441 | $result['message'] = $result['message']."\n\nRemoving the base64-Encoded Mime-Parts might help."; |
||
442 | |||
443 | } |
||
444 | |||
445 | return $result; |
||
446 | |||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Ajax action to check the spam-score for the pasted email-source |
||
451 | */ |
||
452 | public function checkSpamScoreOfSentEmailAction(Request $request) |
||
482 | } |
||
483 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.