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-09-29 16:59:07 +02:00
|
|
|
use App\Models\Range;
|
2026-11-15 17:13:39 +01:00
|
|
|
use App\Services\PublicImageStorageService;
|
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 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\UpdateRangeInput, \App\Models\Range>
|
2026-11-15 20:31:37 +01:00
|
|
|
* @implements \App\Contracts\HandlesSuccess<\App\Cqs\Actions\Ranges\UpdateRangeInput, \App\Models\Range>
|
|
|
|
|
* @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Ranges\UpdateRangeInput>
|
2026-09-29 16:59:07 +02:00
|
|
|
*/
|
2026-11-15 20:31:37 +01:00
|
|
|
final class UpdateRange 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(
|
2026-11-15 17:13:39 +01:00
|
|
|
private readonly PublicImageStorageService $publicImageStorageService,
|
2026-11-14 16:06:12 +01:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
/**
|
2026-11-10 14:28:00 +01:00
|
|
|
* @param \App\Cqs\Actions\Ranges\UpdateRangeInput $input
|
2026-09-29 16:59:07 +02:00
|
|
|
*/
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function __invoke(ActionInput $input): Range
|
|
|
|
|
{
|
|
|
|
|
Assert::isInstanceOf($input, UpdateRangeInput::class);
|
|
|
|
|
|
2026-11-15 17:13:39 +01:00
|
|
|
$previousPicture = $input->range->picture;
|
|
|
|
|
$newPicture = null;
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
$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 17:13:39 +01:00
|
|
|
$newPicture = $this->publicImageStorageService->store($input->picture);
|
|
|
|
|
$attributes['picture'] = $newPicture;
|
2026-11-15 20:31:37 +01:00
|
|
|
$this->trackUploadedPath($input, $newPicture);
|
2026-09-29 16:59:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$range = $input->range;
|
|
|
|
|
$range->fill($attributes);
|
|
|
|
|
$range->saveOrFail();
|
|
|
|
|
|
2026-11-15 20:31:37 +01:00
|
|
|
$this->trackReplacedPath($input, $previousPicture, $newPicture);
|
2026-11-14 16:06:12 +01:00
|
|
|
|
2026-11-15 17:13:39 +01:00
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->pullObsoletePaths($input) as $path) {
|
|
|
|
|
$this->publicImageStorageService->deleteIfExists($path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
public function onFailure(ActionInput $input, \Throwable $exception): void
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->pullUploadedPaths($input) as $path) {
|
|
|
|
|
$this->publicImageStorageService->deleteIfExists($path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-29 16:59:07 +02:00
|
|
|
}
|