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); $nodesOfText = $this->filterForNonParentNodes( $this->nodeRepo->findByTextId($textId) ); $plan = $this->planRepo->create(new CreatePlanDto( name: $request->name, user: $user, )); foreach ($nodesOfText as $node) { $this->createScheduledNode->execute( new CreateScheduledNodeRequest( date: new DateTimeImmutable(), planId: $plan->getId(), ) ); } return $plan; } /** * @param Node[] $nodes * @return Node[] */ private function filterForNonParentNodes(array $nodes): array { $result = []; foreach ($nodes as $node) { $result[$node->getId()] = $node; $parentNode = $node->getParentNode(); if ($parentNode !== null) { unset($result[$parentNode->getId()]); } } return array_values($result); } }