Skip to content

Commit

Permalink
Replace mail driver with listener. (#17)
Browse files Browse the repository at this point in the history
Closes #17 #14
  • Loading branch information
jstoone authored Nov 25, 2018
1 parent b6aaf36 commit f58394b
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 165 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed
- Stored UUID's of mails are now orderd by time
- Replaced mail driver with event listener

### Removed

Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public function tools()
}
```

And as a last step, switch to use Mailman as your mail driver:

```bash
MAIL_DRIVER=mailman
```

## Usage

Click on the "nova-mailman" menu item in your Nova app to see the tool provided by this package.
Expand Down
62 changes: 25 additions & 37 deletions src/Mailer/MailmanTransport.php → src/DeliverToInbox.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
namespace Jstoone\Mailman\Mailer;

namespace Jstoone\Mailman;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Mail\Transport\Transport;
use Swift_Mime_SimpleMessage;
use Illuminate\Mail\Events\MessageSent;
use Swift_Message;

class MailmanTransport extends Transport
class DeliverToInbox
{
/**
* @var Filesystem
Expand All @@ -15,53 +16,29 @@ class MailmanTransport extends Transport
/**
* @var string
*/
protected $mailboxPath;
protected $identifier;

/**
* @var string
*/
protected $identifier;
protected $mailboxPath;

public function __construct(Filesystem $files)
{
$this->files = $files;
$this->mailboxPath = 'mailman';
}

/**
* Iterate through registered plugins and execute plugins' methods.
*
* @param Swift_Mime_SimpleMessage $message
*
* @return void
*/
protected function beforeSendPerformed(Swift_Mime_SimpleMessage $message)
{
parent::beforeSendPerformed($message);

$this->identifier = MailIdentifier::generate();
$this->mailboxPath = 'mailman';
}

/**
* Store the mail safely in a mailbox, and inform Nova.
*/
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
public function handle(MessageSent $event): void
{
$this->beforeSendPerformed($message);

$this->createMailbox();

// Create html file
$this->files->put(
$this->getMailPath() . '.blade.php',
$this->getMailContent($message)
);
$this->storeMailContent($event->message);

// Create json file
$this->files->put(
$this->getMailPath() . '.json',
$this->getMailMetadata($message)
);
$this->storeMailMetadata($event->message);
}

/**
Expand All @@ -88,15 +65,26 @@ protected function getMailPath(): string
/**
* Get the content from the given mail message.
*/
protected function getMailContent(Swift_Mime_SimpleMessage $message): string
protected function storeMailContent(Swift_Message $message): void
{
return $message->getBody();
$this->files->put(
$this->getMailPath() . '.blade.php',
$message->getBody()
);
}

/**
* Get the metadata for the given mail message.
*/
protected function getMailMetadata(Swift_Mime_SimpleMessage $message): string
protected function storeMailMetadata(Swift_Message $message): void
{
$this->files->put(
$this->getMailPath() . '.json',
$this->getMetadata($message)
);
}

protected function getMetadata(Swift_Message $message): string
{
return json_encode([
'id' => $this->identifier,
Expand Down
4 changes: 1 addition & 3 deletions src/Mailer/MailIdentifier.php → src/MailIdentifier.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

namespace Jstoone\Mailman\Mailer;

use Jstoone\Mailman\GenerateMailIdentifier;
namespace Jstoone\Mailman;

class MailIdentifier
{
Expand Down
23 changes: 0 additions & 23 deletions src/Mailer/MailProvider.php

This file was deleted.

16 changes: 3 additions & 13 deletions src/MailmanServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Jstoone\Mailman;

use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use Jstoone\Mailman\Http\Middleware\Authorize;
use Jstoone\Mailman\Mailer\MailProvider;

class MailmanServiceProvider extends ServiceProvider
{
Expand All @@ -19,6 +19,8 @@ public function boot()
{
$this->views();

$this->app->make('events')->listen(MessageSent::class, DeliverToInbox::class);

$this->app->booted(function () {
$this->routes();
});
Expand Down Expand Up @@ -46,16 +48,4 @@ protected function routes()
->prefix('nova-vendor/jstoone/nova-mailman')
->group(__DIR__ . '/../routes/api.php');
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
if (config('mail.driver') === 'mailman') {
$this->app->register(MailProvider::class);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
<?php

namespace Jstoone\Mailman\Tests\Mailer;
namespace Jstoone\Mailman\Tests;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\View\View;
use Jstoone\Mailman\GenerateMailIdentifier;
use Jstoone\Mailman\Mailer\MailmanTransport;
use Jstoone\Mailman\Tests\TestCase;
use Swift_Message;

class MailmanTransportTest extends TestCase
class DeliverToInboxTest extends TestCase
{
/** @test */
public function it_creates_a_directory_for_storing_emails()
{
$message = new Swift_Message('Mail Subject', 'Mail Body');
$message->setTo('[email protected]');

$transport = $this->app->make(MailmanTransport::class);

$transport->send($message);
$this->sendMail('Mail Subject', '[email protected]');

$this->assertTrue(
app(Filesystem::class)->exists('mailman')
Expand All @@ -33,7 +25,7 @@ public function it_stores_emails_as_blade_views()
return 'unique-identifier';
});

$message = $this->sendMail('Mail Subject', '[email protected]');
$this->sendMail('Mail Subject', '[email protected]');

$view = view('nova-mailman-mails::unique-identifier');
$this->assertInstanceOf(View::class, $view);
Expand All @@ -47,13 +39,13 @@ public function it_stores_email_metadata_in_a_json_file()
return 'unique-identifier';
});

$message = $this->sendMail('Mail Subject', '[email protected]');
$this->sendMail('Mail Subject', '[email protected]');
$file = app(Filesystem::class)->get('mailman/unique-identifier.json');

$this->assertEquals(
[
'id' => 'unique-identifier',
'subject' => $message->getSubject(),
'subject' => 'Mail Subject',
'recipient' => '[email protected]',
'sent_at' => time(),
'link' => route('nova-mailman.show', 'unique-identifier'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace Jstoone\Mailman\Tests\Mailer;
namespace Jstoone\Mailman\Tests;

use Jstoone\Mailman\GenerateMailIdentifier;
use Jstoone\Mailman\Mailer\MailIdentifier;
use Jstoone\Mailman\Tests\TestCase;
use Jstoone\Mailman\MailIdentifier;

class MailmanControllerTest extends TestCase
class MailIdentifierTest extends TestCase
{
/** @test */
public function it_can_generate_a_mail_identifier()
Expand Down
20 changes: 0 additions & 20 deletions tests/Mailer/MailProviderTest.php

This file was deleted.

22 changes: 3 additions & 19 deletions tests/MailmanServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@

namespace Jstoone\Mailman\Tests;

use Jstoone\Mailman\Mailer\MailProvider;
use Illuminate\Mail\Events\MessageSent;

class MailmanServiceProviderTest extends TestCase
{
/** @test */
public function it_registers_the_mailman_mail_service_provider()
public function it_registers_a_mail_sent_listener()
{
$this->assertArrayHasKey(
MailProvider::class,
$this->app->getLoadedProviders()
);
}

/** @test */
public function it_only_registers_mail_provider_when_using_mailman_driver()
{
static::$mailDriver = 'failman';

$app = $this->createApplication();

$this->assertArrayNotHasKey(
MailProvider::class,
$app->getLoadedProviders()
);
$this->assertCount(1, $this->app['events']->getListeners(MessageSent::class));
}
}
38 changes: 12 additions & 26 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,12 @@
namespace Jstoone\Mailman\Tests;

use Illuminate\Contracts\Filesystem\Filesystem;
use Jstoone\Mailman\Mailer\MailmanTransport;
use Illuminate\Support\Facades\Mail;
use Jstoone\Mailman\MailmanServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Swift_Message;

abstract class TestCase extends Orchestra
{
/** @var string */
public static $mailDriver = 'mailman';

/** @var MailmanTransport */
private $transport;

public function setUp()
{
parent::setUp();

$this->transport = $this->app->make(MailmanTransport::class);
}

public function tearDown()
{
app(Filesystem::class)->deleteDirectory('mailman');
Expand All @@ -33,28 +19,28 @@ public function tearDown()
protected function getEnvironmentSetUp($app)
{
$app['router']->middlewareGroup('nova', []);
}

protected function getPackageProviders($app)
{
$app['config']->set('mail.driver', self::$mailDriver);
$app['config']->set('mail.driver', 'array');

$app['config']->set('filesystems.disks.local', [
'driver' => 'local',
'root' => __DIR__ . '/temp',
]);
}

protected function getPackageProviders($app)
{
return [
MailmanServiceProvider::class,
];
}

protected function sendMail(string $subject, string $recipient): Swift_Message
protected function sendMail(string $subject, string $recipient): void
{
$message = new Swift_Message($subject, 'Mail Body');
$message->setTo($recipient);

$this->transport->send($message);

return $message;
Mail::raw('Mail Body', function ($message) use ($subject, $recipient) {
$message->from('[email protected]')
->to($recipient)
->subject($subject);
});
}
}

0 comments on commit f58394b

Please sign in to comment.