Rabbi_Gerzi/backend/app/Element/UseCases/UpdateElement/UpdateLongPdfPath.php
Yisroel Baum 9bbabc7fa6
route element file uploads
Add short and long PDF paths to elements, keep pdfPath as a short PDF compatibility alias, and route generic file uploads by fileType.
2026-06-14 23:17:56 +03:00

65 lines
1.6 KiB
PHP

<?php
namespace App\Element\UseCases\UpdateElement;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
use App\Shared\Files\FileUploader;
class UpdateLongPdfPath
{
public function __construct(
private ElementRepository $elementRepository,
private FileUploader $fileUploader,
) {
}
/**
* @throws BadRequestException
* @throws NotFoundException
*/
public function execute(UpdateLongPdfPathRequest $request): Element
{
if ($request->id === null) {
throw new BadRequestException('id is required');
}
if ($request->longPdfPath === null) {
throw new BadRequestException('longPdfPath is required');
}
$element = $this->elementRepository->find($request->id);
if ($element === null) {
throw new NotFoundException('Element not found');
}
$longPdfPath = $this->nullableString($request->longPdfPath);
if ($longPdfPath === null) {
$this->deleteManagedFile($element->getLongPdfPath());
}
$element->setLongPdfPath($longPdfPath);
return $this->elementRepository->update($element);
}
private function nullableString(string $value): ?string
{
if ($value === '') {
return null;
}
return $value;
}
private function deleteManagedFile(?string $path): void
{
if ($path === null) {
return;
}
$this->fileUploader->delete($path);
}
}