Rabbi_Gerzi/backend/app/Element/UseCases/CreateElement/CreateElement.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

112 lines
3.4 KiB
PHP

<?php
namespace App\Element\UseCases\CreateElement;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Set\Set as DomainSet;
use App\Set\SetRepository;
use DomainException;
class CreateElement
{
public function __construct(
private ElementRepository $elementRepo,
private SetRepository $setRepo,
) {
}
/**
* @throws BadRequestException
* @throws DomainException
*/
public function execute(CreateElementRequest $request): Element
{
if ($request->setId === null) {
throw new BadRequestException('setId is required');
}
if ($request->title === null || $request->title === '') {
throw new BadRequestException('title is required');
}
$description = $request->description ?? '';
$iconImageUrl = $request->iconImageUrl === ''
? null
: $request->iconImageUrl;
$richText = $request->richText ?? '';
$shortPdfPath = $request->shortPdfPath === ''
? null
: $request->shortPdfPath;
$longPdfPath = $request->longPdfPath === ''
? null
: $request->longPdfPath;
$youtubeUrl = $request->youtubeUrl === ''
? null
: $request->youtubeUrl;
$set = $this->setRepo->find($request->setId);
if ($set === null) {
throw new DomainException(
"Set with id: {$request->setId} doesnt exist"
);
}
if ($request->parentElementId === null) {
$this->validateNoRootElementExists($set);
return $this->elementRepo->create(new CreateElementDto(
set: $set,
title: $request->title,
description: $description,
iconImageUrl: $iconImageUrl,
richText: $richText,
shortPdfPath: $shortPdfPath,
longPdfPath: $longPdfPath,
youtubeUrl: $youtubeUrl,
parentElement: null,
));
}
$parentElement = $this->elementRepo->find(
$request->parentElementId
);
if ($parentElement === null) {
throw new DomainException(
"Element with id: {$request->parentElementId} doesnt exist"
);
}
if ($parentElement->getSet()->getId() !== $set->getId()) {
throw new DomainException(
'Parent element must belong to the same set'
);
}
return $this->elementRepo->create(new CreateElementDto(
set: $set,
title: $request->title,
description: $description,
iconImageUrl: $iconImageUrl,
richText: $richText,
shortPdfPath: $shortPdfPath,
longPdfPath: $longPdfPath,
youtubeUrl: $youtubeUrl,
parentElement: $parentElement,
));
}
/**
* @throws DomainException
*/
private function validateNoRootElementExists(DomainSet $set): void
{
$elements = $this->elementRepo->findBySet($set);
foreach ($elements as $element) {
if ($element->getParentElement() === null) {
throw new DomainException(
'A root element already exists for this set'
);
}
}
}
}