Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Sep 8, 2024
1 parent 5a401f6 commit 5adecbe
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/Actions/RegisterWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace Vormkracht10\Mails\Actions;

use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\View\Components\Factory;
use Vormkracht10\Mails\Shared\AsAction;
use Vormkracht10\Mails\Facades\MailProvider;
use Illuminate\Console\View\Components\Factory;
use Illuminate\Console\Concerns\InteractsWithIO;

class RegisterWebhooks
{
use AsAction, InteractsWithIO;

public function handle(Factory $components)
{
MailProvider::with('postmark')->registerWebhooks();
MailProvider::with('postmark')->registerWebhooks(
components: $components
);
}
}
}
9 changes: 6 additions & 3 deletions src/Actions/SendHighBounceRateNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ class SendHighBounceRateNotifications
/**
* @param float|int $rate
* @param float|int $threshold
* @return bool
*/
public function handle($rate, $threshold): void
public function handle($rate, $threshold): bool
{
if (! $channels = config('mails.events.bouncerate.notify')) {
return;
return false;
}

$notification = new HighBounceRateNotification($rate, $threshold);

$this->send($notification, $channels);

return true;
}
}
}
16 changes: 12 additions & 4 deletions src/Drivers/MailDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Str;
use Vormkracht10\Mails\Models\Mail;

class MailDriver
abstract class MailDriver
{
protected string $mailModel;

Expand All @@ -21,6 +21,14 @@ public function __construct()
$this->uuidHeaderName = config('mails.headers.uuid');
}

abstract protected function getUuidFromPayload(array $payload): ?string;

abstract protected function dataMapping(): array;

abstract protected function getTimestampFromPayload(array $payload): string;

abstract protected function eventMapping(): array;

public function getMailFromPayload(array $payload): ?Mail
{
return $this->mailModel::query()
Expand All @@ -30,7 +38,7 @@ public function getMailFromPayload(array $payload): ?Mail
public function getDataFromPayload(array $payload): array
{
return collect($this->dataMapping())
->mapWithKeys(fn ($value, $key) => [$key => data_get($payload, $value)])
->mapWithKeys(fn($value, $key) => [$key => data_get($payload, $value)])
->filter()
->merge([
'type' => $this->getEventFromPayload($payload),
Expand All @@ -42,7 +50,7 @@ public function getDataFromPayload(array $payload): array
public function getEventFromPayload(array $payload): string
{
foreach ($this->eventMapping() as $event => $mapping) {
if (collect($mapping)->every(fn ($value, $key) => data_get($payload, $key) === $value)) {
if (collect($mapping)->every(fn($value, $key) => data_get($payload, $key) === $value)) {
return $event;
}
}
Expand Down Expand Up @@ -129,4 +137,4 @@ public function unsubscribed(Mail $mail, string $timestamp): void
'opens' => $mail->opens + 1,
]);
}
}
}
2 changes: 1 addition & 1 deletion src/Drivers/MailgunDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getUuidFromPayload(array $payload): ?string
null;
}

protected function getTimestampFromPayload(array $payload)
protected function getTimestampFromPayload(array $payload): string
{
return $payload['event-data']['timestamp'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/PostmarkDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getUuidFromPayload(array $payload): ?string
null;
}

protected function getTimestampFromPayload(array $payload)
protected function getTimestampFromPayload(array $payload): string
{
return $payload['DeliveredAt'] ?? $payload['BouncedAt'] ?? $payload['ReceivedAt'] ?? now();
}
Expand Down
11 changes: 7 additions & 4 deletions src/Jobs/ResendMailJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
private array $to,
private array $cc = [],
private array $bcc = [],
private array $replyTo = [],
private array $replyTo = []
) {
//
}
Expand All @@ -43,7 +43,11 @@ private function setMessageContent(Message $message): self
->text($this->mail->text ?? '');

foreach ($this->mail->attachments as $attachment) {
$message->attachData($attachment->fileData, $attachment->filename, ['mime' => $attachment->mime]);
$message->attachData(
$attachment->file_data ?? $attachment->fileData ?? '',
$attachment->file_name ?? $attachment->filename ?? '',
['mime' => $attachment->mime_type ?? $attachment->mime ?? '']
);
}

return $this;
Expand Down Expand Up @@ -73,7 +77,6 @@ private function setMessageRecipients(Message $message): self
private function getFirstAddress(string $jsonAddresses): string
{
$addresses = json_decode($jsonAddresses, true);

return array_key_first($addresses);
}
}
}
4 changes: 2 additions & 2 deletions src/Listeners/LogMailEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private function record($provider, $payload): void
->logMailEvent($payload);
}

private function dispatch($payload): void
private function dispatch($provider, $payload): void
{
dispatch(fn () => $this->record($payload));
dispatch(fn () => $this->record($provider, $payload));
}
}
25 changes: 20 additions & 5 deletions src/Traits/SendsNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Notification as Notifications;
use Vormkracht10\Mails\Notifications\Concerns\HasDynamicDrivers;

trait SendsNotifications
{
/**
* @param HasDynamicDrivers & Notification $notification
* @param Notification $notification
*/
public function send(Notification $notification, array $channels): void
{
Expand All @@ -21,14 +20,30 @@ public function send(Notification $notification, array $channels): void
);

if (empty($accounts)) {
return;
continue;
}

foreach ($accounts as $route) {
Notifications::route($channel, $route)->notify(
$notification->on($channel),
$this->prepareNotification($notification, $channel)
);
}
}
}
}

/**
* Prepare the notification for sending.
*
* @param Notification $notification
* @param string $channel
* @return Notification
*/
protected function prepareNotification(Notification $notification, string $channel): Notification
{
if (method_exists($notification, 'on')) {
return $notification->on($channel);
}

return $notification;
}
}

0 comments on commit 5adecbe

Please sign in to comment.