2026-09-29 15:29:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
namespace App\Cqs\Actions\Slides;
|
2026-09-29 15:29:43 +02:00
|
|
|
|
|
|
|
|
use App\Contracts\Action;
|
|
|
|
|
use App\Contracts\ActionInput;
|
2026-11-15 20:31:37 +01:00
|
|
|
use App\Contracts\HandlesFailure;
|
|
|
|
|
use App\Contracts\HandlesSuccess;
|
2026-11-14 16:06:12 +01:00
|
|
|
use App\Lib\PublicS3Disk;
|
2026-09-29 15:29:43 +02:00
|
|
|
use App\Models\Slide;
|
2026-11-14 16:06:12 +01:00
|
|
|
use App\Services\ImageOptimizerService;
|
2026-11-15 20:31:37 +01:00
|
|
|
use App\Traits\TracksPendingStoragePaths;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2026-09-29 15:29:43 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
2026-09-29 15:29:43 +02:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-10 14:28:00 +01:00
|
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Slides\CreateSlideInput, \App\Models\Slide>
|
2026-11-15 20:31:37 +01:00
|
|
|
* @implements \App\Contracts\HandlesSuccess<\App\Cqs\Actions\Slides\CreateSlideInput, \App\Models\Slide>
|
|
|
|
|
* @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Slides\CreateSlideInput>
|
2026-09-29 15:29:43 +02:00
|
|
|
*/
|
2026-11-15 20:31:37 +01:00
|
|
|
final class CreateSlide implements Action, HandlesFailure, HandlesSuccess
|
2026-09-29 15:29:43 +02:00
|
|
|
{
|
2026-11-15 20:31:37 +01:00
|
|
|
use TracksPendingStoragePaths;
|
|
|
|
|
|
2026-11-14 16:06:12 +01:00
|
|
|
public function __construct(
|
|
|
|
|
protected readonly StorageService $storageService,
|
|
|
|
|
protected readonly PublicS3Disk $s3Disk,
|
|
|
|
|
protected readonly ImageOptimizerService $imageOptimizerService,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
/**
|
2026-11-10 14:28:00 +01:00
|
|
|
* @param \App\Cqs\Actions\Slides\CreateSlideInput $input
|
2026-09-29 15:29:43 +02:00
|
|
|
*/
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function __invoke(ActionInput $input): Slide
|
|
|
|
|
{
|
|
|
|
|
Assert::isInstanceOf($input, CreateSlideInput::class);
|
|
|
|
|
|
2026-09-29 15:38:59 +02:00
|
|
|
$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,
|
|
|
|
|
];
|
|
|
|
|
|
2026-11-14 16:06:12 +01:00
|
|
|
if ($input->picture instanceof UploadedFile) {
|
2026-11-15 20:31:37 +01:00
|
|
|
$storedPath = $this->storeFile($input->picture);
|
|
|
|
|
$attributes['picture'] = $storedPath;
|
|
|
|
|
$this->trackUploadedPath($input, $storedPath);
|
2026-09-29 15:29:43 +02:00
|
|
|
}
|
2026-09-29 15:38:59 +02:00
|
|
|
|
|
|
|
|
$slide = new Slide();
|
|
|
|
|
$slide->fill($attributes);
|
2026-09-29 15:29:43 +02:00
|
|
|
$slide->saveOrFail();
|
|
|
|
|
|
|
|
|
|
return $slide;
|
|
|
|
|
}
|
2026-11-14 16:06:12 +01:00
|
|
|
|
2026-11-15 20:31:37 +01:00
|
|
|
#[\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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 16:06:12 +01:00
|
|
|
protected function storeFile(UploadedFile $file): StorageFilePath
|
|
|
|
|
{
|
|
|
|
|
$optimized = $this->imageOptimizerService->optimize($file);
|
|
|
|
|
|
|
|
|
|
return $this->storageService->storeFileOntoDisk($optimized, $this->s3Disk->disk);
|
|
|
|
|
}
|
2026-09-29 15:29:43 +02:00
|
|
|
}
|