Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arduinomaster22 committed Jan 23, 2025
1 parent 8e700a5 commit 8594dbe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ return new class extends Migration
});

Schema::table('mails', function (Blueprint $table): void {
if (!Schema::hasColumn('mails', 'driver')) {
$table->string('driver')
->after('uuid');
}
$table->string('provider')
->after('uuid');

if (!Schema::hasColumn('mails', 'stream_id')) {
$table->string('stream_id')
->nullable()
->after('driver');
}
$table->string('stream_id')
->nullable()
->after('provider');
});
}
};
46 changes: 14 additions & 32 deletions src/Actions/LogMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vormkracht10\Mails\Actions;

use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Collection;
Expand All @@ -14,7 +13,7 @@ class LogMail
{
use AsAction;

public function handle(MessageSending|MessageSent $event, $mailer): mixed
public function handle(MessageSending|MessageSent $event): mixed
{
if (! config('mails.logging.enabled')) {
return null;
Expand All @@ -23,7 +22,7 @@ public function handle(MessageSending|MessageSent $event, $mailer): mixed
$mail = $this->newMailModelInstance();

if ($event instanceof MessageSending) {
$mail->fill($this->getOnlyConfiguredAttributes($event, $mailer));
$mail->fill($this->getOnlyConfiguredAttributes($event));
$mail->save();

$this->collectAttachments($mail, $event->message->getAttachments());
Expand All @@ -32,7 +31,7 @@ public function handle(MessageSending|MessageSent $event, $mailer): mixed
if ($event instanceof MessageSent) {
$mail = $mail->firstWhere('uuid', $this->getCustomUuid($event));

$mail->update($this->getOnlyConfiguredAttributes($event, $mailer));
$mail->update($this->getOnlyConfiguredAttributes($event));
}

return null;
Expand All @@ -48,11 +47,11 @@ public function newMailModelInstance()
return new $model;
}

public function getOnlyConfiguredAttributes(MessageSending|MessageSent $event, Mailer $mailer): array
public function getOnlyConfiguredAttributes(MessageSending|MessageSent $event): array
{
return collect($this->getDefaultLogAttributes($event))
->only($this->getConfiguredAttributes())
->merge($this->getMandatoryAttributes($event, $mailer))
->merge($this->getMandatoryAttributes($event))
->toArray();
}

Expand All @@ -75,44 +74,27 @@ public function getDefaultLogAttributes(MessageSending|MessageSent $event): arra
];
}

protected function getMailerName(Mailer $mailer)
protected function getStreamId(MessageSending|MessageSent $event)
{
$class = $mailer;

$reflection = new \ReflectionClass($class);
$property = $reflection->getProperty('name');
$property->setAccessible(true);

$name = $property->getValue($class);

return $name;
}

protected function getStreamId(MessageSending|MessageSent $event, string $driver)
{
if ($driver !== 'postmark') {
return null;
if ($event->data['mailer'] === 'postmark') {
return config('mail.mailers.postmark.message_stream_id');
}

if (! $event->message->getHeaders()->has('x-pm-metadata-x-mails-uuid')) {
return null;
if (null !== $messageStream = $event->message->getHeaders()->get('x-pm-message-stream')) {
return $messageStream;
}

$headerValue = $event->message->getHeaders()->get('x-pm-metadata-x-mails-uuid');

return $headerValue->getValue();
return null;
}

public function getMandatoryAttributes(MessageSending|MessageSent $event, Mailer $mailer): array
public function getMandatoryAttributes(MessageSending|MessageSent $event): array
{
$driver = $this->getMailerName($mailer);

return [
'uuid' => $this->getCustomUuid($event),
// 'mail_class' => $this->getMailClassHeaderValue($event),
'sent_at' => $event instanceof MessageSent ? now() : null,
'driver' => $driver,
'stream_id' => $this->getStreamId($event, $this->getMailerName($mailer)),
'mailer' => $event->data['mailer'],
'stream_id' => $this->getStreamId($event),
];
}

Expand Down
10 changes: 1 addition & 9 deletions src/Listeners/LogSendingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

namespace Vormkracht10\Mails\Listeners;

use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Events\MessageSending;
use Vormkracht10\Mails\Actions\LogMail;

class LogSendingMail
{
protected Mailer $mailer;

public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}

public function handle(MessageSending $event): void
{
(new LogMail)($event, $this->mailer);
(new LogMail)($event);
}
}
10 changes: 1 addition & 9 deletions src/Listeners/LogSentMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

namespace Vormkracht10\Mails\Listeners;

use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Events\MessageSent;
use Vormkracht10\Mails\Actions\LogMail;

class LogSentMail
{
protected Mailer $mailer;

public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}

public function handle(MessageSent $event): void
{
(new LogMail)($event, $this->mailer);
(new LogMail)($event);
}
}
2 changes: 1 addition & 1 deletion src/Models/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Mail extends Model

protected $fillable = [
'uuid',
'driver',
'provider',
'stream_id',
'mail_class',
'subject',
Expand Down

0 comments on commit 8594dbe

Please sign in to comment.