Skip to content

Commit

Permalink
Merge branch '3.0' into 3.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	src/TransportManager.php

Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Feb 12, 2016
2 parents ecfbd13 + 0234059 commit 9f75802
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 76 deletions.
4 changes: 4 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ title: Notifier Change Log

## Version 3.0 {#v3-0}

### v3.0.2 {#v3-0-2}

* Add `Orchestra\Notifier\TransportManager::getConfig()` and `Orchestra\Notifier\TransportManager::getSecureConfig()`.

### v3.0.1 {#v3-0-1}

* Add Amazon SES Transport support.
Expand Down
72 changes: 31 additions & 41 deletions src/TransportManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,11 @@
use Illuminate\Mail\Transport\MailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;
use Illuminate\Contracts\Encryption\DecryptException;

class TransportManager extends Manager
{
use ContainerTrait;

/**
* Create a new manager instance.
*
* @param \Illuminate\Foundation\Application $app
*/
public function __construct($app)
{
$this->app = $app;
$this->encrypter = $app->make('encrypter');
}

/**
* Register the SMTP Swift Transport instance.
*
Expand All @@ -49,7 +37,7 @@ protected function createSmtpDriver()
// transporter instance so that we'll properly authenticate delivery.
if (isset($config['username'])) {
$transport->setUsername($config['username']);
$transport->setPassword($this->getDecryptedConfig($config['password']));
$transport->setPassword($this->getSecureConfig('password'));
}

return $transport;
Expand All @@ -62,9 +50,7 @@ protected function createSmtpDriver()
*/
protected function createSendmailDriver()
{
$config = $this->getTransportConfig();

return SendmailTransport::newInstance($config['sendmail']);
return SendmailTransport::newInstance($this->getConfig('sendmail'));
}

/**
Expand All @@ -74,14 +60,12 @@ protected function createSendmailDriver()
*/
protected function createSesDriver()
{
$config = $this->getTransportConfig();

$client = new SesClient([
'credentials' => [
'key' => $this->getDecryptedConfig($config['key']),
'secret' => $this->getDecryptedConfig($config['secret']),
'key' => $this->getSecureConfig('key'),
'secret' => $this->getSecureConfig('secret'),
],
'region' => $config['region'],
'region' => $this->getConfig('region'),
'service' => 'email',
'version' => 'latest',
]);
Expand All @@ -106,11 +90,9 @@ protected function createMailDriver()
*/
protected function createMailgunDriver()
{
$config = $this->getTransportConfig();

$client = new HttpClient(Arr::get($config, 'guzzle', []));
$client = new HttpClient($this->getConfig('guzzle', []));

return new MailgunTransport($client, $this->getDecryptedConfig($config['secret']), $config['domain']);
return new MailgunTransport($client, $this->getSecureConfig('secret'), $this->getConfig('domain'));
}

/**
Expand All @@ -120,11 +102,9 @@ protected function createMailgunDriver()
*/
protected function createMandrillDriver()
{
$config = $this->getTransportConfig();

$client = new HttpClient(Arr::get($config, 'guzzle', []));
$client = new HttpClient($this->getConfig('guzzle', []));

return new MandrillTransport($client, $this->getDecryptedConfig($config['secret']));
return new MandrillTransport($client, $this->getSecureConfig('secret'));
}

/**
Expand All @@ -148,28 +128,38 @@ protected function getTransportConfig()
}

/**
* Get the default driver name.
* Get transport configuration.
*
* @return string
* @param string $key
* @param mixed $default
*
* @return array
*/
public function getDefaultDriver()
public function getConfig($key, $default = null)
{
return $this->memory->get('email.driver', 'mail');
return $this->memory->get("email.{$key}", $default);
}

/**
* Get decrypted configuration value.
* Get transport encrypted configuration.
*
* @param string|null $key
* @param mixed $default
*
* @param string $value
* @return array
*/
public function getSecureConfig($key = null, $default = null)
{
return $this->memory->secureGet("email.{$key}", $default);
}

/**
* Get the default driver name.
*
* @return string
*/
public function getDecryptedConfig($value)
public function getDefaultDriver()
{
try {
return $this->encrypter->decrypt($value);
} catch (DecryptException $e) {
return $value;
}
return $this->memory->get('email.driver', 'mail');
}
}
39 changes: 5 additions & 34 deletions tests/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function testPushMethodUsesSend()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

Expand Down Expand Up @@ -57,7 +56,6 @@ public function testPushMethodUsesQueue()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));
$app->instance('queue', $queue = m::mock('QueueListener'));
Expand Down Expand Up @@ -95,7 +93,6 @@ public function testSendMethod()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

Expand Down Expand Up @@ -124,7 +121,6 @@ public function testSendMethodViaMail()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

Expand Down Expand Up @@ -153,14 +149,10 @@ public function testSendMethodViaSendMail()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

