* @implements \App\Contracts\HandlesSuccess<\App\Cqs\Actions\Slides\CreateSlideInput, \App\Models\Slide> * @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Slides\CreateSlideInput> */ final class CreateSlide implements Action, HandlesFailure, HandlesSuccess { use TracksPendingStoragePaths; public function __construct( protected readonly StorageService $storageService, protected readonly PublicS3Disk $s3Disk, protected readonly ImageOptimizerService $imageOptimizerService, ) { } /** * @param \App\Cqs\Actions\Slides\CreateSlideInput $input */ #[\Override] public function __invoke(ActionInput $input): Slide { Assert::isInstanceOf($input, CreateSlideInput::class); $attributes = [ 'title' => $input->title, 'text' => $input->text, 'picture_alt' => $input->pictureAlt, 'picture_title' => $input->pictureTitle, 'link' => $input->link, 'link_label' => $input->linkLabel, 'link_new_tab' => $input->linkNewTab, 'connected' => $input->connected, 'date_start' => $input->period->start !== null ? Carbon::instance($input->period->start) : null, 'date_end' => $input->period->end !== null ? Carbon::instance($input->period->end) : null, ]; if ($input->picture instanceof UploadedFile) { $storedPath = $this->storeFile($input->picture); $attributes['picture'] = $storedPath; $this->trackUploadedPath($input, $storedPath); } $slide = new Slide(); $slide->fill($attributes); $slide->saveOrFail(); return $slide; } #[\Override] public function onSuccess(ActionInput $input, mixed $result): void { $this->clearTrackedPaths($input); } #[\Override] public function onFailure(ActionInput $input, \Throwable $exception): void { foreach ($this->pullUploadedPaths($input) as $path) { if ($this->storageService->fileExistsOnDisk($path, $this->s3Disk->disk)) { $this->storageService->removeFromDisk($path, $this->s3Disk->disk); } } } protected function storeFile(UploadedFile $file): StorageFilePath { $optimized = $this->imageOptimizerService->optimize($file); return $this->storageService->storeFileOntoDisk($optimized, $this->s3Disk->disk); } }