Skip to content

Commit

Permalink
Merge pull request #9 from laravelrotebal/8-save-recipient-entity
Browse files Browse the repository at this point in the history
#8 save recipient entity
  • Loading branch information
gabrieliuga committed Mar 27, 2020
2 parents 2b69526 + 891003e commit 5a589dc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterMailLogTableAddRecipientColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mail_log', function (Blueprint $table) {
$table->nullableMorphs('recipient');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mail_log', function (Blueprint $table) {
$table->dropMorphs('recipient');
});
}
}
7 changes: 7 additions & 0 deletions src/Listeners/MailSentListener.php
Expand Up @@ -4,6 +4,7 @@

use Giuga\LaravelMailLog\Models\MailLog;
use Giuga\LaravelMailLog\Traits\Occurrable;
use Giuga\LaravelMailLog\Traits\Recipientable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -64,6 +65,12 @@ public function handle(MessageSent $event)
if ($occuredProcess && $occuredProcess instanceof Model) {
$log->occurredProcess()->associate($occuredProcess)->save();
}

$recipient = $event->data[Recipientable::getRecipientKey()] ?? null;

if ($recipient && $recipient instanceof Model) {
$log->recipient()->associate($recipient)->save();
}
} catch (\Throwable $e) {
Log::debug('Failed to save mail log ['.$e->getMessage().']');
}
Expand Down
5 changes: 5 additions & 0 deletions src/Models/MailLog.php
Expand Up @@ -21,4 +21,9 @@ public function occurredEntity()
{
return $this->morphTo();
}

public function recipient()
{
return $this->morphTo();
}
}
20 changes: 20 additions & 0 deletions src/Traits/Recipientable.php
@@ -0,0 +1,20 @@
<?php

namespace Giuga\LaravelMailLog\Traits;

use Illuminate\Database\Eloquent\Model;

trait Recipientable
{
public static function getRecipientKey()
{
return 'event.recipient';
}

public function recipient(Model $recipient = null)
{
$this->with(static::getRecipientKey(), $recipient);

return $this;
}
}

0 comments on commit 5a589dc

Please sign in to comment.