/**
  * @Route("user/challenge/accept/{slug}", name="challenge_accept")
  */
 public function challengeAcceptAction(Request $request, $slug)
 {
     $em = $this->getDoctrine()->getManager();
     $challenge = $em->getRepository('AppBundle:Challenge')->find($slug);
     $match = new Match();
     $match->setPlayer1($challenge->getPlayer1());
     $match->setPlayer2($challenge->getPlayer2());
     $match->setClub($challenge->getClub());
     $match->setDate($challenge->getDate());
     $match->setTime($challenge->getTime());
     if (!$challenge) {
         throw $this->createNotFoundException("No challenge found for id" . $slug);
     }
     $challenge->setStatus("Accepted");
     $em->persist($match);
     $em->flush();
     $userId = $this->getUser()->getId();
     $challenges = $this->getDoctrine()->getRepository('AppBundle:Challenge')->findUserChallenges($userId);
     return $this->render('challenge/challenge_list.html.twig', array('data' => $request, 'userId' => $userId, 'challenges' => $challenges));
 }
 public function getMatchData(Match $match)
 {
     try {
         $matchData = $this->whoscored->loadStatistics('match-centre2', array('id' => $match->getWsId()));
         $incidents = $this->whoscored->loadStatistics('live-player-stats', array('id' => $match->getWsId()));
     } catch (\Exception $e) {
         throw $e;
     }
     $stage = $match->getStage();
     $homeTeam = $match->getHomeTeam();
     $awayTeam = $match->getAwayTeam();
     //$this->getSquads($homeTeam);
     //$this->getSquads($awayTeam);
     $statBuilder = new StatBuilder();
     $statBuilder->prepareEvents($matchData->events);
     foreach ($incidents[0][1][0] as $event) {
         foreach ([1, 2] as $index) {
             foreach ($event[$index] as $incident) {
                 $incidentType = $incident[2];
                 $info = $incident[4] ? $incident[4] : '';
                 $minute = $incident[5];
                 $player = $this->getPlayer($incident[6]);
                 $runningScore = $incident[3] ? $incident[3] : '';
                 if ($incident[7]) {
                     $participatingPlayer = $this->getPlayer($incident[7]);
                 } else {
                     $participatingPlayer = null;
                 }
                 $matchIncident = new MatchEvent();
                 $matchIncident->setPlayer($player);
                 $matchIncident->setInfo($info);
                 $matchIncident->setEventType($incidentType);
                 $matchIncident->setMatch($match);
                 $matchIncident->setParticipatingPlayer($participatingPlayer);
                 $matchIncident->setSide($index);
                 $matchIncident->setMinute($minute);
                 $matchIncident->setRunningScore($runningScore);
                 $this->em->persist($matchIncident);
             }
         }
     }
     foreach (['home', 'away'] as $side) {
         $team = $side === 'home' ? $homeTeam : $awayTeam;
         $players = $matchData->{$side}->players;
         foreach ($players as $player) {
             if (($p = $this->getPlayer($player->playerId, $team)) == null) {
                 $p = new Player();
                 $p->setFirstName(null);
                 $p->setLastName(null);
                 $p->setKnownName($player->name);
                 $p->setAge($player->age);
                 $p->setWsId($player->playerId);
                 $p->setTeam($team);
                 $this->players[$player->playerId] = $p;
                 $this->em->persist($p);
             }
             $mps = new MatchPlayerStatistics();
             foreach ($statBuilder->filters as $filterIndex => $filter) {
                 $mps->{$filter->callback}($statBuilder->getFilterValue($filterIndex, $player->playerId));
             }
             $mps->setPlayer($p);
             $mps->setMatch($match);
             if (isset($player->isFirstEleven) && $player->isFirstEleven == true) {
                 $started = 1;
             } else {
                 $started = 0;
             }
             $mps->setGameStarted($started);
             $mps->setManOfTheMatch($player->isManOfTheMatch ? 1 : 0);
             $mps->setPositionText($player->position);
             $mps->setShirtNo(isset($player->shirtNo) ? $player->shirtNo : 0);
             $mps->setMinsPlayed($statBuilder->getMinutesPlayed($player->playerId, $started));
             if (isset($player->stats->ratings)) {
                 foreach ($player->stats->ratings as $rating) {
                     $mps->setRating($rating);
                 }
             } else {
                 $mps->setRating(0);
             }
             if ($p->getId() !== null) {
                 $sps = $this->em->getRepository('AppBundle:StagePlayerStatistics')->findOneBy(array('stage' => $stage, 'team' => $team, 'player' => $p));
             }
             if (!isset($sps)) {
                 $sps = new StagePlayerStatistics();
                 $sps->setPlayer($p);
                 $sps->setStage($stage);
                 $sps->setTeam($team);
             }
             $sps->addMatchStatistics($mps);
             $this->em->persist($sps);
             $this->em->persist($mps);
             unset($sps);
             unset($mps);
         }
     }
     $match->setStatus(1);
     $this->em->flush();
 }