ldc-cocotte/app/Cqs/Actions/Contact/SubmitContactForm.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

86 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Cqs\Actions\Contact;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Contracts\HandlesFailure;
use App\Contracts\HandlesSuccess;
use App\Lib\PrivateS3Disk;
use App\Traits\TracksPendingStoragePaths;
use Domain\Models\ValueObjects\AttachedFile;
use Domain\Models\ValueObjects\ContactFormData;
use Illuminate\Http\UploadedFile;
use Uwil\Storage\Contracts\StorageService;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<
* \App\Cqs\Actions\Contact\SubmitContactFormInput,
* \Domain\Models\ValueObjects\ContactFormData
* >
* @implements \App\Contracts\HandlesSuccess<
* \App\Cqs\Actions\Contact\SubmitContactFormInput,
* \Domain\Models\ValueObjects\ContactFormData
* >
* @implements \App\Contracts\HandlesFailure<\App\Cqs\Actions\Contact\SubmitContactFormInput>
*/
final class SubmitContactForm implements Action, HandlesFailure, HandlesSuccess
{
use TracksPendingStoragePaths;
public function __construct(
private readonly StorageService $storageService,
private readonly PrivateS3Disk $privateS3Disk,
) {
}
/**
* @param \App\Cqs\Actions\Contact\SubmitContactFormInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): ContactFormData
{
Assert::isInstanceOf($input, SubmitContactFormInput::class);
$attachedFile = null;
if ($input->file instanceof UploadedFile && $input->file->isValid()) {
$storedPath = $this->storageService->storeUploadedOntoDisk($input->file, $this->privateS3Disk->disk);
$this->trackUploadedPath($input, $storedPath);
$attachedFile = new AttachedFile(
path: $storedPath->value,
originalName: $input->file->getClientOriginalName(),
mimeType: $input->file->getMimeType() ?? 'application/octet-stream',
);
}
return new ContactFormData(
lastName: $input->lastName,
firstName: $input->firstName,
email: $input->email,
subject: $input->subject,
message: $input->message,
orderRef: $input->orderRef,
file: $attachedFile,
);
}
#[\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->privateS3Disk->disk)) {
$this->storageService->removeFromDisk($path, $this->privateS3Disk->disk);
}
}
}
}