ProjectDynamicObserver.php 1.45 KB
<?php

namespace App\Observers;

use App\Helpers\UploadHelper;
use App\Jobs\MediaTranscodeJob;
use App\Models\ProjectDynamic;
use Illuminate\Support\Str;

class ProjectDynamicObserver
{
    public bool $afterCommit = true;

    /**
     * @throws \JsonException
     */
    public function created(ProjectDynamic $dynamic): void
    {
        if ($dynamic->getAttribute('type') !== 'image' && !Str::endsWith($dynamic->getAttribute('url'), ['.mp3', '.mp4'])) {
            if ($dynamic->getAttribute('type') === 'audio') {
                MediaTranscodeJob::dispatch($dynamic->getAttribute('url'), [
                    "TemplateId"   => "S00000001-300050",
                    "OutputObject" => UploadHelper::getFileName('audio') . '.mp3',
                    "UserData"     => json_encode(['event_id' => $dynamic->getKey(), 'event_type' => class_basename($dynamic), 'event_attribute' => 'properties->url'], JSON_THROW_ON_ERROR)
                ]);
            }

            if ($dynamic->getAttribute('type') === 'video') {
                MediaTranscodeJob::dispatch($dynamic->getAttribute('url'), [
                    "TemplateId"   => "S00000001-200000",
                    "OutputObject" => UploadHelper::getFileName('video') . '.mp4',
                    "UserData"     => json_encode(['event_id' => $dynamic->getKey(), 'event_type' => class_basename($dynamic), 'event_attribute' => 'properties->url'], JSON_THROW_ON_ERROR)
                ]);
            }
        }
    }
}