Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremykenedy committed Feb 27, 2023
1 parent dfc8617 commit 8ab9792
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 71 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"require-dev": {
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^10.0"
"phpunit/phpunit": "^10.0",
"suin/phpcs-psr4-sniff": "^3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -44,7 +45,10 @@
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"laravel": {
Expand Down
32 changes: 15 additions & 17 deletions src/database/migrations/2015_07_31_1_email_log.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class EmailLog extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('email_log', function (Blueprint $table) {
$table->increments('id');
$table->dateTime('date');
$table->string('from')->nullable();
$table->string('to')->nullable();
$table->string('cc')->nullable();
$table->string('bcc')->nullable();
$table->string('subject');
$table->text('body');
$table->text('headers')->nullable();
$table->text('attachments')->nullable();
});
Schema::create(
'email_log', function (Blueprint $table) {
$table->increments('id');
$table->dateTime('date');
$table->string('from')->nullable();
$table->string('to')->nullable();
$table->string('cc')->nullable();
$table->string('bcc')->nullable();
$table->string('subject');
$table->text('body');
$table->text('headers')->nullable();
$table->text('attachments')->nullable();
}
);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddBccColumnEmailLog extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('email_log', function ($table) {
if (! Schema::hasColumn('email_log', 'bcc')) {
$table->string('to')->nullable()->change();
$table->string('bcc')->after('to')->nullable();
Schema::table(
'email_log', function ($table) {
if (! Schema::hasColumn('email_log', 'bcc')) {
$table->string('to')->nullable()->change();
$table->string('bcc')->after('to')->nullable();
}
}
});
);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('email_log', function ($table) {
$table->string('to')->change();
$table->dropColumn('bcc');
});
Schema::table(
'email_log', function ($table) {
$table->string('to')->change();
$table->dropColumn('bcc');
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddMoreMailColumnsEmailLog extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('email_log', function ($table) {
if (! Schema::hasColumn('email_log', 'id')) {
$table->increments('id')->first();
$table->string('from')->after('date')->nullable();
$table->string('cc')->after('to')->nullable();
$table->text('headers')->after('body')->nullable();
$table->text('attachments')->after('headers')->nullable();
Schema::table(
'email_log', function ($table) {
if (! Schema::hasColumn('email_log', 'id')) {
$table->increments('id')->first();
$table->string('from')->after('date')->nullable();
$table->string('cc')->after('to')->nullable();
$table->text('headers')->after('body')->nullable();
$table->text('attachments')->after('headers')->nullable();
}
}
});
);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('email_log', function ($table) {
$table->dropColumn(['id', 'from', 'cc', 'headers', 'attachments']);
});
Schema::table(
'email_log', function ($table) {
$table->dropColumn(['id', 'from', 'cc', 'headers', 'attachments']);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UseLongtextForAttachments extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('email_log', function (Blueprint $table) {
$table->longText('attachments')->nullable()->change();
});
Schema::table(
'email_log', function (Blueprint $table) {
$table->longText('attachments')->nullable()->change();
}
);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('email_log', function (Blueprint $table) {
$table->text('attachments')->nullable()->change();
});
Schema::table(
'email_log', function (Blueprint $table) {
$table->text('attachments')->nullable()->change();
}
);
}
}
12 changes: 7 additions & 5 deletions src/jeremykenedy/LaravelEmailDatabaseLog/EmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class EmailLogger
/**
* Handle the actual logging.
*
* @param MessageSending $event
* @param MessageSending $event
* @return void
*/
public function handle(MessageSending $event): void
{
$message = $event->message;

DB::table('email_log')->insert([
DB::table('email_log')->insert(
[
'date' => Carbon::now()->format('Y-m-d H:i:s'),
'from' => $this->formatAddressField($message, 'From'),
'to' => $this->formatAddressField($message, 'To'),
Expand All @@ -30,14 +31,15 @@ public function handle(MessageSending $event): void
'body' => $message->getBody()->bodyToString(),
'headers' => $message->getHeaders()->toString(),
'attachments' => $this->saveAttachments($message),
]);
]
);
}

/**
* Format address strings for sender, to, cc, bcc.
*
* @param Email $message
* @param string $field
* @param string $field
* @return null|string
*/
public function formatAddressField(Email $message, string $field): ?string
Expand All @@ -50,7 +52,7 @@ public function formatAddressField(Email $message, string $field): ?string
/**
* Collect all attachments and format them as strings.
*
* @param Email $message
* @param Email $message
* @return string|null
*/
protected function saveAttachments(Email $message): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class LaravelEmailDatabaseLogServiceProvider extends ServiceProvider
{
/**
* Register any other events for your application.
*
* @return void
*/
public function boot(): void
{
Expand All @@ -18,17 +16,17 @@ public function boot(): void

/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
$this->app->register(LaravelEmailDatabaseLogEventServiceProvider::class);

if ($this->app->runningInConsole()) {
$this->publishes([
$this->publishes(
[
__DIR__.'/../../database/migrations' => database_path('migrations'),
], 'laravel-email-database-log-migration');
], 'laravel-email-database-log-migration'
);
}
}
}

0 comments on commit 8ab9792

Please sign in to comment.