Skip to content

Commit

Permalink
Fixed Swift Mailer integration
Browse files Browse the repository at this point in the history
  • Loading branch information
snc committed Oct 2, 2017
1 parent ba3903f commit 3fa62af
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 11 deletions.
46 changes: 46 additions & 0 deletions DependencyInjection/Compiler/SwiftMailerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the SncRedisBundle package.
*
* (c) Henrik Westphal <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Snc\RedisBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class SwiftMailerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$serviceId = 'snc_redis.swiftmailer.spool';
if ($container->hasDefinition($serviceId)) {
$handlerDefinition = $container->getDefinition($serviceId);
if ('Snc\\RedisBundle\\SwiftMailer\\RedisSpool' === $handlerDefinition->getClass()) {
// default class, lets check for Swift Mailer version and set correct class
$class = new \ReflectionClass('\\Swift_Spool');
$parameters = $class->getMethod('queueMessage')->getParameters();
switch ($parameters[0]->getClass()->getName()) {
case 'Swift_Mime_Message':
// Swift Mailer 5.x
$handlerDefinition->setClass($handlerDefinition->getClass().'5');
break;
case 'Swift_Mime_SimpleMessage':
// Swift Mailer 6.x
$handlerDefinition->setClass($handlerDefinition->getClass().'6');
break;
default:
throw new \RuntimeException('Failed to detect the current Swift Mailer version.');
}
}
}
}
}
2 changes: 2 additions & 0 deletions SncRedisBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Snc\RedisBundle\DependencyInjection\Compiler\LoggingPass;
use Snc\RedisBundle\DependencyInjection\Compiler\MonologPass;
use Snc\RedisBundle\DependencyInjection\Compiler\SwiftMailerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -29,5 +30,6 @@ public function build(ContainerBuilder $container)
parent::build($container);
$container->addCompilerPass(new LoggingPass());
$container->addCompilerPass(new MonologPass());
$container->addCompilerPass(new SwiftMailerPass());
}
}
12 changes: 1 addition & 11 deletions SwiftMailer/RedisSpool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* RedisSpool
*/
class RedisSpool extends \Swift_ConfigurableSpool
abstract class RedisSpool extends \Swift_ConfigurableSpool
{
/**
* @var string
Expand Down Expand Up @@ -64,16 +64,6 @@ public function isStarted()
return true;
}

/**
* {@inheritdoc}
*/
public function queueMessage(\Swift_Mime_SimpleMessage $message)
{
$this->redis->rpush($this->key, serialize($message));

return true;
}

/**
* {@inheritdoc}
*/
Expand Down
28 changes: 28 additions & 0 deletions SwiftMailer/RedisSpool5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the SncRedisBundle package.
*
* (c) Henrik Westphal <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Snc\RedisBundle\SwiftMailer;

/**
* RedisSpool5 (Swift Mailer 5.x)
*/
class RedisSpool5 extends RedisSpool
{
/**
* {@inheritdoc}
*/
public function queueMessage(\Swift_Mime_Message $message)
{
$this->redis->rpush($this->key, serialize($message));

return true;
}
}
28 changes: 28 additions & 0 deletions SwiftMailer/RedisSpool6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the SncRedisBundle package.
*
* (c) Henrik Westphal <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Snc\RedisBundle\SwiftMailer;

/**
* RedisSpool6 (Swift Mailer 6.x)
*/
class RedisSpool6 extends RedisSpool
{
/**
* {@inheritdoc}
*/
public function queueMessage(\Swift_Mime_SimpleMessage $message)
{
$this->redis->rpush($this->key, serialize($message));

return true;
}
}

0 comments on commit 3fa62af

Please sign in to comment.