Пример #1
0
 /**
  * Gets leagues list from STEAM API and loads them to database (in league table)
  *
  * @author Dzianis Kotau <*****@*****.**>
  * @return int Exit code
  */
 public function actionLeagueList()
 {
     echo '12312312';
     try {
         $data = $this->getRawData(self::METHOD_URL_LEAGUE_LIST);
         var_dump($this->quiet);
         //die;
         var_dump($data);
         die;
     } catch (Exception $e) {
         if ($this->quiet) {
             $this->stderr('An error occurred while fetching data from remote API method URL "' . self::METHOD_URL_LEAGUE_LIST . '" with error: "' . $e->getMessage() . '"' . PHP_EOL, Console::FG_RED);
         }
         var_dump($e->getMessage());
         die;
         return self::EXIT_CODE_BAD_RAW_DATA;
     }
     $data = $data['result']->{'leagues'};
     if (empty($data)) {
         if (!$this->quiet) {
             $this->stdout('Nothing to update.' . PHP_EOL, Console::FG_YELLOW);
         }
         return self::EXIT_CODE_NORMAL;
     }
     $data = array_map(function ($val) {
         $val->{'id'} = (int) $val->{'id'};
         $val->{'name'} = trim($val->{'name'});
         $val->{'description'} = trim($val->{'description'});
         $val->{'tournament_url'} = trim($val->{'tournament_url'});
         $val->{'itemdef'} = (int) $val->{'itemdef'};
         return $val;
     }, $data);
     //$leagues = League::find()->asArray()->all();
     $model = new League();
     $leagues = $model->getAllLeagueIds();
     $oldLeagueIds = [];
     foreach ($leagues as $row) {
         $oldLeagueIds[] = (int) $row['id'];
     }
     $newLeagueIds = [];
     foreach ($data as $row) {
         $newLeagueIds[] = (int) $row->{'leagueid'};
     }
     $model->completeLeague(array_diff($oldLeagueIds, $newLeagueIds));
     $insertLeagueIds = array_diff($newLeagueIds, $oldLeagueIds);
     $data = array_filter($data, function ($row) use($insertLeagueIds) {
         return in_array($row->{'leagueid'}, $insertLeagueIds);
     });
     $model->addLeagues($data);
     if ($this->verbose) {
         $this->stdout('All done.' . PHP_EOL, Console::FG_GREEN);
     }
     return self::EXIT_CODE_NORMAL;
 }