예제 #1
0
 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();
 }