ldc-cocotte/app/Traits/TracksPendingStoragePaths.php
Jérémy Munsch 86e7340c48
feat(products): add success and failure handling for product actions
- 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.
2026-11-15 20:31:37 +01:00

75 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Traits;
use App\Contracts\ActionInput;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
trait TracksPendingStoragePaths
{
/** @var array<int, list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>> */
private array $pendingUploadedPaths = [];
/** @var array<int, list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>> */
private array $pendingObsoletePaths = [];
protected function trackUploadedPath(ActionInput $input, StorageFilePath $path): void
{
$key = spl_object_id($input);
$this->pendingUploadedPaths[$key][] = $path;
}
protected function trackReplacedPath(
ActionInput $input,
?StorageFilePath $oldPath,
?StorageFilePath $newPath,
): void {
if ($oldPath === null || $newPath === null || $oldPath->value === $newPath->value) {
return;
}
$this->trackObsoletePath($input, $oldPath);
}
protected function trackObsoletePath(ActionInput $input, ?StorageFilePath $path): void
{
if ($path === null) {
return;
}
$key = spl_object_id($input);
$this->pendingObsoletePaths[$key][] = $path;
}
/**
* @return list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>
*/
protected function pullUploadedPaths(ActionInput $input): array
{
$key = spl_object_id($input);
$paths = $this->pendingUploadedPaths[$key] ?? [];
$this->clearTrackedPaths($input);
return $paths;
}
/**
* @return list<\Uwil\Storage\Lib\ValueObjects\StorageFilePath>
*/
protected function pullObsoletePaths(ActionInput $input): array
{
$key = spl_object_id($input);
$paths = $this->pendingObsoletePaths[$key] ?? [];
$this->clearTrackedPaths($input);
return $paths;
}
protected function clearTrackedPaths(ActionInput $input): void
{
$key = spl_object_id($input);
unset($this->pendingUploadedPaths[$key], $this->pendingObsoletePaths[$key]);
}
}