2026-09-29 16:59:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
namespace App\Cqs\Actions\Ranges;
|
2026-09-29 16:59:07 +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 16:59:07 +02:00
|
|
|
use App\Models\Range;
|
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;
|
|
|
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
2026-09-29 16:59:07 +02:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-10 14:28:00 +01:00
|
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Ranges\CreateRangeInput, \App\Models\Range>
|
2026-11-15 20:31:37 +01:00
|
|
|
* @implements \App\Contracts\HandlesSuccess<\App\Cqs\Actions\Ranges\CreateRangeInput, \App\Models\Range>
|
|
|
|
|
* @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Ranges\CreateRangeInput>
|
2026-09-29 16:59:07 +02:00
|
|
|
*/
|
2026-11-15 20:31:37 +01:00
|
|
|
final class CreateRange implements Action, HandlesFailure, HandlesSuccess
|
2026-09-29 16:59:07 +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 16:59:07 +02:00
|
|
|
/**
|
2026-11-10 14:28:00 +01:00
|
|
|
* @param \App\Cqs\Actions\Ranges\CreateRangeInput $input
|
2026-09-29 16:59:07 +02:00
|
|
|
*/
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function __invoke(ActionInput $input): Range
|
|
|
|
|
{
|
|
|
|
|
Assert::isInstanceOf($input, CreateRangeInput::class);
|
|
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
|
'name' => $input->name,
|
|
|
|
|
'description' => $input->description,
|
|
|
|
|
'picture_alt' => $input->pictureMetadata->alt ?? '',
|
|
|
|
|
'picture_title' => $input->pictureMetadata->title ?? '',
|
|
|
|
|
'status' => $input->status,
|
|
|
|
|
];
|
|
|
|
|
|
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 16:59:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$range = new Range();
|
|
|
|
|
$range->fill($attributes);
|
|
|
|
|
$range->saveOrFail();
|
|
|
|
|
|
|
|
|
|
return $range;
|
|
|
|
|
}
|
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 16:59:07 +02:00
|
|
|
}
|