Goal-Calibration/app/Node/UseCases/CreateNode.php

40 lines
1 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
{
$id = $request->textId;
$text = $this->textRepo->find($id);
if ($text === null) {
throw new DomainException("Text with id: $id doesnt exist");
}
if ($request->parentNodeId === null) {
return $this->nodeRepo->create(new CreateNodeDto(
text: $text,
title: $request->title,
parentNode: null,
));
}
$parentNode = $this->nodeRepo->find($request->parentNodeId);
return $this->nodeRepo->create(new CreateNodeDto(
text: $text,
title: $request->title,
parentNode: $parentNode,
));
}
}