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) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Show a web-version of an email that has been sent to recipients and has been stored in the database. |
||
| 131 | * @param Request $request |
||
| 132 | * @param string $token |
||
| 133 | * @return Response |
||
| 134 | */ |
||
| 135 | 1 | public function webViewAction (Request $request, $token) |
|
|
|
|||
| 136 | { |
||
| 137 | // find email recipients, template & params |
||
| 138 | 1 | $sentEmail = $this->getSentEmailForToken($token); |
|
| 139 | |||
| 140 | // check if the sent email is available |
||
| 141 | 1 | if ($sentEmail !== null) { |
|
| 142 | |||
| 143 | // check if the current user is allowed to see the email |
||
| 144 | if ($this->userIsAllowedToSeeThisMail($sentEmail)) { |
||
| 145 | |||
| 146 | $template = $sentEmail->getTemplate(); |
||
| 147 | $emailVars = $sentEmail->getVariables(); |
||
| 148 | |||
| 149 | // re-attach all entities to the EntityManager. |
||
| 150 | $this->reAttachAllEntities($emailVars); |
||
| 151 | |||
| 152 | // remove the web-view-token from the param-array |
||
| 153 | $templateProvider = $this->getTemplateProviderService(); |
||
| 154 | unset($emailVars[$templateProvider->getWebViewTokenId()]); |
||
| 155 | |||
| 156 | // render & return email |
||
| 157 | $response = $this->renderResponse("$template.html.twig", $emailVars); |
||
| 158 | |||
| 159 | $campaignParams = $templateProvider->getCampaignParamsFor($template, $emailVars); |
||
| 160 | |||
| 161 | if (sizeof($campaignParams) > 0) { |
||
| 162 | $response->setContent($this->get("azine.email.bundle.twig.filters")->addCampaignParamsToAllUrls($response->getContent(), $campaignParams)); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $response; |
||
| 166 | |||
| 167 | // if the user is not allowed to see this mail |
||
| 168 | } else { |
||
| 169 | $msg = $this->get('translator')->trans('web.pre.view.test.mail.access.denied'); |
||
| 170 | throw new AccessDeniedException($msg); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // the parameters-array is null => the email is not available in webView |
||
| 175 | 1 | $days = $this->getParameter("azine_email_web_view_retention"); |
|
| 176 | 1 | $response = $this->renderResponse("AzineEmailBundle:Webview:mail.not.available.html.twig", array('days' => $days)); |
|
| 177 | 1 | $response->setStatusCode(404); |
|
| 178 | |||
| 179 | 1 | return $response; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if the user is allowed to see the email. |
||
| 184 | * => the mail is public or the user is among the recipients or the user is an admin. |
||
| 185 | * |
||
| 186 | * @param SentEmail $mail |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | private function userIsAllowedToSeeThisMail(SentEmail $mail) |
||
| 190 | { |
||
| 191 | $recipients = $mail->getRecipients(); |
||
| 192 | |||
| 193 | // it is a public email |
||
| 194 | if ($recipients === null) { |
||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | // get the current user |
||
| 199 | $currentUser = null; |
||
| 200 | if (!$this->has('security.token_storage')) { |
||
| 201 | // @codeCoverageIgnoreStart |
||
| 202 | throw new \LogicException('The SecurityBundle is not registered in your application.'); |
||
| 203 | // @codeCoverageIgnoreEnd |
||
| 204 | |||
| 205 | } else { |
||
| 206 | $token = $this->get('security.token_storage')->getToken(); |
||
| 207 | |||
| 208 | // check if the token is not null and the user in the token an object |
||
| 209 | if ($token instanceof TokenInterface && is_object($token->getUser())) { |
||
| 210 | $currentUser = $token->getUser(); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // it is not a public email, and a user is logged in |
||
| 215 | if ($currentUser !== null) { |
||
| 216 | |||
| 217 | // the user is among the recipients |
||
| 218 | if(array_search($currentUser->getEmail(), $recipients) !== false) |
||
| 219 | |||
| 220 | return true; |
||
| 221 | |||
| 222 | // the user is admin |
||
| 223 | if($currentUser->hasRole("ROLE_ADMIN")) |
||
| 224 | |||
| 225 | return true; |
||
| 226 | } |
||
| 227 | |||
| 228 | // not public email, but |
||
| 229 | // - there is no user, or |
||
| 230 | // - the user is not among the recipients and |
||
| 231 | // - the user not an admin-user either |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Replace all unmanaged Objects in the array (recursively) |
||
| 237 | * by managed Entities fetched via Doctrine EntityManager. |
||
| 238 | * |
||
| 239 | * It is assumed that managed objects can be identified |
||
| 240 | * by their id and implement the function getId() to get that id. |
||
| 241 | * |
||
| 242 | * @param array $vars passed by reference & manipulated but not returned. |
||
| 243 | * @return null |
||
| 244 | */ |
||
| 245 | private function reAttachAllEntities(array &$vars) |
||
| 246 | { |
||
| 247 | $em = $this->get('doctrine')->getManager(); |
||
| 248 | foreach ($vars as $key => $next) { |
||
| 249 | if (is_object($next) && method_exists($next, 'getId')) { |
||
| 250 | $className = get_class($next); |
||
| 251 | $managedEntity = $em->find($className, $next->getId()); |
||
| 252 | if ($managedEntity) { |
||
| 253 | $vars[$key] = $managedEntity; |
||
| 254 | } |
||
| 255 | continue; |
||
| 256 | } elseif (is_array($next)) { |
||
| 257 | $this->reAttachAllEntities($next); |
||
| 258 | continue; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Serve the image from the templates-folder |
||
| 266 | * @param Request $request |
||
| 267 | * @param string $folderKey |
||
| 268 | * @param string $filename |
||
| 269 | * @return BinaryFileResponse |
||
| 270 | */ |
||
| 271 | 2 | public function serveImageAction(Request $request, $folderKey, $filename) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @return TemplateProviderInterface |
||
| 288 | */ |
||
| 289 | 3 | protected function getTemplateProviderService() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $view |
||
| 296 | * @param array $parameters |
||
| 297 | * @param Response $response |
||
| 298 | * @return Response |
||
| 299 | */ |
||
| 300 | 2 | protected function renderResponse($view, array $parameters = array(), Response $response = null) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get the sent email from the database |
||
| 307 | * @param string $token the token identifying the sent email |
||
| 308 | * @return SentEmail |
||
| 309 | */ |
||
| 310 | 1 | protected function getSentEmailForToken($token) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Send a test-mail for the template to the given email-address |
||
| 319 | * @param Request $request |
||
| 320 | * @param string $template templateId without ending => AzineEmailBundle::baseEmailLayout (without .txt.twig) |
||
| 321 | * @param string $email |
||
| 322 | * @return RedirectResponse |
||
| 323 | */ |
||
| 324 | public function sendTestEmailAction(Request $request, $template, $email) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Make an RESTful call to http://spamcheck.postmarkapp.com/filter to test the emails-spam-index. |
||
| 386 | * See http://spamcheck.postmarkapp.com/doc |
||
| 387 | * @return array TestResult array('success', 'message', 'curlHttpCode', 'curlError', ['score', 'report']) |
||
| 388 | */ |
||
| 389 | public function getSpamIndexReportForSwiftMessage(\Swift_Message $message, $report = 'long') |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param $msgString |
||
| 396 | * @param string $report |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | private function getSpamIndexReport($msgString, $report = 'long') |
||
| 400 | { |
||
| 401 | // check if cURL is loaded/available |
||
| 402 | if (!function_exists('curl_init')) { |
||
| 403 | // @codeCoverageIgnoreStart |
||
| 404 | return array( "success" => false, |
||
| 405 | "curlHttpCode" => "-", |
||
| 406 | "curlError" => "-", |
||
| 407 | "message" => "No Spam-Check done. cURL module is not available.", |
||
| 408 | ); |
||
| 409 | // @codeCoverageIgnoreEnd |
||
| 410 | } |
||
| 411 | |||
| 412 | 1 | $ch = curl_init("http://spamcheck.postmarkapp.com/filter"); |
|
| 413 | 1 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 414 | 1 | curl_setopt($ch, CURLOPT_POST, true); |
|
| 415 | 1 | $data = array("email" => $msgString, "options" => $report); |
|
| 416 | 1 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
|
| 417 | 1 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json")); |
|
| 418 | 1 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); // max wait for 5sec for reply |
|
| 419 | |||
| 420 | 1 | $result = json_decode(curl_exec($ch), true); |
|
| 421 | 1 | $error = curl_error($ch); |
|
| 422 | 1 | $result['curlHttpCode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 423 | 1 | curl_close($ch); |
|
| 424 | |||
| 425 | 1 | if (strlen($error) > 0) { |
|
| 426 | $result['curlError'] = $error; |
||
| 427 | } |
||
| 428 | |||
| 429 | 1 | if (!array_key_exists("message", $result)) { |
|
| 430 | 1 | $result['message'] = "-"; |
|
| 431 | 1 | } |
|
| 432 | |||
| 433 | 1 | if (!array_key_exists('success', $result)) { |
|
| 434 | $result['message'] = "Something went wrong! Here's the content of the curl-reply:\n\n".nl2br(print_r($result, true)); |
||
| 435 | |||
| 436 | 1 | } elseif (!$result['success'] && strpos($msgString, "Content-Transfer-Encoding: base64") !== false) { |
|
| 437 | $result['message'] = $result['message']."\n\nRemoving the base64-Encoded Mime-Parts might help."; |
||
| 438 | |||
| 439 | } |
||
| 440 | |||
| 441 | 1 | return $result; |
|
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Ajax action to check the spam-score for the pasted email-source |
||
| 447 | */ |
||
| 448 | public function checkSpamScoreOfSentEmailAction(Request $request) |
||
| 478 | } |
||
| 479 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.