Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

2.7 KB
Raw Blame
<?php

namespace HiEvents\Mail;

use HiEvents\Mail\Concerns\ContainsEventContent;
use HiEvents\Services\Domain\Mail\MailBrandingService;
use HiEvents\Services\Domain\Mail\UnpublishedEventMailPolicy;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\SentMessage;
use Illuminate\Queue\SerializesModels;

abstract class BaseMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public function __construct()
    {
        $this->afterCommit();
    }

    abstract public function envelope(): Envelope;

    abstract public function content(): Content;

    public function showsHiEventsPromo(): bool
    {
        return false;
    }

    public function isSentToBuyers(): bool
    {
        return true;
    }

    public function send($mailer): ?SentMessage
    {
        if ($this instanceof ContainsEventContent
            && app(UnpublishedEventMailPolicy::class)->shouldWithholdEventContent(
                $this->getContentEvent(),
                $this->allRecipientAddresses(),
            )) {
            $replacement = $this->withheldEventContentReplacement()
                ->to($this->asRecipients($this->to))
                ->cc($this->asRecipients($this->cc))
                ->bcc($this->asRecipients($this->bcc))
                ->locale($this->locale);

            if ($this->mailer !== null) {
                $replacement->mailer($this->mailer);
            }

            return $replacement->send($mailer);
        }

        return $this->withMailBranding(fn () => parent::send($mailer));
    }

    private function withMailBranding(callable $callback): mixed
    {
        $branding = app(MailBrandingService::class)->forMail($this);

        if ($branding->senderName !== null && $this->from === []) {
            $this->from(config('mail.from.address'), $branding->senderName);
        }

        $view = app('view');
        $view->share('mailBranding', $branding);

        try {
            return $callback();
        } finally {
            $view->share('mailBranding', null);
        }
    }

    private function asRecipients(array $addresses): array
    {
        return array_map(
            fn (array $recipient) => ['name' => $recipient['name'] ?? null, 'email' => $recipient['address']],
            $addresses,
        );
    }

    /**
     * @return string[]
     */
    private function allRecipientAddresses(): array
    {
        return collect([...$this->to, ...$this->cc, ...$this->bcc])
            ->pluck('address')
            ->filter()
            ->values()
            ->all();
    }
}