diff --git a/EventListener/SwiftmailerEmailSenderListener.php b/EventListener/SwiftmailerEmailSenderListener.php new file mode 100644 index 0000000..824e7d4 --- /dev/null +++ b/EventListener/SwiftmailerEmailSenderListener.php @@ -0,0 +1,67 @@ + + */ + +namespace Mmoreram\GearmanBundle\EventListener; + +use Mmoreram\GearmanBundle\GearmanEvents; +use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Sends emails for the memory spool. + * + * Emails are sent on the gearman.client.callback.complete event. + * + * @author Dominic Luechinger + */ +class SwiftmailerEmailSenderListener implements EventSubscriberInterface +{ + + /** + * @var ContainerInterface + */ + private $container; + + /** + * Set container + * + * @param ContainerInterface $container Container + * + * @return GearmanExecute self Object + */ + public function setContainer(ContainerInterface $container) + { + $this->container = $container; + + return $this; + } + + public function onComplete() + { + if (!$this->container->has('swiftmailer.email_sender.listener')) { + return; + } + $this->container->get('swiftmailer.email_sender.listener')->onTerminate(); + } + + /** + * {@inheritDoc} + */ + public static function getSubscribedEvents() + { + return array( + GearmanEvents::GEARMAN_CLIENT_CALLBACK_COMPLETE => 'onComplete', + ); + } +} diff --git a/Resources/config/classes.yml b/Resources/config/classes.yml index 24ff429..665b632 100644 --- a/Resources/config/classes.yml +++ b/Resources/config/classes.yml @@ -25,6 +25,11 @@ parameters: gearman.parser.class: Mmoreram\GearmanBundle\Service\GearmanParser gearman.cache.wrapper.class: Mmoreram\GearmanBundle\Service\GearmanCacheWrapper + # + # EventListeners + # + gearman.swiftmailer_email_sender.listener.class: Mmoreram\GearmanBundle\EventListener\SwiftmailerEmailSenderListener + # # Commands # diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 1d20d85..c303f51 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -41,7 +41,13 @@ services: parent: gearman.abstract.service calls: - [setContainer, ["@service_container"]] - - [setEventDispatcher, ["@event_dispatcher"]] + + gearman.swiftmailer_email_sender.listener: + class: "%gearman.swiftmailer_email_sender.listener.class%" + calls: + - [setContainer, ["@service_container"]] + tags: + - { name: kernel.event_subscriber } gearman: class: "%gearman.client.class%"