Skip to content

Instantly share code, notes, and snippets.

@bobbybouwmann
Created July 13, 2020 09:23
Show Gist options
  • Save bobbybouwmann/ecb7cfd50f76048504dc604f9e5129ac to your computer and use it in GitHub Desktop.
Save bobbybouwmann/ecb7cfd50f76048504dc604f9e5129ac to your computer and use it in GitHub Desktop.
#33409
<?php
final class CardActivationReminder extends Notification implements MailNotificationInterface
{
use Queueable;
/**
* @var User
*/
private $user;
/**
* @var Card
*/
private $card;
/**
* @var bool
*/
private $hidden;
public function __construct(User $user, Card $card, $hidden = false)
{
$this->user = $user;
$this->card = $card;
$this->hidden = $hidden;
}
public function via($notifiable): array
{
return [SendInBlueChannel::class, 'database'];
}
public function toSendInBlue($notifiable): SendInBlueMessage
{
return new SendInBlueMessage($this->sendInBlueKey());
}
public function toArray($notifiable): array
{
return [
'sendToEmail' => $this->user->email,
'cardId' => $this->card->id,
'hidden' => $this->hidden,
'subject' => $this->sendInBlueKey(),
];
}
public function sendInBlueKey(): string
{
return Emails::CARD_ACTIVATION_REMINDER;
}
}
<?php
final class EmailChangedEmail extends Notification implements ShouldQueue, MailNotificationInterface
{
use Queueable;
/**
* @var string
*/
public $previousEmail;
/**
* @var string
*/
public $newEmail;
/**
* @var string
*/
private $sendToEmail;
public function __construct(string $previousEmail, string $newEmail, string $sendToEmail)
{
$this->previousEmail = $previousEmail;
$this->newEmail = $newEmail;
$this->sendToEmail = $sendToEmail;
}
public function via($notifiable): array
{
return [SendInBlueChannel::class, 'database'];
}
public function toSendInBlue($notifiable): SendInBlueMessage
{
$user = User::where('email', $this->previousEmail)
->orWhere('email', $this->newEmail)
->first();
return (new SendInBlueMessage($this->sendInBlueKey()))
->language($user->language)
->params([
'previousEmail' => $this->previousEmail,
'newEmail' => $this->newEmail,
]);
}
public function toArray($notifiable): array
{
return [
'sendToEmail' => $notifiable instanceof AnonymousNotifiable ? $this->sendToEmail : $notifiable->email,
'previousEmail' => $this->previousEmail,
'newEmail' => $this->newEmail,
'subject' => $this->sendInBlueKey(),
];
}
public function sendInBlueKey(): string
{
return Emails::EMAIL_CHANGED;
}
}
<?php
// Used in a scheduled command that doesn't need to send the email and only store it in the database
Notification::route('database', $user->notifications())
->notifyNow(new CardActivationReminder($user, $card, true));
// A method on the Uer model for a single card
public function sendCardActivationReminder(User $user, Card $card): void
{
$this->notify(new CardActivationReminder($user, $card));
}
<?php
final class SendEmailChangedOldEmailNotification
{
public function handle(EmailChanged $event): void
{
$notification = new EmailChangedEmail($event->previousEmail, $event->newEmail, $event->previousEmail);
Notification::route('sendInBlue', $event->previousEmail)
->route('database', $event->user->notifications())
->notifyNow($notification);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment