44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Node\UseCases;
|
|
|
|
use App\Node\Node;
|
|
use App\Node\CreateNodeDto;
|
|
use App\Node\NodeRepository;
|
|
use App\Text\TextRepository;
|
|
use DomainException;
|
|
|
|
class CreateNode
|
|
{
|
|
public function __construct(
|
|
private NodeRepository $nodeRepo,
|
|
private TextRepository $textRepo,
|
|
) {}
|
|
|
|
public function execute(CreateNodeRequest $request): Node
|
|
{
|
|
$textId = $request->textId;
|
|
$text = $this->textRepo->find($textId);
|
|
if ($text === null) {
|
|
throw new DomainException("Text with id: $textId doesnt exist");
|
|
}
|
|
if ($request->parentNodeId === null) {
|
|
return $this->nodeRepo->create(new CreateNodeDto(
|
|
text: $text,
|
|
title: $request->title,
|
|
parentNode: null,
|
|
));
|
|
}
|
|
$parentNodeId = $request->parentNodeId;
|
|
$parentNode = $this->nodeRepo->find($parentNodeId);
|
|
if ($parentNode === null) {
|
|
throw new DomainException("Node with id: $parentNodeId doesnt exist");
|
|
}
|
|
|
|
return $this->nodeRepo->create(new CreateNodeDto(
|
|
text: $text,
|
|
title: $request->title,
|
|
parentNode: $parentNode,
|
|
));
|
|
}
|
|
}
|