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();
 }
 public function addMatchStatistics(MatchPlayerStatistics $mps)
 {
     $this->setAccurateCrosses($this->getAccurateCrosses() + $mps->getAccurateCrosses());
     $this->setAccurateLongBalls($this->getAccurateLongBalls() + $mps->getAccurateLongBalls());
     $this->setAccuratePasses($this->getAccuratePasses() + $mps->getAccuratePasses());
     $this->setAccurateThroughBalls($this->getAccurateThroughBalls() + $mps->getAccurateThroughBalls());
     $this->setAerialLost($this->getAerialLost() + $mps->getAerialLost());
     $this->setAerialWon($this->getAerialWon() + $mps->getAerialWon());
     $this->setAssists($this->getAssists() + $mps->getAssists());
     $this->setBlockedShots($this->getBlockedShots() + $mps->getBlockedShots());
     $this->setClaims($this->getClaims() + $mps->getClaims());
     $this->setDispossessed($this->getDispossessed() + $mps->getDispossessed());
     $this->setDribblesLost($this->getDribblesLost() + $mps->getDribblesLost());
     $this->setDribblesWon($this->getDribblesWon() + $mps->getDribblesWon());
     $this->setEffectiveClearances($this->getEffectiveClearances() + $mps->getEffectiveClearances());
     $this->setErrorLeadGoal($this->getErrorLeadGoal() + $mps->getErrorLeadGoal());
     $this->setErrorLeadShot($this->getErrorLeadShot() + $mps->getErrorLeadShot());
     $this->setFouls($this->getFouls() + $mps->getFouls());
     $this->setGamesStarted($this->getGamesStarted() + $mps->getGameStarted());
     $this->setGoalsScored($this->getGoalsScored() + $mps->getGoalsScored());
     $this->setInterceptions($this->getInterceptions() + $mps->getInterceptions());
     $this->setKeyPasses($this->getKeyPasses() + $mps->getKeyPasses());
     $this->setManOfTheMatch($this->getManOfTheMatch() + $mps->getManOfTheMatch());
     $this->setMinsPlayed($this->getMinsPlayed() + $mps->getMinsPlayed());
     $this->setOffsides($this->getOffsides() + $mps->getOffsides());
     $this->setOffsidesProvoked($this->getOffsidesProvoked() + $mps->getOffsidesProvoked());
     $this->setOwnGoalsScored($this->getOwnGoalsScored() + $mps->getOwnGoalsScored());
     $this->setPunches($this->getPunches() + $mps->getPunches());
     $this->setRedCards($this->getRedCards() + $mps->getRedCards());
     $this->setSaves($this->getSaves() + $mps->getSaves());
     $this->setSecondYellowCards($this->getSecondYellowCards() + $mps->getSecondYellowCards());
     $this->setShotsBlocked($this->getShotsBlocked() + $mps->getShotsBlocked());
     $this->setShotsHitPost($this->getShotsHitPost() + $mps->getShotsHitPost());
     $this->setShotsOnTarget($this->getShotsOnTarget() + $mps->getShotsOnTarget());
     if ($mps->getSubstitutionType() == 1) {
         $this->setSubOff($this->getSubOff() + 1);
         $this->setSubOn($this->getSubOn() == null ? 0 : $this->getSubOn());
     } elseif ($mps->getSubstitutionType() == 2) {
         $this->setSubOn($this->getSubOn() + 1);
         $this->setSubOff($this->getSubOff() == null ? 0 : $this->getSubOff());
     } else {
         $this->setSubOff($this->getSubOff() == null ? 0 : $this->getSubOff());
         $this->setSubOn($this->getSubOn() == null ? 0 : $this->getSubOn());
     }
     $this->setTotalClearances($this->getTotalClearances() + $mps->getTotalClearances());
     $this->setTotalCrosses($this->getTotalCrosses() + $mps->getTotalCrosses());
     $this->setTotalLongBalls($this->getTotalLongBalls() + $mps->getTotalLongBalls());
     $this->setTotalPasses($this->getTotalPasses() + $mps->getTotalPasses());
     $this->setTotalShots($this->getTotalShots() + $mps->getTotalShots());
     $this->setTotalTackles($this->getTotalTackles() + $mps->getTotalTackles());
     $this->setTotalThroughBalls($this->getTotalThroughBalls() + $mps->getTotalThroughBalls());
     $this->setTouches($this->getTouches() + $mps->getTouches());
     $this->setTurnovers($this->getTurnovers() + $mps->getTurnovers());
     $this->setWasDribbled($this->getWasDribbled() + $mps->getWasDribbled());
     $this->setWasFouled($this->getWasFouled() + $mps->getWasFouled());
     $this->setYellowCards($this->getYellowCards() + $mps->getYellowCards());
 }