/** * Add round * * @param Round $round * @return Phase */ public function addRound(Round $round) { $this->rounds[] = $round; $round->setPhase($this); return $this; }
/** * Return a Game object pointing at the next available ground in the next available round * it may create a new Round if all rounds are complete * * @param Phase $phase * @param Collection $eventGrounds * @return Game */ private function nextGameSlot(Phase $phase, Collection $eventGrounds) { // Get the first incomplete round $newGame = new Game(); $roundNumber = 0; foreach ($phase->getRounds() as $existingRound) { $roundNumber = $existingRound->getNumber(); $takenGrounds = new ArrayCollection(); foreach ($existingRound->getGames() as $existingGame) { $takenGrounds->add($existingGame->getGround()); } // Search for a free ground foreach ($eventGrounds as $eventGround) { if (!$takenGrounds->contains($eventGround)) { // We found a free ground in an incomplete round $existingRound->addGame($newGame); $newGame->setGround($eventGround); return $newGame; } } } // If no free ground in incomplete round, create a new round $round = new Round(); $round->setNumber($roundNumber + 1); $round->addGame($newGame); $phase->addRound($round); $newGame->setGround($eventGrounds[0]); return $newGame; }