. */ namespace Poniverse\Ponyfm\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Poniverse\Ponyfm\Models\Email; abstract class BaseNotification extends Mailable { use Queueable, SerializesModels; /** @var Email */ protected $emailRecord; /** @var \Poniverse\Ponyfm\Models\Notification */ protected $notificationRecord; /** @var \Poniverse\Ponyfm\Models\Activity */ protected $activityRecord; /** @var \Poniverse\Ponyfm\Models\User */ protected $initiatingUser; /** * Create a new message instance. * * @param Email $email */ public function __construct(Email $email) { $this->emailRecord = $email; $this->notificationRecord = $email->notification; $this->activityRecord = $email->notification->activity; $this->initiatingUser = $email->notification->activity->initiatingUser; } /** * Build the message. * * @return $this */ abstract public function build(); /** * Generates an unsubscribe URL unique to the user. * * @return string */ protected function generateUnsubscribeUrl() { $subscriptionKey = encrypt($this->emailRecord->getSubscription()->id); return route('email:unsubscribe', ['subscriptionKey' => $subscriptionKey]); } /** * Generates a trackable URL to the content item this email is about. * * @return string */ protected function generateNotificationUrl() { $emailKey = encrypt($this->emailRecord->id); return route('email:click', ['emailKey' => $emailKey]); } /** * Helper method to eliminate duplication between different types of notifications. * Use it inside the build() method on this class's children. * * @param string $templateName * @param string $subject * @param array $extraVariables * @return $this */ protected function renderEmail(string $templateName, string $subject, array $extraVariables) { return $this ->subject($subject) ->view("emails.{$templateName}") ->text("emails.{$templateName}_plaintext") ->with(array_merge($extraVariables, [ 'notificationUrl' => $this->generateNotificationUrl(), 'unsubscribeUrl' => $this->generateUnsubscribeUrl() ])); } }