From fe594e823acbc10a9377103c6ba3796c99424dc2 Mon Sep 17 00:00:00 2001 From: David Carr Date: Mon, 6 May 2024 12:27:56 +0100 Subject: [PATCH 1/3] Add .phpunit.cache to .gitignore file A new type of cache file, .phpunit.cache, has been introduced into the project which was not previously ignored by Git. To prevent it from being tracked, it has been added to the .gitignore file. --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c1285db..8b4a382 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ composer.lock vendor .idea -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.phpunit.cache \ No newline at end of file From 7da18251d355ddd659888502fa1175f0338761c1 Mon Sep 17 00:00:00 2001 From: David Carr Date: Mon, 6 May 2024 12:28:09 +0100 Subject: [PATCH 2/3] added pint --- composer.json | 3 ++- config/sentemails.php | 2 +- pint.json | 3 +++ src/Controllers/SentEmailsController.php | 12 +++++----- src/Listeners/EmailLogger.php | 22 +++++++++---------- src/Models/SentEmail.php | 2 +- src/SentEmailsServiceProvider.php | 9 ++++---- .../migrations/create_sent_emails_table.php | 4 ++-- src/routes/web.php | 2 +- tests/SentEmailsTest.php | 4 +--- tests/TestCase.php | 4 ++-- 11 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 pint.json diff --git a/composer.json b/composer.json index bd49b64..cf2ff87 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ }, "require-dev": { "pestphp/pest": "^2.0", - "orchestra/testbench": "^8.22" + "orchestra/testbench": "^8.22", + "laravel/pint": "^1.15" }, "autoload": { "psr-4": { diff --git a/config/sentemails.php b/config/sentemails.php index 8085be8..82cf0f3 100644 --- a/config/sentemails.php +++ b/config/sentemails.php @@ -5,7 +5,7 @@ */ return [ //set the route path to load the sent emails ui defaults to /sentemails - 'routepath' => 'admin/sentemails', + 'routepath' => 'admin/sentemails', // set the route middlewares to apply on the sent emails ui 'middleware' => ['web', 'auth'], diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..661e522 --- /dev/null +++ b/pint.json @@ -0,0 +1,3 @@ +{ + "preset": "laravel" +} \ No newline at end of file diff --git a/src/Controllers/SentEmailsController.php b/src/Controllers/SentEmailsController.php index 9e77cee..de141d5 100644 --- a/src/Controllers/SentEmailsController.php +++ b/src/Controllers/SentEmailsController.php @@ -3,8 +3,8 @@ namespace Dcblogdev\LaravelSentEmails\Controllers; use Citco\Carbon; -use Illuminate\Contracts\View\View; use Dcblogdev\LaravelSentEmails\Models\SentEmail; +use Illuminate\Contracts\View\View; class SentEmailsController { @@ -18,20 +18,20 @@ public function index(): View $to = request('to'); $subject = request('subject'); - if ($date !='') { + if ($date != '') { $date = Carbon::parse($date)->format('Y-m-d'); $emails->where('date', '=', $date); } - if ($from !='') { + if ($from != '') { $emails->where('from', 'like', "%$from%"); } - if ($to !='') { + if ($to != '') { $emails->where('to', 'like', "%$to%"); } - if ($subject !='') { + if ($subject != '') { $emails->where('subject', 'like', "%$subject%"); } } @@ -54,4 +54,4 @@ public function body(string $id): string return $email->body; } -} \ No newline at end of file +} diff --git a/src/Listeners/EmailLogger.php b/src/Listeners/EmailLogger.php index fc31f28..52fdac6 100644 --- a/src/Listeners/EmailLogger.php +++ b/src/Listeners/EmailLogger.php @@ -2,8 +2,8 @@ namespace Dcblogdev\LaravelSentEmails\Listeners; -use Illuminate\Mail\Events\MessageSending; use Dcblogdev\LaravelSentEmails\Models\SentEmail; +use Illuminate\Mail\Events\MessageSending; class EmailLogger { @@ -12,25 +12,25 @@ public function handle(MessageSending $event): void $message = $event->message; SentEmail::create([ - 'date' => date('Y-m-d H:i:s'), - 'from' => $this->formatAddressField($message->getFrom()), - 'to' => $this->formatAddressField($message->getTo()), - 'cc' => $this->formatAddressField($message->getCc()), - 'bcc' => $this->formatAddressField($message->getBcc()), - 'subject' => $message->getSubject(), - 'body' => $message->getHtmlBody() + 'date' => date('Y-m-d H:i:s'), + 'from' => $this->formatAddressField($message->getFrom()), + 'to' => $this->formatAddressField($message->getTo()), + 'cc' => $this->formatAddressField($message->getCc()), + 'bcc' => $this->formatAddressField($message->getBcc()), + 'subject' => $message->getSubject(), + 'body' => $message->getHtmlBody(), ]); } - function formatAddressField(array $field): ?string + public function formatAddressField(array $field): ?string { $strings = []; - foreach($field as $row) { + foreach ($field as $row) { $email = $row->getAddress(); $name = $row->getName(); - if ($name !='') { + if ($name != '') { $email = $name.' <'.$email.'>'; } diff --git a/src/Models/SentEmail.php b/src/Models/SentEmail.php index 45d410a..49eca7f 100644 --- a/src/Models/SentEmail.php +++ b/src/Models/SentEmail.php @@ -13,7 +13,7 @@ class SentEmail extends Model 'cc', 'bcc', 'subject', - 'body' + 'body', ]; public function getBodyAttribute($compressed): string diff --git a/src/SentEmailsServiceProvider.php b/src/SentEmailsServiceProvider.php index 00d5d05..0d7eeb2 100644 --- a/src/SentEmailsServiceProvider.php +++ b/src/SentEmailsServiceProvider.php @@ -2,11 +2,10 @@ namespace Dcblogdev\LaravelSentEmails; +use Dcblogdev\LaravelSentEmails\Listeners\EmailLogger; use Illuminate\Mail\Events\MessageSending; use Illuminate\Support\Facades\Event; -use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; -use Dcblogdev\LaravelSentEmails\Listeners\EmailLogger; class SentEmailsServiceProvider extends ServiceProvider { @@ -18,7 +17,7 @@ public function boot(): void ); $this->loadViewsFrom(__DIR__.'/resources/views', 'sentemails'); - $this->loadRoutesFrom(__DIR__ . '/routes/web.php'); + $this->loadRoutesFrom(__DIR__.'/routes/web.php'); if ($this->app->runningInConsole()) { @@ -28,7 +27,7 @@ public function boot(): void $timestamp = date('Y_m_d_His', time()); $this->publishes([ - __DIR__.'/database/migrations/create_sent_emails_table.php' => $this->app->databasePath() . "/migrations/{$timestamp}_create_sent_emails_table.php", + __DIR__.'/database/migrations/create_sent_emails_table.php' => $this->app->databasePath()."/migrations/{$timestamp}_create_sent_emails_table.php", ], 'migrations'); // Publishing the views. @@ -41,6 +40,6 @@ public function boot(): void public function register(): void { - $this->mergeConfigFrom(__DIR__. '/../config/sentemails.php', 'sentemails'); + $this->mergeConfigFrom(__DIR__.'/../config/sentemails.php', 'sentemails'); } } diff --git a/src/database/migrations/create_sent_emails_table.php b/src/database/migrations/create_sent_emails_table.php index 9241c25..58da2ad 100644 --- a/src/database/migrations/create_sent_emails_table.php +++ b/src/database/migrations/create_sent_emails_table.php @@ -1,8 +1,8 @@ name('sentemails.index'); Route::get(config('sentemails.routepath').'/email/{id}', [SentEmailsController::class, 'email'])->name('sentemails.email'); Route::get(config('sentemails.routepath').'/body/{id}', [SentEmailsController::class, 'body'])->name('sentemails.body'); -}); \ No newline at end of file +}); diff --git a/tests/SentEmailsTest.php b/tests/SentEmailsTest.php index 15e2691..56843c9 100644 --- a/tests/SentEmailsTest.php +++ b/tests/SentEmailsTest.php @@ -1,8 +1,6 @@ get(route('sentemails.index'))->assertStatus(500); -}); \ No newline at end of file +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 7fd7dd1..eb95c76 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,9 +22,9 @@ protected function defineEnvironment($app) // Setup default database to use sqlite :memory: $app['config']->set('database.default', 'mysql'); $app['config']->set('database.connections.mysql', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); } } From 349a18858122a0050e14075503a5d4e354822bf4 Mon Sep 17 00:00:00 2001 From: David Carr Date: Mon, 6 May 2024 12:29:46 +0100 Subject: [PATCH 3/3] added pint --- config/sentemails.php | 2 +- src/Controllers/SentEmailsController.php | 12 +++++----- src/Listeners/EmailLogger.php | 24 +++++++++---------- src/Models/SentEmailAttachment.php | 2 +- src/SentEmailsServiceProvider.php | 13 +++++----- .../factories/SentEmailAttachmentFactory.php | 5 ++-- src/database/factories/SentEmailFactory.php | 15 ++++++------ ...7_create_sent_emails_attachments_table.php | 4 ++-- src/routes/web.php | 2 +- tests/SentEmailsTest.php | 2 +- tests/TestCase.php | 2 +- 11 files changed, 40 insertions(+), 43 deletions(-) diff --git a/config/sentemails.php b/config/sentemails.php index ae63199..36eae9c 100644 --- a/config/sentemails.php +++ b/config/sentemails.php @@ -5,7 +5,7 @@ */ return [ //set the route path to load the sent emails ui defaults to /sentemails - 'routepath' => 'sentemails', + 'routepath' => 'sentemails', // set the route middlewares to apply on the sent emails ui 'middleware' => ['web', 'auth'], diff --git a/src/Controllers/SentEmailsController.php b/src/Controllers/SentEmailsController.php index 321dca1..587ae6f 100644 --- a/src/Controllers/SentEmailsController.php +++ b/src/Controllers/SentEmailsController.php @@ -3,9 +3,9 @@ namespace Dcblogdev\LaravelSentEmails\Controllers; use Citco\Carbon; +use Dcblogdev\LaravelSentEmails\Models\SentEmail; use Dcblogdev\LaravelSentEmails\Models\SentEmailAttachment; use Illuminate\Contracts\View\View; -use Dcblogdev\LaravelSentEmails\Models\SentEmail; use Symfony\Component\HttpFoundation\BinaryFileResponse; class SentEmailsController @@ -20,20 +20,20 @@ public function index(): View $to = request('to'); $subject = request('subject'); - if ($date !='') { + if ($date != '') { $date = Carbon::parse($date)->format('Y-m-d'); $emails->where('date', '=', $date); } - if ($from !='') { + if ($from != '') { $emails->where('from', 'like', "%$from%"); } - if ($to !='') { + if ($to != '') { $emails->where('to', 'like', "%$to%"); } - if ($subject !='') { + if ($subject != '') { $emails->where('subject', 'like', "%$subject%"); } } @@ -63,4 +63,4 @@ public function downloadAttachment(string $id): BinaryFileResponse return response()->download(storage_path('app/'.$attachment->path), $attachment->filename); } -} \ No newline at end of file +} diff --git a/src/Listeners/EmailLogger.php b/src/Listeners/EmailLogger.php index 62569d5..9f8fd82 100644 --- a/src/Listeners/EmailLogger.php +++ b/src/Listeners/EmailLogger.php @@ -2,9 +2,9 @@ namespace Dcblogdev\LaravelSentEmails\Listeners; +use Dcblogdev\LaravelSentEmails\Models\SentEmail; use Dcblogdev\LaravelSentEmails\Models\SentEmailAttachment; use Illuminate\Mail\Events\MessageSending; -use Dcblogdev\LaravelSentEmails\Models\SentEmail; use Illuminate\Support\Facades\Storage; class EmailLogger @@ -14,19 +14,19 @@ public function handle(MessageSending $event): void $message = $event->message; $email = SentEmail::create([ - 'date' => date('Y-m-d H:i:s'), - 'from' => $this->formatAddressField($message->getFrom()), - 'to' => $this->formatAddressField($message->getTo()), - 'cc' => $this->formatAddressField($message->getCc()), - 'bcc' => $this->formatAddressField($message->getBcc()), - 'subject' => $message->getSubject(), - 'body' => $message->getHtmlBody() + 'date' => date('Y-m-d H:i:s'), + 'from' => $this->formatAddressField($message->getFrom()), + 'to' => $this->formatAddressField($message->getTo()), + 'cc' => $this->formatAddressField($message->getCc()), + 'bcc' => $this->formatAddressField($message->getBcc()), + 'subject' => $message->getSubject(), + 'body' => $message->getHtmlBody(), ]); if (config('sentemails.storeAttachments')) { foreach ($message->getAttachments() as $attachment) { - $path = 'sent-emails/' . now() . '-' . $attachment->getFilename(); + $path = 'sent-emails/'.now().'-'.$attachment->getFilename(); Storage::disk('local')->put($path, $attachment->getBody()); SentEmailAttachment::create([ @@ -38,15 +38,15 @@ public function handle(MessageSending $event): void } } - function formatAddressField(array $field): ?string + public function formatAddressField(array $field): ?string { $strings = []; - foreach($field as $row) { + foreach ($field as $row) { $email = $row->getAddress(); $name = $row->getName(); - if ($name !='') { + if ($name != '') { $email = $name.' <'.$email.'>'; } diff --git a/src/Models/SentEmailAttachment.php b/src/Models/SentEmailAttachment.php index fba3999..9fbeda8 100644 --- a/src/Models/SentEmailAttachment.php +++ b/src/Models/SentEmailAttachment.php @@ -15,7 +15,7 @@ class SentEmailAttachment extends Model protected $fillable = [ 'sent_email_id', 'filename', - 'path' + 'path', ]; protected static function newFactory(): SentEmailAttachmentFactory diff --git a/src/SentEmailsServiceProvider.php b/src/SentEmailsServiceProvider.php index 8b08314..3ba49be 100644 --- a/src/SentEmailsServiceProvider.php +++ b/src/SentEmailsServiceProvider.php @@ -2,11 +2,10 @@ namespace Dcblogdev\LaravelSentEmails; +use Dcblogdev\LaravelSentEmails\Listeners\EmailLogger; use Illuminate\Mail\Events\MessageSending; use Illuminate\Support\Facades\Event; -use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; -use Dcblogdev\LaravelSentEmails\Listeners\EmailLogger; class SentEmailsServiceProvider extends ServiceProvider { @@ -18,8 +17,8 @@ public function boot(): void ); $this->loadViewsFrom(__DIR__.'/resources/views', 'sentemails'); - $this->loadRoutesFrom(__DIR__ . '/routes/web.php'); - $this->loadMigrationsFrom(__DIR__ . '/database/migrations'); + $this->loadRoutesFrom(__DIR__.'/routes/web.php'); + $this->loadMigrationsFrom(__DIR__.'/database/migrations'); if ($this->app->runningInConsole()) { @@ -28,8 +27,8 @@ public function boot(): void ], 'config'); $this->publishes([ - __DIR__.'/database/migrations/2020_07_16_222057_create_sent_emails_table.php' => $this->app->databasePath() . "/migrations/2020_07_16_222057_create_sent_emails_table.php", - __DIR__.'/database/migrations/2024_05_07_222057_create_sent_emails_attachments_table.php' => $this->app->databasePath() . "/migrations/{2024_05_07_222057_create_sent_emails_attachments_table.php", + __DIR__.'/database/migrations/2020_07_16_222057_create_sent_emails_table.php' => $this->app->databasePath().'/migrations/2020_07_16_222057_create_sent_emails_table.php', + __DIR__.'/database/migrations/2024_05_07_222057_create_sent_emails_attachments_table.php' => $this->app->databasePath().'/migrations/{2024_05_07_222057_create_sent_emails_attachments_table.php', ], 'migrations'); $this->publishes([ @@ -40,6 +39,6 @@ public function boot(): void public function register(): void { - $this->mergeConfigFrom(__DIR__. '/../config/sentemails.php', 'sentemails'); + $this->mergeConfigFrom(__DIR__.'/../config/sentemails.php', 'sentemails'); } } diff --git a/src/database/factories/SentEmailAttachmentFactory.php b/src/database/factories/SentEmailAttachmentFactory.php index 32d6d40..b8ba466 100644 --- a/src/database/factories/SentEmailAttachmentFactory.php +++ b/src/database/factories/SentEmailAttachmentFactory.php @@ -15,9 +15,8 @@ public function definition(): array { return [ 'sent_email_id' => SentEmail::factory(), - 'filename' => Str::random(10) . '.txt', - 'path' => 'sent-emails/' . Str::random(10) . '.txt' + 'filename' => Str::random(10).'.txt', + 'path' => 'sent-emails/'.Str::random(10).'.txt', ]; } } - diff --git a/src/database/factories/SentEmailFactory.php b/src/database/factories/SentEmailFactory.php index eb720bf..2fd77f3 100644 --- a/src/database/factories/SentEmailFactory.php +++ b/src/database/factories/SentEmailFactory.php @@ -12,14 +12,13 @@ class SentEmailFactory extends Factory public function definition(): array { return [ - 'date' => date('Y-m-d H:i:s'), - 'from' => fake()->email, - 'to' => fake()->email, - 'cc' => fake()->email, - 'bcc' => fake()->email, - 'subject' => fake()->sentence, - 'body' => fake()->realText + 'date' => date('Y-m-d H:i:s'), + 'from' => fake()->email, + 'to' => fake()->email, + 'cc' => fake()->email, + 'bcc' => fake()->email, + 'subject' => fake()->sentence, + 'body' => fake()->realText, ]; } } - diff --git a/src/database/migrations/2024_05_07_222057_create_sent_emails_attachments_table.php b/src/database/migrations/2024_05_07_222057_create_sent_emails_attachments_table.php index 309665e..a5b359d 100644 --- a/src/database/migrations/2024_05_07_222057_create_sent_emails_attachments_table.php +++ b/src/database/migrations/2024_05_07_222057_create_sent_emails_attachments_table.php @@ -1,9 +1,9 @@ name('sentemails.email'); Route::get(config('sentemails.routepath').'/body/{id}', [SentEmailsController::class, 'body'])->name('sentemails.body'); Route::get(config('sentemails.routepath').'/attachment-{id}', [SentEmailsController::class, 'downloadAttachment'])->name('sentemails.downloadAttachment'); -}); \ No newline at end of file +}); diff --git a/tests/SentEmailsTest.php b/tests/SentEmailsTest.php index 004f1f8..913c3ee 100644 --- a/tests/SentEmailsTest.php +++ b/tests/SentEmailsTest.php @@ -34,4 +34,4 @@ $this->get(route('sentemails.downloadAttachment', $attachment->id))->assertDownload($filename); unlink($filename); -}); \ No newline at end of file +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 3a7085c..0dd5a98 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -30,7 +30,7 @@ protected function defineEnvironment($app) 'prefix' => '', ]); $app['config']->set('sentemails.middleware', [ - 'web' + 'web', ]); } }