Esempio n. 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()
 {
     try {
         $data = $this->getRawData(self::METHOD_URL_LEAGUE_LIST);
     } 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);
         }
         return self::EXIT_CODE_BAD_RAW_DATA;
     }
     print_r($data);
     die;
     $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;
 }
Esempio n. 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getLeague()
 {
     return $this->hasOne(League::className(), ['id' => 'league_id']);
 }