public function joinMatch(Match $match, User $user, NotificationService $notificationService) { if ($match->getPlayersCount() < $match->getType()->getPlayers()) { try { $player = new MatchUser(); $player->setUser($user); $player->setMatch($match); $match->addPlayer($player); $match->increasePlayersCount(); $this->em->persist($match); $this->em->persist($player); $this->em->flush(); $this->session->getFlashBag()->add('success', 'Sėkmingai prisijungėte prie mačo!'); $full = false; if ($match->getPlayersCount() == $match->getType()->getPlayers()) { $full = true; } $notificationService->matchJoinNotification($match->getId(), $user->getId(), $full); } catch (UniqueConstraintViolationException $ex) { $this->session->getFlashBag()->add('error', 'Jūs jau esate prisijungę prie šio mačo'); } } else { $this->session->getFlashBag()->add('error', 'Prie mačo prisijungti negalima. Surinktas reikiamas žaidėjų skaičiu.'); return false; } return true; }