Created
January 9, 2015 21:32
-
-
Save ricbra/7478a267296aeef45108 to your computer and use it in GitHub Desktop.
Example rabbitmq-cli-consumer command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Wb\Bundle\CoreBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class MailConsumerCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('wb:consumer:mail') | |
->addArgument('message', InputArgument::REQUIRED) | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$message = $input->getArgument('message'); | |
if (! $message = unserialize(base64_decode($message))) { | |
throw new \InvalidArgumentException('Invalid input received'); | |
} | |
$container = $this->getContainer(); | |
$mailer = $container->get('swiftmailer.mailer.mandrill'); | |
$transport = $container->get('swiftmailer.mailer.mandrill.transport.real'); | |
$logger = $container->get('rabbitmq_mail_logger'); | |
$logger->info(sprintf( | |
'[%s] [%s] Received message from queue', | |
$message->getSubject(), implode(', ', array_keys($message->getTo())) | |
)); | |
$spool = $mailer->getTransport()->getSpool(); | |
$mailer->send($message); | |
$spool->flushQueue($transport); | |
$logger->info(sprintf( | |
'[%s] [%s] Mail send', | |
$message->getSubject(), implode(', ', array_keys($message->getTo())) | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment