diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 9841b33..c732b28 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -30,33 +30,10 @@ class CreatePlan */ public function execute(CreatePlanRequest $request): Plan { - 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'); - } - + $this->validate($request); $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) { @@ -115,4 +92,34 @@ class CreatePlan } return array_values($result); } + + private function validate(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'); + } + } }