authorizeRequest->authorizeLeaguePresident($leagueId); $leagueEntity = $this->leagueRepository->find($leagueId); if (is_null($leagueEntity)) { throw new HttpException(Response::HTTP_NOT_FOUND, 'League not found'); } $seasonCount = $this->seasonRepository->count([ 'league' => $leagueId ]); $seasonDto = new SeasonDto(); $seasonDto->seasonNumber = $seasonCount + 1; $seasonDto->leagueId = $leagueEntity->getId(); $seasonDto->leagueName = $leagueEntity->getName(); $seasonDto->fillFromArray($request->toArray()); $seasonDto->validate(); $numberOfTeams = $request->toArray()['numberOfTeams']; $seasonEntity = $this->seasonFactory::create($seasonDto); $seasonEntity->setLeague($leagueEntity); if (!empty($numberOfTeams)) { $this->createTeams($numberOfTeams, $seasonEntity); } $this->entityManager->persist($seasonEntity); $this->entityManager->flush(); $seasonDto->id = $seasonEntity->getId(); return new JsonResponse( data: [ 'success' => true, 'season' => $seasonDto->createSeasonArray() ], status: Response::HTTP_OK ); } private function createTeams(int $numberOfTeams, Season $seasonEntity): void { if (!empty($numberOfTeams)) { for ($i = 0; $i < $numberOfTeams; $i++) { $teamEntity = new Team(); $teamEntity->addSeason($seasonEntity); $teamEntity->setName('Equipo '. $i+1); $this->entityManager->persist($teamEntity); } } } }