67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Node\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Node\CreateNodeDto;
|
|
use App\Node\Node;
|
|
use App\Node\NodeRepository;
|
|
use App\Text\TextRepository;
|
|
use DomainException;
|
|
|
|
class BulkCreateNodes
|
|
{
|
|
public function __construct(
|
|
private NodeRepository $nodeRepo,
|
|
private TextRepository $textRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @return Node[]
|
|
* @throws BadRequestException
|
|
* @throws DomainException
|
|
*/
|
|
public function execute(BulkCreateNodesRequest $request): array
|
|
{
|
|
if ($request->textId === null) {
|
|
throw new BadRequestException('textId is required');
|
|
}
|
|
|
|
if ($request->parentNodeId === null) {
|
|
throw new BadRequestException('parentNodeId is required');
|
|
}
|
|
|
|
if ($request->titlePrefix === null) {
|
|
throw new BadRequestException('titlePrefix is required');
|
|
}
|
|
|
|
if ($request->count === null) {
|
|
throw new BadRequestException('count is required');
|
|
}
|
|
|
|
if ($request->count < 1) {
|
|
throw new BadRequestException('count must be at least 1');
|
|
}
|
|
|
|
$text = $this->textRepo->find($request->textId);
|
|
if ($text === null) {
|
|
throw new DomainException("Text with id: {$request->textId} doesnt exist");
|
|
}
|
|
|
|
$parentNode = $this->nodeRepo->find($request->parentNodeId);
|
|
if ($parentNode === null) {
|
|
throw new DomainException("Node with id: {$request->parentNodeId} doesnt exist");
|
|
}
|
|
|
|
$created = [];
|
|
for ($i = 1; $i <= $request->count; $i++) {
|
|
$created[] = $this->nodeRepo->create(new CreateNodeDto(
|
|
text: $text,
|
|
title: "{$request->titlePrefix} {$i}",
|
|
parentNode: $parentNode,
|
|
));
|
|
}
|
|
|
|
return $created;
|
|
}
|
|
}
|