Skip to content

Commit

Permalink
Merge pull request #17 from dcblogdev/add-laravel-pint
Browse files Browse the repository at this point in the history
Add laravel pint
  • Loading branch information
dcblogdev authored May 6, 2024
2 parents 197133a + 349a188 commit 02bb1e2
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 49 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion config/sentemails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
3 changes: 3 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "laravel"
}
12 changes: 6 additions & 6 deletions src/Controllers/SentEmailsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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%");
}
}
Expand Down Expand Up @@ -63,4 +63,4 @@ public function downloadAttachment(string $id): BinaryFileResponse

return response()->download(storage_path('app/'.$attachment->path), $attachment->filename);
}
}
}
24 changes: 12 additions & 12 deletions src/Listeners/EmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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([
Expand All @@ -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.'>';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Models/SentEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SentEmail extends Model
'cc',
'bcc',
'subject',
'body'
'body',
];

protected static function newFactory(): SentEmailFactory
Expand Down
2 changes: 1 addition & 1 deletion src/Models/SentEmailAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SentEmailAttachment extends Model
protected $fillable = [
'sent_email_id',
'filename',
'path'
'path',
];

protected static function newFactory(): SentEmailAttachmentFactory
Expand Down
13 changes: 6 additions & 7 deletions src/SentEmailsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()) {

Expand All @@ -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([
Expand All @@ -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');
}
}
5 changes: 2 additions & 3 deletions src/database/factories/SentEmailAttachmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
}

15 changes: 7 additions & 8 deletions src/database/factories/SentEmailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

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

return new class extends Migration
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use Dcblogdev\LaravelSentEmails\Models\SentEmail;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
Expand Down
2 changes: 1 addition & 1 deletion src/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
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');
Route::get(config('sentemails.routepath').'/attachment-{id}', [SentEmailsController::class, 'downloadAttachment'])->name('sentemails.downloadAttachment');
});
});
2 changes: 1 addition & 1 deletion tests/SentEmailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
$this->get(route('sentemails.downloadAttachment', $attachment->id))->assertDownload($filename);

unlink($filename);
});
});
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ 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' => '',
]);
$app['config']->set('sentemails.middleware', [
'web'
'web',
]);
}
}

0 comments on commit 02bb1e2

Please sign in to comment.