ActivityMaterialMail.php 3.55 KB
<?php

namespace App\Mail;

use App\Enums\ActivitySongTypeEnum;
use App\Models\Activity;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Attachment;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;

class ActivityMaterialMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public string $guide;

    public string $guideName;

    public string $karaoke;

    public string $karaokeName;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(protected Activity $activity, protected User $user)
    {
        $this->queue = 'mail';

        $this->guide     = data_get($this->activity, 'expand.guide_source.url', '');
        $this->guideName = $this->getFileName('guide');

        $this->karaoke     = data_get($this->activity, 'expand.karaoke_source.url', '');
        $this->karaokeName = $this->getFileName('karaoke');

        $this->to($this->user->getAttribute('email'), $this->user->getAttribute('nick_name'));
    }

    /**
     * @return int
     */
    protected function getActivityType(): int
    {
        return $this->activity->getAttribute('song_type');
    }

    /**
     * @return string
     */
    protected function getActivityName(): string
    {
        return $this->activity->getAttribute('song_name');
    }

    /**
     * @return string
     */
    protected function getActivityLyric(): string
    {
        return $this->activity->getAttribute('lyric');
    }

    /**
     * @param string $type
     * @return string
     */
    protected function getFileName(string $type): string
    {
        return match ($type) {
            'guide' => $this->getActivityType() === ActivitySongTypeEnum::SONG->value ? '导唱(含人声)' : '音频',
            'karaoke' => $this->getActivityType() === ActivitySongTypeEnum::SONG->value ? '伴奏(不含人声)' : '伴奏'
        };
    }

    /**
     * @param string $url
     * @return string
     */
    protected function getFileType(string $url): string
    {
        return Str::afterLast($url, '.');
    }

    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: '【泡泡留声】《' . $this->getActivityName() . '》- 物料资源',
        );
    }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.activity-material',
            with: [
                'userName' => $this->user->getAttribute('nick_name'),
                'songName' => $this->getActivityName(),
                'lyric'    => $this->getActivityLyric(),
            ]
        );
    }

//    public function attachments(): array
//    {
//        $data = [Attachment::fromPath($this->guide)->as($this->guideName . '.' . $this->getFileType($this->guide))->withMime('audio/*')];
//
//        if ($this->karaoke) {
//            $data[] = Attachment::fromPath($this->karaoke)->as($this->karaokeName . '.' . $this->getFileType($this->karaoke))->withMime('audio/*');
//        }
//
//        if ($this->getActivityLyric()) {
//            $data[] = Attachment::fromData(fn() => $this->getActivityLyric(), '歌词.lyric');
//        }
//
//        return $data;
//    }
}