/** * @param $gamertag * @return Account|void|static * @throws H5PlayerNotFoundException * @throws Helpers\Network\ThreeFourThreeOfflineException */ public function getAccountByGamertag($gamertag) { $url = sprintf(Constants::$servicerecord_arena, Text::encodeGamertagForApi($gamertag)); $account = $this->checkCacheForGamertag($gamertag); if ($account instanceof Account) { // lets check if they have H5 data if (!$account->h5 instanceof Data) { $h5_data = new Data(); $h5_data->account_id = $account->id; $h5_data->save(); } return $account; } $json = $this->getJson($url); if (isset($json['Results'][0]['ResultCode']) && $json['Results'][0]['ResultCode'] == 0) { try { return Account::firstOrCreate(['gamertag' => $json['Results'][0]['Id']]); } catch (QueryException $e) { throw new H5PlayerNotFoundException(); } } else { throw new H5PlayerNotFoundException(); } }
/** * @param $platform * @param $gamertag * @return \Onyx\Account * @throws Helpers\Network\BungieOfflineException * @throws PlayerNotFoundException */ public function fetchAccountByGamertag($platform, $gamertag) { $platform = intval($platform); $url = sprintf(Constants::$searchDestinyPlayer, $platform, $gamertag); $account = $this->checkCacheForGamertag($gamertag); // @todo check if destiny_membershipId is NULL, if so. Pull JSON and update fields if ($account instanceof Account) { return $account; } $json = $this->getJson($url); if (isset($json['Response'][0]['membershipId'])) { try { return Account::firstOrCreate(['gamertag' => $json['Response'][0]['displayName'], 'accountType' => $json['Response'][0]['membershipType'], 'destiny_membershipId' => $json['Response'][0]['membershipId']]); } catch (QueryException $e) { // Assuming this character already exists, but has had a name change $char = Character::where('membershipId', $json['Response'][0]['membershipId'])->first(); $account = $char->account; if ($account instanceof Account) { $account->gamertag = $json['Response'][0]['displayName']; $account->save(); return $account; } return Account::firstOrCreate(['gamertag' => $json['Response'][0]['displayName'], 'accountType' => $json['Response'][0]['membershipType'], 'destiny_membershipId' => $json['Response'][0]['membershipId']]); } } else { throw new PlayerNotFoundException(); } }