diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 9841b33..b12fc50 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -2,6 +2,7 @@ namespace App\Plan\UseCases; +use App\User\User; use App\Exceptions\BadRequestException; use App\Node\NodeRepository; use App\Plan\CreatePlanDto; @@ -9,6 +10,7 @@ use App\Plan\Plan; use App\Plan\PlanRepository; use App\ScheduledNode\UseCases\CreateScheduledNode; use App\ScheduledNode\UseCases\CreateScheduledNodeRequest; +use App\Text\Text; use App\Text\TextRepository; use App\User\UserRepository; use DateTimeImmutable; @@ -16,6 +18,10 @@ use DomainException; class CreatePlan { + private User $user; + + private Text $text; + public function __construct( private PlanRepository $planRepo, private UserRepository $userRepo, @@ -30,49 +36,16 @@ 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->validateAndSetUp($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) { - 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( - $this->nodeRepo->findByTextId($textId) + $this->nodeRepo->findByTextId($this->text->getId()) ); $plan = $this->planRepo->create(new CreatePlanDto( name: $request->name, - user: $user, + user: $this->user, )); $dates = []; @@ -115,4 +88,46 @@ class CreatePlan } 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; + } }