$memory->shouldReceive('get')->with('email', [])->andReturn([
'driver' => 'sendmail',
'sendmail' => '/bin/sendmail -t',
])
$memory->shouldReceive('get')->with('email.sendmail', null)->andReturn('/bin/sendmail -t')
->shouldReceive('get')->with('email.driver', 'mail')->andReturn('sendmail')
->shouldReceive('get')->with('email.from')->andReturn([
'address' => 'hello@orchestraplatform.com',
Expand All @@ -185,18 +177,16 @@ public function testSendMethodViaSmtp()
{
$app = new Container();

$app->instance('encrypter', $encrypter = m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

$memory->shouldReceive('get')->with('email', [])->andReturn([
'driver' => 'smtp',
'host' => 'smtp.mailgun.org',
'port' => 587,
'encryption' => 'tls',
'username' => 'hello@orchestraplatform.com',
'password' => 123456,
])
->shouldReceive('secureGet')->with('email.password', null)->andReturn(123456)
->shouldReceive('get')->with('email.driver', 'mail')->andReturn('smtp')
->shouldReceive('get')->with('email.from')->andReturn([
'address' => 'hello@orchestraplatform.com',
Expand All @@ -207,8 +197,6 @@ public function testSendMethodViaSmtp()
->shouldReceive('alwaysFrom')->once()->with('hello@orchestraplatform.com', 'Orchestra Platform')
->shouldReceive('send')->once()->with('foo.bar', ['foo' => 'foobar'], '')->andReturn(true);

$encrypter->shouldReceive('decrypt')->once()->with(123456)->andReturn(123456);

$transport = new TransportManager($app);
$stub = with(new Mailer($app, $transport))->attach($memory);
$this->assertInstanceOf('\Orchestra\Notifier\Receipt', $stub->send('foo.bar', ['foo' => 'foobar'], ''));
Expand All @@ -223,16 +211,12 @@ public function testSendMethodViaMailgun()
{
$app = new Container();

$app->instance('encrypter', $encrypter = m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

$memory->shouldReceive('get')->with('email', [])->andReturn([
'driver' => 'mailgun',
'secret' => 'auniquetoken',
'domain' => 'mailer.mailgun.org',
])
$memory->shouldReceive('secureGet')->with('email.secret', null)->andReturn('auniquetoken')
->shouldReceive('get')->with('email.driver', 'mail')->andReturn('mailgun')
->shouldReceive('get')->with('email.domain', null)->andReturn('mailer.mailgun.org')
->shouldReceive('get')->with('email.from')->andReturn([
'address' => 'hello@orchestraplatform.com',
'name' => 'Orchestra Platform',
Expand All @@ -242,8 +226,6 @@ public function testSendMethodViaMailgun()
->shouldReceive('alwaysFrom')->once()->with('hello@orchestraplatform.com', 'Orchestra Platform')
->shouldReceive('send')->once()->with('foo.bar', ['foo' => 'foobar'], '')->andReturn(true);

$encrypter->shouldReceive('decrypt')->once()->with('auniquetoken')->andReturn('auniquetoken');

$transport = new TransportManager($app);
$stub = with(new Mailer($app, $transport))->attach($memory);
$this->assertInstanceOf('\Orchestra\Notifier\Receipt', $stub->send('foo.bar', ['foo' => 'foobar'], ''));
Expand All @@ -258,14 +240,10 @@ public function testSendMethodViaMandrill()
{
$app = new Container();

$app->instance('encrypter', $encrypter = m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

$memory->shouldReceive('get')->with('email', [])->andReturn([
'driver' => 'mandrill',
'secret' => 'auniquetoken',
])
$memory->shouldReceive('secureGet')->with('email.secret', null)->andReturn('auniquetoken')
->shouldReceive('get')->with('email.driver', 'mail')->andReturn('mandrill')
->shouldReceive('get')->with('email.from')->andReturn([
'address' => 'hello@orchestraplatform.com',
Expand All @@ -276,8 +254,6 @@ public function testSendMethodViaMandrill()
->shouldReceive('alwaysFrom')->once()->with('hello@orchestraplatform.com', 'Orchestra Platform')
->shouldReceive('send')->once()->with('foo.bar', ['foo' => 'foobar'], '')->andReturn(true);

$encrypter->shouldReceive('decrypt')->once()->with('auniquetoken')->andReturn('auniquetoken');

$transport = new TransportManager($app);
$stub = with(new Mailer($app, $transport))->attach($memory);
$this->assertInstanceOf('\Orchestra\Notifier\Receipt', $stub->send('foo.bar', ['foo' => 'foobar'], ''));
Expand All @@ -294,7 +270,6 @@ public function testSendMethodViaLog()

$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));
$app->instance('log', $logger = m::mock('\Illuminate\Log\Writer'));
Expand Down Expand Up @@ -327,7 +302,6 @@ public function testSendMethodViaInvalidDriverThrowsException()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

Expand Down Expand Up @@ -356,7 +330,6 @@ public function testQueueMethod()
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));
$app->instance('queue', $queue = m::mock('QueueListener'));
Expand Down Expand Up @@ -393,7 +366,6 @@ public function testQueueMethod()
public function testQueueMethodWhenClassNameIsGiven()
{$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));
$app->instance('queue', $queue = m::mock('QueueListener'));
Expand Down Expand Up @@ -455,7 +427,6 @@ public function testHandleQueuedMessageMethod($view, $data, $callback)
{
$app = new Container();

$app->instance('encrypter', m::mock('\Illuminate\Contracts\Encryption\Encrypter'));
$app->instance('orchestra.memory', $memory = m::mock('\Orchestra\Contracts\Memory\Provider'));
$app->instance('mailer', $mailer = m::mock('\Illuminate\Contracts\Mail\Mailer'));

Expand Down
1 change: 0 additions & 1 deletion tests/NotifierServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function testRegisterMethod()
->andReturnUsing(function ($n, $c) use ($app) {
return $c($app);
})
->shouldReceive('make')->once()->with('encrypter')->andReturn(m::mock('\Illuminate\Contracts\Encryption\Encrypter'))
->shouldReceive('singleton')->once()->with('orchestra.notifier', m::type('Closure'))
->andReturnUsing(function ($n, $c) use ($app) {
return $c($app);
Expand Down

0 comments on commit 9f75802

Please sign in to comment.