| Conditions | 18 |
| Paths | 20 |
| Total Lines | 90 |
| Code Lines | 61 |
| 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 |
||
| 37 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 38 | { |
||
| 39 | /** @var \Symfony\Component\Routing\RequestContext $context */ |
||
| 40 | $context = $this->getContainer()->get('router')->getContext(); |
||
| 41 | $context->setHost('fwdays.com'); |
||
| 42 | $context->setScheme('http'); |
||
| 43 | |||
| 44 | $limit = 10; |
||
| 45 | |||
| 46 | if ($input->getOption('amount')) { |
||
| 47 | $limit = (int) $input->getOption('amount'); |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($input->getOption('host')) { |
||
| 51 | $context->setHost($input->getOption('host')); |
||
|
|
|||
| 52 | } |
||
| 53 | |||
| 54 | /** @var $em \Doctrine\ORM\EntityManager */ |
||
| 55 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
| 56 | /** @var $mailer MyMailer */ |
||
| 57 | $mailer = $this->getContainer()->get('app.my_mailer.service'); |
||
| 58 | /** @var $mailerHelper \Stfalcon\Bundle\EventBundle\Helper\StfalconMailerHelper */ |
||
| 59 | $mailerHelper = $this->getContainer()->get('stfalcon_event.mailer_helper'); |
||
| 60 | /** @var $queueRepository \Stfalcon\Bundle\EventBundle\Repository\MailQueueRepository */ |
||
| 61 | $queueRepository = $em->getRepository('StfalconEventBundle:MailQueue'); |
||
| 62 | |||
| 63 | $mailsQueue = $queueRepository->getMessages($limit); |
||
| 64 | $logger = $this->getContainer()->get('logger'); |
||
| 65 | /* @var $mail Mail */ |
||
| 66 | foreach ($mailsQueue as $item) { |
||
| 67 | $user = $item->getUser(); |
||
| 68 | $mail = $item->getMail(); |
||
| 69 | |||
| 70 | if (!( |
||
| 71 | $user && |
||
| 72 | $mail && |
||
| 73 | $user->isEnabled() && |
||
| 74 | $user->isSubscribe() && |
||
| 75 | $user->isEmailExists() && |
||
| 76 | filter_var($user->getEmail(), FILTER_VALIDATE_EMAIL) |
||
| 77 | )) { |
||
| 78 | $mail->decTotalMessages(); |
||
| 79 | $em->remove($item); |
||
| 80 | $em->flush(); |
||
| 81 | continue; |
||
| 82 | } |
||
| 83 | |||
| 84 | try { |
||
| 85 | $message = $mailerHelper->formatMessage($user, $mail); |
||
| 86 | } catch (\Exception $e) { |
||
| 87 | $logger->addError('Mailer:'.$e->getMessage(), ['email' => $user->getEmail()]); |
||
| 88 | |||
| 89 | $mail->decTotalMessages(); |
||
| 90 | $em->remove($item); |
||
| 91 | $em->flush(); |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | $headers = $message->getHeaders(); |
||
| 96 | $http = $this->getContainer()->get('router')->generate( |
||
| 97 | 'unsubscribe', |
||
| 98 | [ |
||
| 99 | 'hash' => $user->getSalt(), |
||
| 100 | 'userId' => $user->getId(), |
||
| 101 | 'mailId' => $mail->getId(), |
||
| 102 | ], |
||
| 103 | true |
||
| 104 | ); |
||
| 105 | |||
| 106 | $headers->removeAll('List-Unsubscribe'); |
||
| 107 | $headers->addTextHeader('List-Unsubscribe', '<'.$http.'>'); |
||
| 108 | $failed = []; |
||
| 109 | if ($mailer->send($message, $failed)) { |
||
| 110 | $mail->incSentMessage(); |
||
| 111 | $item->setIsSent(true); |
||
| 112 | $em->flush(); |
||
| 113 | } else { |
||
| 114 | $logger->addError('Mailer send exception', [ |
||
| 115 | 'mail_id' => $mail->getId(), |
||
| 116 | 'user_id' => $user->getId(), |
||
| 117 | 'error_swift_message' => isset($failed['error_swift_message']) ? $failed['error_swift_message'] : '', |
||
| 118 | 'error_swift_code' => isset($failed['error_swift_code']) ? $failed['error_swift_code'] : '', |
||
| 119 | 'error_swift_trace' => isset($failed['error_swift_trace']) ? $failed['error_swift_trace'] : '', |
||
| 120 | 'error_exception_message' => isset($failed['error_exception_message']) ? $failed['error_exception_message'] : '', |
||
| 121 | 'error_exception_code' => isset($failed['error_exception_code']) ? $failed['error_exception_code'] : '', |
||
| 122 | 'error_exception_trace' => isset($failed['error_exception_trace']) ? $failed['error_exception_trace'] : '', |
||
| 123 | ]); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | $em->flush(); |
||
| 127 | } |
||
| 129 |