Files
muninn/app/Livewire/VideoProjects/Show.php
T

110 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Livewire\VideoProjects;
use App\Jobs\RunWorkflowStepJob;
use App\Models\VideoProject;
use App\Models\WorkflowStep;
use App\Models\WorkflowTemplate;
use App\Support\VideoWorkflow\StepHandlers\StepHandlerManager;
use App\Support\VideoWorkflow\WorkflowEngine;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('components.layouts.app')]
class Show extends Component
{
public VideoProject $videoProject;
public string $workflowTemplateId = '';
public ?string $notice = null;
public function mount(VideoProject $videoProject): void
{
$this->videoProject = $videoProject;
}
public function render()
{
$this->videoProject->refresh();
return view('livewire.video-projects.show', [
'steps' => $this->videoProject->steps()->with('assets')->get(),
'assets' => $this->videoProject->assets()->orderByDesc('created_at')->get(),
'templates' => WorkflowTemplate::orderBy('name')->get(),
'hasActiveStep' => $this->videoProject->steps()
->whereIn('status', ['pending', 'in_progress'])
->exists(),
]);
}
public function startWithTemplate(WorkflowEngine $engine): void
{
if (! $this->workflowTemplateId) {
$this->notice = 'Pick a template first.';
return;
}
$template = WorkflowTemplate::findOrFail($this->workflowTemplateId);
$engine->startFromTemplate($this->videoProject, $template);
$this->runCurrentStepIfHandled($engine);
}
public function startDefault(WorkflowEngine $engine): void
{
$engine->start($this->videoProject, [
['type' => 'script_generation', 'requires_approval' => true],
['type' => 'tts_voiceover', 'requires_approval' => true],
['type' => 'storyboard', 'requires_approval' => true],
['type' => 'video_generation', 'requires_approval' => true],
['type' => 'video_editing', 'requires_approval' => true],
['type' => 'thumbnail_generation', 'requires_approval' => true],
['type' => 'metadata_generation', 'requires_approval' => true],
['type' => 'youtube_publish', 'requires_approval' => true],
]);
$this->runCurrentStepIfHandled($engine);
}
public function runStep(int $stepId): void
{
RunWorkflowStepJob::dispatch($stepId);
$this->notice = 'Step queued to run.';
}
public function approveStep(int $stepId, WorkflowEngine $engine): void
{
$engine->approveStep(WorkflowStep::findOrFail($stepId));
$this->runCurrentStepIfHandled($engine);
}
public function rejectStep(int $stepId, WorkflowEngine $engine): void
{
$engine->rejectStep(WorkflowStep::findOrFail($stepId));
}
public function retryStep(int $stepId, WorkflowEngine $engine): void
{
$step = $engine->retryStep(WorkflowStep::findOrFail($stepId));
RunWorkflowStepJob::dispatch($step->id);
}
public function skipStep(int $stepId, WorkflowEngine $engine): void
{
$engine->skipStep(WorkflowStep::findOrFail($stepId));
$this->runCurrentStepIfHandled($engine);
}
private function runCurrentStepIfHandled(WorkflowEngine $engine): void
{
$current = $engine->currentStep($this->videoProject->fresh());
if ($current && $current->status === 'in_progress' && app(StepHandlerManager::class)->resolve($current->type)) {
RunWorkflowStepJob::dispatch($current->id);
}
}
}