From f4f76bb51967d167a5277939ca7bfce224ceffca Mon Sep 17 00:00:00 2001 From: Baspa Date: Sun, 8 Sep 2024 15:42:44 +0200 Subject: [PATCH 1/5] PHPStan fixes --- src/Actions/RegisterWebhooks.php | 11 +++++++---- .../SendHighBounceRateNotifications.php | 9 ++++++--- src/Controllers/WebhookController.php | 7 ++++--- src/Drivers/MailDriver.php | 16 ++++++++++++---- src/Drivers/MailgunDriver.php | 6 +++--- src/Drivers/PostmarkDriver.php | 4 ++-- src/Jobs/ResendMailJob.php | 19 ++++++++++++------- src/Listeners/LogMailEvent.php | 6 +++--- 8 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/Actions/RegisterWebhooks.php b/src/Actions/RegisterWebhooks.php index a1079e4..711de19 100644 --- a/src/Actions/RegisterWebhooks.php +++ b/src/Actions/RegisterWebhooks.php @@ -2,9 +2,10 @@ 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 { @@ -12,6 +13,8 @@ class RegisterWebhooks public function handle(Factory $components) { - MailProvider::with('postmark')->registerWebhooks(); + MailProvider::with('postmark')->registerWebhooks( + components: $components + ); } -} +} \ No newline at end of file diff --git a/src/Actions/SendHighBounceRateNotifications.php b/src/Actions/SendHighBounceRateNotifications.php index 8534a4c..35d7c8a 100644 --- a/src/Actions/SendHighBounceRateNotifications.php +++ b/src/Actions/SendHighBounceRateNotifications.php @@ -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; } -} +} \ No newline at end of file diff --git a/src/Controllers/WebhookController.php b/src/Controllers/WebhookController.php index b74373f..06d78ab 100644 --- a/src/Controllers/WebhookController.php +++ b/src/Controllers/WebhookController.php @@ -12,7 +12,7 @@ class WebhookController { public function __invoke(Request $request, string $driver): Response { - if (array_key_exists($driver, array_column(Provider::cases(), 'value'))) { + if (!in_array($driver, array_column(Provider::cases(), 'value'))) { return response('Unknown provider.', status: 400); } @@ -21,9 +21,10 @@ public function __invoke(Request $request, string $driver): Response } MailEvent::dispatch( - $driver, $request->except('signature') + $driver, + $request->except('signature') ); return response('Event processed.', status: 202); } -} +} \ No newline at end of file diff --git a/src/Drivers/MailDriver.php b/src/Drivers/MailDriver.php index 39c64b6..1592444 100644 --- a/src/Drivers/MailDriver.php +++ b/src/Drivers/MailDriver.php @@ -6,7 +6,7 @@ use Illuminate\Support\Str; use Vormkracht10\Mails\Models\Mail; -class MailDriver +abstract class MailDriver { protected string $mailModel; @@ -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() @@ -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), @@ -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; } } @@ -129,4 +137,4 @@ public function unsubscribed(Mail $mail, string $timestamp): void 'opens' => $mail->opens + 1, ]); } -} +} \ No newline at end of file diff --git a/src/Drivers/MailgunDriver.php b/src/Drivers/MailgunDriver.php index d637216..fa9290e 100644 --- a/src/Drivers/MailgunDriver.php +++ b/src/Drivers/MailgunDriver.php @@ -19,7 +19,7 @@ public function verifyWebhookSignature(array $payload): bool return false; } - $hmac = hash_hmac('sha256', $payload['signature']['timestamp'].$payload['signature']['token'], config('services.mailgun.api_key')); + $hmac = hash_hmac('sha256', $payload['signature']['timestamp'] . $payload['signature']['token'], config('services.mailgun.api_key')); if (function_exists('hash_equals')) { return hash_equals($hmac, $payload['signature']['signature']); @@ -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']; } @@ -68,4 +68,4 @@ public function dataMapping(): array 'tag' => 'tags', ]; } -} +} \ No newline at end of file diff --git a/src/Drivers/PostmarkDriver.php b/src/Drivers/PostmarkDriver.php index 21bd74f..8d6fe12 100644 --- a/src/Drivers/PostmarkDriver.php +++ b/src/Drivers/PostmarkDriver.php @@ -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(); } @@ -100,4 +100,4 @@ public function dataMapping(): array 'user_agent' => 'UserAgent', ]; } -} +} \ No newline at end of file diff --git a/src/Jobs/ResendMailJob.php b/src/Jobs/ResendMailJob.php index 564536f..cb15814 100644 --- a/src/Jobs/ResendMailJob.php +++ b/src/Jobs/ResendMailJob.php @@ -24,6 +24,7 @@ public function __construct( private array $to, private array $cc = [], private array $bcc = [], + private array $replyTo = [] ) { // } @@ -42,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; @@ -54,16 +59,16 @@ private function setMessageRecipients(Message $message): self ->from($this->getFirstAddress($this->mail->from)) ->to($this->to); - if ($this->mail->cc) { - $message->cc($this->mail->cc); + if ($this->mail->cc || $this->cc) { + $message->cc($this->mail->cc ?? $this->cc); } - if ($this->mail->bcc) { - $message->bcc($this->mail->bcc); + if ($this->mail->bcc || $this->bcc) { + $message->bcc($this->mail->bcc ?? $this->bcc); } - if ($this->mail->reply_to) { - $message->replyTo($this->getFirstAddress($this->mail->reply_to)); + if ($this->mail->reply_to || $this->replyTo) { + $message->replyTo($this->getFirstAddress($this->mail->reply_to ?? $this->replyTo)); } return $this; diff --git a/src/Listeners/LogMailEvent.php b/src/Listeners/LogMailEvent.php index 0a42764..8ef722e 100644 --- a/src/Listeners/LogMailEvent.php +++ b/src/Listeners/LogMailEvent.php @@ -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)); } -} +} \ No newline at end of file From 2806a3d48e209b1d976a94eedea3a1778931d5b1 Mon Sep 17 00:00:00 2001 From: Baspa Date: Sun, 8 Sep 2024 13:43:14 +0000 Subject: [PATCH 2/5] Fix styling --- src/Actions/RegisterWebhooks.php | 8 ++++---- src/Actions/SendHighBounceRateNotifications.php | 3 +-- src/Controllers/WebhookController.php | 4 ++-- src/Drivers/MailDriver.php | 6 +++--- src/Drivers/MailgunDriver.php | 4 ++-- src/Drivers/PostmarkDriver.php | 2 +- src/Listeners/LogMailEvent.php | 4 ++-- 7 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Actions/RegisterWebhooks.php b/src/Actions/RegisterWebhooks.php index 711de19..5bbb50f 100644 --- a/src/Actions/RegisterWebhooks.php +++ b/src/Actions/RegisterWebhooks.php @@ -2,10 +2,10 @@ namespace Vormkracht10\Mails\Actions; -use Vormkracht10\Mails\Shared\AsAction; -use Vormkracht10\Mails\Facades\MailProvider; -use Illuminate\Console\View\Components\Factory; use Illuminate\Console\Concerns\InteractsWithIO; +use Illuminate\Console\View\Components\Factory; +use Vormkracht10\Mails\Facades\MailProvider; +use Vormkracht10\Mails\Shared\AsAction; class RegisterWebhooks { @@ -17,4 +17,4 @@ public function handle(Factory $components) components: $components ); } -} \ No newline at end of file +} diff --git a/src/Actions/SendHighBounceRateNotifications.php b/src/Actions/SendHighBounceRateNotifications.php index 35d7c8a..d384c96 100644 --- a/src/Actions/SendHighBounceRateNotifications.php +++ b/src/Actions/SendHighBounceRateNotifications.php @@ -13,7 +13,6 @@ class SendHighBounceRateNotifications /** * @param float|int $rate * @param float|int $threshold - * @return bool */ public function handle($rate, $threshold): bool { @@ -27,4 +26,4 @@ public function handle($rate, $threshold): bool return true; } -} \ No newline at end of file +} diff --git a/src/Controllers/WebhookController.php b/src/Controllers/WebhookController.php index 06d78ab..55de31e 100644 --- a/src/Controllers/WebhookController.php +++ b/src/Controllers/WebhookController.php @@ -12,7 +12,7 @@ class WebhookController { public function __invoke(Request $request, string $driver): Response { - if (!in_array($driver, array_column(Provider::cases(), 'value'))) { + if (! in_array($driver, array_column(Provider::cases(), 'value'))) { return response('Unknown provider.', status: 400); } @@ -27,4 +27,4 @@ public function __invoke(Request $request, string $driver): Response return response('Event processed.', status: 202); } -} \ No newline at end of file +} diff --git a/src/Drivers/MailDriver.php b/src/Drivers/MailDriver.php index 1592444..b53b340 100644 --- a/src/Drivers/MailDriver.php +++ b/src/Drivers/MailDriver.php @@ -38,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), @@ -50,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; } } @@ -137,4 +137,4 @@ public function unsubscribed(Mail $mail, string $timestamp): void 'opens' => $mail->opens + 1, ]); } -} \ No newline at end of file +} diff --git a/src/Drivers/MailgunDriver.php b/src/Drivers/MailgunDriver.php index fa9290e..8c43f51 100644 --- a/src/Drivers/MailgunDriver.php +++ b/src/Drivers/MailgunDriver.php @@ -19,7 +19,7 @@ public function verifyWebhookSignature(array $payload): bool return false; } - $hmac = hash_hmac('sha256', $payload['signature']['timestamp'] . $payload['signature']['token'], config('services.mailgun.api_key')); + $hmac = hash_hmac('sha256', $payload['signature']['timestamp'].$payload['signature']['token'], config('services.mailgun.api_key')); if (function_exists('hash_equals')) { return hash_equals($hmac, $payload['signature']['signature']); @@ -68,4 +68,4 @@ public function dataMapping(): array 'tag' => 'tags', ]; } -} \ No newline at end of file +} diff --git a/src/Drivers/PostmarkDriver.php b/src/Drivers/PostmarkDriver.php index 8d6fe12..502d283 100644 --- a/src/Drivers/PostmarkDriver.php +++ b/src/Drivers/PostmarkDriver.php @@ -100,4 +100,4 @@ public function dataMapping(): array 'user_agent' => 'UserAgent', ]; } -} \ No newline at end of file +} diff --git a/src/Listeners/LogMailEvent.php b/src/Listeners/LogMailEvent.php index 8ef722e..e534998 100644 --- a/src/Listeners/LogMailEvent.php +++ b/src/Listeners/LogMailEvent.php @@ -32,6 +32,6 @@ private function record($provider, $payload): void private function dispatch($provider, $payload): void { - dispatch(fn() => $this->record($provider, $payload)); + dispatch(fn () => $this->record($provider, $payload)); } -} \ No newline at end of file +} From a8b33d72433f997f68b8ba5688b17701dca927fb Mon Sep 17 00:00:00 2001 From: Baspa Date: Sun, 8 Sep 2024 15:46:45 +0200 Subject: [PATCH 3/5] wip --- .../HighBounceRateNotification.php | 2 +- src/Traits/SendsNotifications.php | 25 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Notifications/HighBounceRateNotification.php b/src/Notifications/HighBounceRateNotification.php index 5415da6..f38b4c3 100644 --- a/src/Notifications/HighBounceRateNotification.php +++ b/src/Notifications/HighBounceRateNotification.php @@ -70,4 +70,4 @@ public function toTelegram(): TelegramMessage return TelegramMessage::create() ->content($this->getMessage()); } -} +} \ No newline at end of file diff --git a/src/Traits/SendsNotifications.php b/src/Traits/SendsNotifications.php index de78287..521df7c 100644 --- a/src/Traits/SendsNotifications.php +++ b/src/Traits/SendsNotifications.php @@ -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 { @@ -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; + } +} \ No newline at end of file From 13807377573a73ddbd7d31b8f4395e50d610545e Mon Sep 17 00:00:00 2001 From: Baspa Date: Sun, 8 Sep 2024 13:47:08 +0000 Subject: [PATCH 4/5] Fix styling --- src/Notifications/HighBounceRateNotification.php | 2 +- src/Traits/SendsNotifications.php | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Notifications/HighBounceRateNotification.php b/src/Notifications/HighBounceRateNotification.php index f38b4c3..5415da6 100644 --- a/src/Notifications/HighBounceRateNotification.php +++ b/src/Notifications/HighBounceRateNotification.php @@ -70,4 +70,4 @@ public function toTelegram(): TelegramMessage return TelegramMessage::create() ->content($this->getMessage()); } -} \ No newline at end of file +} diff --git a/src/Traits/SendsNotifications.php b/src/Traits/SendsNotifications.php index 521df7c..62f94e9 100644 --- a/src/Traits/SendsNotifications.php +++ b/src/Traits/SendsNotifications.php @@ -7,9 +7,6 @@ trait SendsNotifications { - /** - * @param Notification $notification - */ public function send(Notification $notification, array $channels): void { foreach ($channels as $channel) { @@ -33,10 +30,6 @@ public function send(Notification $notification, array $channels): void /** * Prepare the notification for sending. - * - * @param Notification $notification - * @param string $channel - * @return Notification */ protected function prepareNotification(Notification $notification, string $channel): Notification { @@ -46,4 +39,4 @@ protected function prepareNotification(Notification $notification, string $chann return $notification; } -} \ No newline at end of file +} From 5b17886e6849c8efd16f5dc6e2cd35bf12e51d12 Mon Sep 17 00:00:00 2001 From: Baspa Date: Sun, 8 Sep 2024 13:56:00 +0000 Subject: [PATCH 5/5] Fix styling --- src/Models/MailEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/MailEvent.php b/src/Models/MailEvent.php index 7efaf74..2f27ccb 100644 --- a/src/Models/MailEvent.php +++ b/src/Models/MailEvent.php @@ -87,6 +87,6 @@ public function mail() protected function getEventClassAttribute(): string { - return 'Vormkracht10\Mails\Events\Mail' . Str::studly($this->type->value); + return 'Vormkracht10\Mails\Events\Mail'.Str::studly($this->type->value); } }