| Conditions | 10 |
| Paths | 60 |
| Total Lines | 49 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 46 | #[Route('/testmail', name: 'zikulathemebundle_tool_testmail')] |
||
| 47 | public function test( |
||
| 48 | Request $request, |
||
| 49 | MailerInterface $mailer, |
||
| 50 | RateLimiterFactory $testMailsLimiter, |
||
| 51 | LoggerInterface $mailLogger // $mailLogger var name auto-injects the mail channel handler |
||
| 52 | ): Response { |
||
| 53 | $form = $this->createForm(MailTestType::class, $this->getDataValues()); |
||
| 54 | $form->handleRequest($request); |
||
| 55 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 56 | if ($form->get('test')->isClicked()) { |
||
|
|
|||
| 57 | $limiter = $testMailsLimiter->create($request->getClientIp()); |
||
| 58 | if (false === $limiter->consume(1)->isAccepted()) { |
||
| 59 | throw new TooManyRequestsHttpException(); |
||
| 60 | } |
||
| 61 | |||
| 62 | $formData = $form->getData(); |
||
| 63 | $html = in_array($formData['messageType'], ['html', 'multipart']) ? true : false; |
||
| 64 | try { |
||
| 65 | $email = (new Email()) |
||
| 66 | ->from(new Address($formData['adminmail'], $formData['sitename'])) |
||
| 67 | ->to(new Address($formData['toAddress'], $formData['toName'])) |
||
| 68 | ->subject($formData['subject']) |
||
| 69 | ->text($formData['bodyText']) |
||
| 70 | ; |
||
| 71 | if ($html) { |
||
| 72 | $email->html($formData['bodyHtml']); |
||
| 73 | } |
||
| 74 | $mailer->send($email); |
||
| 75 | if ($this->mailLoggingEnabled) { |
||
| 76 | $mailLogger->info(sprintf('Email sent to %s', $formData['toAddress']), [ |
||
| 77 | 'in' => __METHOD__, |
||
| 78 | ]); |
||
| 79 | } |
||
| 80 | $this->addFlash('status', 'Done! Message sent.'); |
||
| 81 | } catch (TransportExceptionInterface $exception) { |
||
| 82 | $mailLogger->error($exception->getMessage(), [ |
||
| 83 | 'in' => __METHOD__, |
||
| 84 | ]); |
||
| 85 | $this->addFlash('error', $exception->getCode() . ': ' . $exception->getMessage()); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | if ($form->get('cancel')->isClicked()) { |
||
| 89 | $this->addFlash('status', 'Operation cancelled.'); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->render('@ZikulaTheme/Tool/testmail.html.twig', [ |
||
| 94 | 'form' => $form, |
||
| 95 | ]); |
||
| 133 |