Skip to content

Instantly share code, notes, and snippets.

@bobbybouwmann
Created July 13, 2020 09:23
Show Gist options
  • Select an option

  • Save bobbybouwmann/ecb7cfd50f76048504dc604f9e5129ac to your computer and use it in GitHub Desktop.

Select an option

Save bobbybouwmann/ecb7cfd50f76048504dc604f9e5129ac to your computer and use it in GitHub Desktop.

Revisions

  1. bobbybouwmann created this gist Jul 13, 2020.
    53 changes: 53 additions & 0 deletions CardActivationReminder.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?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;
    }
    }
    62 changes: 62 additions & 0 deletions EmailChangedEmail.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?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;
    }
    }
    11 changes: 11 additions & 0 deletions SendCardActivationReminders.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?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));
    }
    13 changes: 13 additions & 0 deletions SendEmailChangedOldEmailNotification.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?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);
    }
    }