extract validation code to private method

This commit is contained in:
Yisroel Baum 2026-04-23 21:12:41 +03:00
parent 49663d70d9
commit 6b9ad5ef8e
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -2,6 +2,7 @@
namespace App\Plan\UseCases; namespace App\Plan\UseCases;
use App\User\User;
use App\Exceptions\BadRequestException; use App\Exceptions\BadRequestException;
use App\Node\NodeRepository; use App\Node\NodeRepository;
use App\Plan\CreatePlanDto; use App\Plan\CreatePlanDto;
@ -9,6 +10,7 @@ use App\Plan\Plan;
use App\Plan\PlanRepository; use App\Plan\PlanRepository;
use App\ScheduledNode\UseCases\CreateScheduledNode; use App\ScheduledNode\UseCases\CreateScheduledNode;
use App\ScheduledNode\UseCases\CreateScheduledNodeRequest; use App\ScheduledNode\UseCases\CreateScheduledNodeRequest;
use App\Text\Text;
use App\Text\TextRepository; use App\Text\TextRepository;
use App\User\UserRepository; use App\User\UserRepository;
use DateTimeImmutable; use DateTimeImmutable;
@ -16,6 +18,10 @@ use DomainException;
class CreatePlan class CreatePlan
{ {
private User $user;
private Text $text;
public function __construct( public function __construct(
private PlanRepository $planRepo, private PlanRepository $planRepo,
private UserRepository $userRepo, private UserRepository $userRepo,
@ -30,49 +36,16 @@ class CreatePlan
*/ */
public function execute(CreatePlanRequest $request): Plan public function execute(CreatePlanRequest $request): Plan
{ {
if ($request->userId === null) { $this->validateAndSetUp($request);
throw new BadRequestException('userId is required');
}
if ($request->textId === null) {
throw new BadRequestException('textId is required');
}
if ($request->name === null) {
throw new BadRequestException('name is required');
}
if ($request->dateStart === null) {
throw new BadRequestException('date start is required');
}
if ($request->dateEnd === null) {
throw new BadRequestException('date end is required');
}
$startDate = new DateTimeImmutable($request->dateStart); $startDate = new DateTimeImmutable($request->dateStart);
$endDate = new DateTimeImmutable($request->dateEnd); $endDate = new DateTimeImmutable($request->dateEnd);
if ($endDate < $startDate) {
throw new BadRequestException('date end cannot be before date start');
}
$userId = $request->userId;
$user = $this->userRepo->find($userId);
if ($user === null) {
throw new DomainException("User with id: $userId doesnt exist");
}
$textId = $request->textId;
$text = $this->textRepo->find($textId);
if ($text === null) {
throw new DomainException("Text with id: $textId doesnt exist");
}
$nodesOfText = $this->filterForNonParentNodes( $nodesOfText = $this->filterForNonParentNodes(
$this->nodeRepo->findByTextId($textId) $this->nodeRepo->findByTextId($this->text->getId())
); );
$plan = $this->planRepo->create(new CreatePlanDto( $plan = $this->planRepo->create(new CreatePlanDto(
name: $request->name, name: $request->name,
user: $user, user: $this->user,
)); ));
$dates = []; $dates = [];
@ -115,4 +88,46 @@ class CreatePlan
} }
return array_values($result); return array_values($result);
} }
private function validateAndSetUp(CreatePlanRequest $request): void
{
if ($request->userId === null) {
throw new BadRequestException('userId is required');
}
if ($request->textId === null) {
throw new BadRequestException('textId is required');
}
if ($request->name === null) {
throw new BadRequestException('name is required');
}
if ($request->dateStart === null) {
throw new BadRequestException('date start is required');
}
if ($request->dateEnd === null) {
throw new BadRequestException('date end is required');
}
$startDate = new DateTimeImmutable($request->dateStart);
$endDate = new DateTimeImmutable($request->dateEnd);
if ($endDate < $startDate) {
throw new BadRequestException('date end cannot be before date start');
}
$userId = $request->userId;
$user = $this->userRepo->find($userId);
if ($user === null) {
throw new DomainException("User with id: $userId doesnt exist");
}
$this->user = $user;
$textId = $request->textId;
$text = $this->textRepo->find($textId);
if ($text === null) {
throw new DomainException("Text with id: $textId doesnt exist");
}
$this->text = $text;
}
} }