50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Element\UseCases\CreateChildElement;
|
|
|
|
use App\Element\CreateElementDto;
|
|
use App\Element\Element;
|
|
use App\Element\ElementRepository;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
|
|
class CreateChildElement
|
|
{
|
|
public function __construct(private ElementRepository $elementRepository)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws NotFoundException
|
|
*/
|
|
public function execute(CreateChildElementRequest $request): Element
|
|
{
|
|
if ($request->parentElementId === null) {
|
|
throw new BadRequestException('parentElementId is required');
|
|
}
|
|
|
|
if ($request->title === null || $request->title === '') {
|
|
throw new BadRequestException('title is required');
|
|
}
|
|
|
|
$parentElement = $this->elementRepository->find(
|
|
$request->parentElementId
|
|
);
|
|
if ($parentElement === null) {
|
|
throw new NotFoundException('Parent element not found');
|
|
}
|
|
|
|
return $this->elementRepository->create(new CreateElementDto(
|
|
set: $parentElement->getSet(),
|
|
title: $request->title,
|
|
description: '',
|
|
iconImageUrl: null,
|
|
richText: '',
|
|
shortPdfPath: null,
|
|
longPdfPath: null,
|
|
youtubeUrl: null,
|
|
parentElement: $parentElement,
|
|
));
|
|
}
|
|
}
|