Goal-Calibration/app/Text/UseCases/CreateText.php

40 lines
881 B
PHP

<?php
namespace App\Text\UseCases;
use App\Exceptions\BadRequestException;
use App\Text\Text;
use App\Text\CreateTextDto;
use App\Text\TextRepository;
use App\Node\NodeRepository;
use App\Node\CreateNodeDto;
class CreateText
{
public function __construct(
private TextRepository $textRepo,
private NodeRepository $nodeRepo,
) {}
/**
* @throws BadRequestException
*/
public function execute(CreateTextRequest $request): Text
{
if ($request->name === null) {
throw new BadRequestException('name is required');
}
$text = $this->textRepo->create(new CreateTextDto(
name: $request->name,
));
$this->nodeRepo->create(new CreateNodeDto(
text: $text,
title: $text->getName(),
parentNode: null,
));
return $text;
}
}