40 lines
924 B
PHP
40 lines
924 B
PHP
<?php
|
|
|
|
namespace App\Set\UseCases\CreateSet;
|
|
|
|
use App\Element\CreateElementDto;
|
|
use App\Element\ElementRepository;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Set\CreateSetDto;
|
|
use App\Set\Set;
|
|
use App\Set\SetRepository;
|
|
|
|
class CreateSet
|
|
{
|
|
public function __construct(
|
|
private SetRepository $setRepo,
|
|
private ElementRepository $elementRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
*/
|
|
public function execute(CreateSetRequest $request): Set
|
|
{
|
|
if ($request->name === null || $request->name === '') {
|
|
throw new BadRequestException('name is required');
|
|
}
|
|
|
|
$set = $this->setRepo->create(new CreateSetDto(
|
|
name: $request->name,
|
|
));
|
|
|
|
$this->elementRepo->create(new CreateElementDto(
|
|
set: $set,
|
|
title: $set->getName(),
|
|
parentElement: null,
|
|
));
|
|
|
|
return $set;
|
|
}
|
|
}
|