- Implemented HandlesSuccess and HandlesFailure interfaces in CreateProduct and UpdateProduct actions. - Added tracking of uploaded paths and cleanup on failure. - Enhanced image storage handling to ensure proper cleanup of orphaned files. feat(ranges): add success and failure handling for range actions - Implemented HandlesSuccess and HandlesFailure interfaces in CreateRange and UpdateRange actions. - Added tracking of uploaded paths and cleanup on failure. - Enhanced image storage handling to ensure proper cleanup of orphaned files. feat(slides): add success and failure handling for slide actions - Implemented HandlesSuccess and HandlesFailure interfaces in CreateSlide and UpdateSlide actions. - Added tracking of uploaded paths and cleanup on failure. - Enhanced image storage handling to ensure proper cleanup of orphaned files. chore: introduce TracksPendingStoragePaths trait - Created a new trait to manage tracking of uploaded and obsolete storage paths. - This will streamline the handling of file cleanup across various actions. test: add tests for action failure handling and cleanup - Added tests to verify that uploaded files are cleaned up on action failure. - Ensured that previous files are preserved during rollback scenarios.
86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\Slides;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\HandlesFailure;
|
|
use App\Contracts\HandlesSuccess;
|
|
use App\Models\Slide;
|
|
use App\Services\PublicImageStorageService;
|
|
use App\Traits\TracksPendingStoragePaths;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Carbon;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<\App\Cqs\Actions\Slides\UpdateSlideInput, \App\Models\Slide>
|
|
* @implements \App\Contracts\HandlesSuccess<\App\Cqs\Actions\Slides\UpdateSlideInput, \App\Models\Slide>
|
|
* @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Slides\UpdateSlideInput>
|
|
*/
|
|
final class UpdateSlide implements Action, HandlesFailure, HandlesSuccess
|
|
{
|
|
use TracksPendingStoragePaths;
|
|
|
|
public function __construct(
|
|
private readonly PublicImageStorageService $publicImageStorageService,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Cqs\Actions\Slides\UpdateSlideInput $input
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): Slide
|
|
{
|
|
Assert::isInstanceOf($input, UpdateSlideInput::class);
|
|
|
|
$previousPicture = $input->slide->picture;
|
|
$newPicture = null;
|
|
|
|
$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) {
|
|
$newPicture = $this->publicImageStorageService->store($input->picture);
|
|
$attributes['picture'] = $newPicture;
|
|
$this->trackUploadedPath($input, $newPicture);
|
|
}
|
|
|
|
$slide = $input->slide;
|
|
$slide->fill($attributes);
|
|
$slide->saveOrFail();
|
|
|
|
$this->trackReplacedPath($input, $previousPicture, $newPicture);
|
|
|
|
return $slide;
|
|
}
|
|
|
|
#[\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);
|
|
}
|
|
}
|
|
}
|