* @implements \App\Contracts\HandlesSuccess<\App\Cqs\Actions\Ranges\UpdateRangeInput, \App\Models\Range> * @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Ranges\UpdateRangeInput> */ final class UpdateRange implements Action, HandlesFailure, HandlesSuccess { use TracksPendingStoragePaths; public function __construct( private readonly PublicImageStorageService $publicImageStorageService, ) { } /** * @param \App\Cqs\Actions\Ranges\UpdateRangeInput $input */ #[\Override] public function __invoke(ActionInput $input): Range { Assert::isInstanceOf($input, UpdateRangeInput::class); $previousPicture = $input->range->picture; $newPicture = null; $attributes = [ 'name' => $input->name, 'description' => $input->description, 'picture_alt' => $input->pictureMetadata->alt ?? '', 'picture_title' => $input->pictureMetadata->title ?? '', 'status' => $input->status, ]; if ($input->picture instanceof UploadedFile) { $newPicture = $this->publicImageStorageService->store($input->picture); $attributes['picture'] = $newPicture; $this->trackUploadedPath($input, $newPicture); } $range = $input->range; $range->fill($attributes); $range->saveOrFail(); $this->trackReplacedPath($input, $previousPicture, $newPicture); return $range; } #[\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); } } }