setId === null) { throw new BadRequestException('setId is required'); } if ($request->title === null || $request->title === '') { throw new BadRequestException('title is required'); } $description = $request->description ?? ''; $richText = $request->richText ?? ''; $pdfPath = $request->pdfPath === '' ? null : $request->pdfPath; $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, richText: $richText, pdfPath: $pdfPath, 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, richText: $richText, pdfPath: $pdfPath, 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' ); } } } }