From d8c5bfe4a7260dbcbd2b4020e4b00cb5746cd583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C3=ADbal=20=C3=81lvarez?= Date: Sun, 26 Sep 2021 05:33:28 -0400 Subject: [PATCH] PaladinsAPI class updated --- src/Exceptions/NotFoundException.php | 8 +- src/Exceptions/PaladinsException.php | 8 +- src/Exceptions/SessionException.php | 6 +- src/PaladinsAPI.php | 657 ++++++++++++++++++++------- 4 files changed, 512 insertions(+), 167 deletions(-) diff --git a/src/Exceptions/NotFoundException.php b/src/Exceptions/NotFoundException.php index 5677c8b..b3b257d 100644 --- a/src/Exceptions/NotFoundException.php +++ b/src/Exceptions/NotFoundException.php @@ -1,11 +1,13 @@ -code}]: {$this->message}\n"; + return __CLASS__ . ": [$this->code]: $this->message\n"; } } \ No newline at end of file diff --git a/src/Exceptions/PaladinsException.php b/src/Exceptions/PaladinsException.php index 849940f..04a8bf5 100644 --- a/src/Exceptions/PaladinsException.php +++ b/src/Exceptions/PaladinsException.php @@ -1,11 +1,13 @@ -code}]: {$this->message}\n"; + return __CLASS__ . ": [$this->code]: $this->message\n"; } } \ No newline at end of file diff --git a/src/Exceptions/SessionException.php b/src/Exceptions/SessionException.php index fbcb904..cef5b80 100644 --- a/src/Exceptions/SessionException.php +++ b/src/Exceptions/SessionException.php @@ -2,10 +2,12 @@ namespace PaladinsDev\PHP\Exceptions; +use Exception; + /** * @codeCoverageIgnore */ -class SessionException extends \Exception +class SessionException extends Exception { public function __construct($message, $code = 0, Exception $previous = null) { @@ -14,6 +16,6 @@ public function __construct($message, $code = 0, Exception $previous = null) public function __toString() { - return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; + return __CLASS__ . ": [$this->code]: $this->message\n"; } } \ No newline at end of file diff --git a/src/PaladinsAPI.php b/src/PaladinsAPI.php index 8812bf5..d5a88ed 100644 --- a/src/PaladinsAPI.php +++ b/src/PaladinsAPI.php @@ -13,14 +13,26 @@ /** * Paladins API - * + * * This class is the entry point and the main class that you will use to interact with the Hi-Rez/Evil Mojo API. - * + * * @author Matthew Hatcher * @copyright 2018 Halfpetal LLC * @license Apache-2.0 * @link https://github.com/PaladinsDev/PHP-API */ + +/** + * PHP 7.1 compatibility + * Additional API methods up to september 2021 + * Updated README file for Laravel 8 singletons compatibility + service providers + * Added try/catchs for exceptions handling + * Optimized buildUrl() method + * New appendVars() method + * Authored by another contributor + * @author Aníbal Álvarez + */ + class PaladinsAPI { /** @@ -29,7 +41,7 @@ class PaladinsAPI * @var string */ private $devId; - + /** * The auth key given to authorize the requests to the server. * @@ -46,7 +58,7 @@ class PaladinsAPI /** * The API endpoint, never changed. - * + * * @var string */ private $apiUrl; @@ -88,11 +100,11 @@ class PaladinsAPI public function __construct(string $devId, string $authKey, Cache $cacheDriver = null) { - $this->devId = $devId; - $this->authKey = $authKey; - $this->cache = $cacheDriver; - $this->languageId = 1; - $this->apiUrl = 'https://api.paladins.com/paladinsapi.svc'; + $this->devId = $devId; + $this->authKey = $authKey; + $this->cache = $cacheDriver; + $this->languageId = 1; + $this->apiUrl = 'https://api.paladins.com/paladinsapi.svc'; $this->guzzleClient = new Client; } @@ -104,8 +116,11 @@ public function __construct(string $devId, string $authKey, Cache $cacheDriver = * @param Cache|null $cacheDriver * @return PaladinsAPI */ - public static function getInstance(string $devId = null, string $authKey = null, Cache $cacheDriver = null): PaladinsAPI - { + public static function getInstance( + string $devId = null, + string $authKey = null, + Cache $cacheDriver = null + ): PaladinsAPI { if (!isset(self::$instance)) { self::$instance = new self($devId, $authKey, $cacheDriver); } @@ -113,83 +128,138 @@ public static function getInstance(string $devId = null, string $authKey = null, return self::$instance; } + // APIs - Connectivity, Development, & System Status + /** * Get the current Hi-Rez Paladins server status. * * @return mixed * @codeCoverageIgnore */ - public function getServerStatus() + public function ping() { - return $this->makeRequest($this->buildUrl('gethirezserverstatus')); + try { + return $this->makeRequest($this->buildUrl('ping', false)); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** - * Get the currnet patch information. + * Get the current session id, or set it if it's not set. + * + * @return string * - * @return mixed * @codeCoverageIgnore + * @throws SessionException|GuzzleException */ - public function getPatchInfo() + private function getSession(): string { - return $this->makeRequest($this->buildUrl('getpatchinfo')); + if (isset($this->cache)) { + $cacheId = 'paladinsdev.php-api.sessionId'; + + if (!$this->cache->contains($cacheId) || $this->cache->fetch($cacheId) == null) { + try { + $response = $this->guzzleClient->get("$this->apiUrl/createsessionJson/$this->devId/{$this->getSignature('createsession')}/{$this->getTimestamp()}"); + $body = json_decode($response->getBody(), true); + + if ($body['ret_msg'] != 'Approved' || !isset($body['session_id'])) { + throw new SessionException($body['ret_msg']); + } else { + $this->cache->save($cacheId, $body['session_id'], Carbon::now()->addMinutes(12)); + + return $this->cache->fetch($cacheId); + } + } catch (Exception $e) { + throw new SessionException($e->getMessage()); + } + } else { + return $this->cache->fetch($cacheId); + } + } else { + if (Carbon::now()->greaterThan($this->sessionExpiresAt) || !isset($this->sessionExpiresAt)) { + try { + $response = $this->guzzleClient->get("$this->apiUrl/createsessionJson/$this->devId/{$this->getSignature('createsession')}/{$this->getTimestamp()}"); + $body = json_decode($response->getBody(), true); + + if ($body['ret_msg'] != 'Approved' || !isset($body['session_id'])) { + throw new SessionException($body['ret_msg']); + } else { + $this->sessionId = $body['session_id']; + $this->sessionExpiresAt = Carbon::now()->addMinutes(12); + + return $this->sessionId; + } + } catch (Exception $e) { + throw new SessionException($e->getMessage()); + } + } else { + return $this->sessionId; + } + } } /** - * Get the top 50 most watched/recent matches. + * Get the current Hi-Rez Paladins server status. * * @return mixed * @codeCoverageIgnore */ - public function getTopMatches() + public function testSession() { - return $this->makeRequest($this->buildUrl('gettopmatches')); + try { + return $this->makeRequest($this->buildUrl('testsession')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** - * Get the ranked leaderboard for a tier and a season + * Show the current usage and usage limits for the API. * - * @param integer $tier - * @param integer $season - * @param integer $queue * @return mixed - * @codeCoverageIgnore - * @throws SessionException */ - public function getRankedLeaderboard(int $tier, int $season, int $queue) + public function getDataUsage() { - $url = $this->buildUrl('getleagueleaderboard', null, null, null, null, $queue, $tier, $season); - - return $this->makeRequest($url); + try { + return $this->makeRequest($this->buildUrl('getdataused')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** - * Get all the seasons and their state for ranked. + * Get the current Hi-Rez Paladins server status. * - * @param integer $queue * @return mixed * @codeCoverageIgnore */ - public function getRankedSeasons(int $queue) + public function getServerStatus() { - return $this->makeRequest($this->buildUrl('getleagueseasons', null, null, null, null, $queue)); + try { + return $this->makeRequest($this->buildUrl('gethirezserverstatus')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** - * Get all the match ids in a selected queue based on date and hours + * Get the currnet patch information. * - * @param string $hour - * @param mixed $date - * @param integer $queue * @return mixed - * * @codeCoverageIgnore */ - public function getMatchIdsByQueue(string $hour, $date, int $queue = 424) + public function getPatchInfo() { - return $this->makeRequest("$this->apiUrl/getmatchidsbyqueueJson/$this->devId/{$this->getSignature('getmatchidsbyqueue')}/{$this->getSession()}/{$this->getTimestamp()}/$queue/$date/$hour"); + try { + return $this->makeRequest($this->buildUrl('getpatchinfo')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } + // APIs - Champions & Items + /** * Get all the champions for the game. * @@ -197,7 +267,11 @@ public function getMatchIdsByQueue(string $hour, $date, int $queue = 424) */ public function getChampions() { - return $this->makeRequest($this->buildUrl('getchampions', null, $this->languageId)); + try { + return $this->makeRequest($this->buildUrl('getchampions', true, [$this->languageId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -208,7 +282,31 @@ public function getChampions() */ public function getChampionCards(int $championId) { - return $this->makeRequest($this->buildUrl('getchampioncards', null, $this->languageId, null, $championId)); + try { + return $this->makeRequest($this->buildUrl('getchampioncards', true, [$championId, $this->languageId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get the top players from the leaderboard. + * This should not reflect actual good players + * as it only requires more than 10 matches. + * It also is based on win + * + * @param integer $championId + * @param integer $queue + * @return mixed + * @codeCoverageIgnore + */ + public function getChampionLeaderboard(int $championId, int $queue) + { + try { + return $this->makeRequest($this->buildUrl('getchampionleaderboard', true, [$championId, $queue])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -219,23 +317,27 @@ public function getChampionCards(int $championId) */ public function getChampionSkins(int $championId) { - return $this->makeRequest($this->buildUrl('getchampionskins', null, $this->languageId, null, $championId)); + try { + return $this->makeRequest($this->buildUrl('getchampionskins', true, [$championId, $this->languageId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** - * Get the top players from the leaderboard. - * This should not reflect actual good players - * as it only requires more than 10 matches. - * It also is based on win + * Get all the available skins for the requested champion. * * @param integer $championId - * @param integer $queue * @return mixed - * @codeCoverageIgnore */ - public function getChampionLeaderboard(int $championId, int $queue) + public function getChampionRecommendedItems(int $championId) { - return $this->makeRequest($this->buildUrl('getchampionleaderboard', null, null, null, $championId, $queue)); + try { + return $this->makeRequest($this->buildUrl('getchampionrecommendeditems', true, + [$championId, $this->languageId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -246,9 +348,30 @@ public function getChampionLeaderboard(int $championId, int $queue) */ public function getItems() { - return $this->makeRequest($this->buildUrl('getitems', null, $this->languageId)); + try { + return $this->makeRequest($this->buildUrl('getitems', true, [$this->languageId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } + /** + * Get all the available in game items. + * + * @return mixed + * @codeCoverageIgnore + */ + public function getBountyItems() + { + try { + return $this->makeRequest($this->buildUrl('getbountyitems')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + // APIs - Players & PlayerIds + /** * Get a player and their details from the API. * @@ -259,8 +382,7 @@ public function getItems() */ public function getPlayer($player, int $platform = 5) { - if (!is_string($player) && !is_int($player)) - { + if (!is_string($player) && !is_int($player)) { throw new PaladinsException('The player must be either a name, string, or a player id, integer.'); } @@ -268,7 +390,7 @@ public function getPlayer($player, int $platform = 5) $players = $this->getPlayerIdByName($player); if (!empty($players)) { - $firstPlayer = Arr::first($players, function($value) use ($platform) { + $firstPlayer = Arr::first($players, function ($value) use ($platform) { return $value['portal_id'] == $platform; }); } @@ -276,11 +398,30 @@ public function getPlayer($player, int $platform = 5) if (!isset($firstPlayer)) { throw new PaladinsException('The requested player could not be found in the Paladins system.'); } else { - $player = $firstPlayer['player_id']; + $playerId = $firstPlayer['player_id']; } } - return $this->makeRequest($this->buildUrl('getplayer', $player)); + try { + return $this->makeRequest($this->buildUrl('getplayer', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get the top 50 most watched/recent matches. + * + * @return mixed + * @codeCoverageIgnore + */ + public function getPlayerBatch($list) + { + try { + return $this->makeRequest($this->buildUrl('getplayerbatch', true, [$list])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -291,7 +432,11 @@ public function getPlayer($player, int $platform = 5) */ public function getPlayerIdByName(string $name) { - return $this->makeRequest($this->buildUrl('getplayeridbyname', $name)); + try { + return $this->makeRequest($this->buildUrl('getplayeridbyname', true, [$name])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -302,9 +447,13 @@ public function getPlayerIdByName(string $name) * @return mixed * @codeCoverageIgnore */ - public function getPlayerIdByPortalUserId(string $name, int $platform) + public function getPlayerIdByPortalUserId(string $name, int $platform = 5) { - return $this->makeRequest($this->buildUrl('getplayeridbyportaluserid', $name, null, null, null, null, null, null, $platform)); + try { + return $this->makeRequest($this->buildUrl('getplayeridbyportaluserid', true, [$name, $platform])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -315,9 +464,13 @@ public function getPlayerIdByPortalUserId(string $name, int $platform) * @return mixed * @codeCoverageIgnore */ - public function getPlayerIdsByGamertag(string $name, int $platform) + public function getPlayerIdsByGamertag(string $name, int $platform = 5) { - return $this->makeRequest($this->buildUrl('getplayeridsbygamertag', $name, null, null, null, null, null, null, $platform)); + try { + return $this->makeRequest($this->buildUrl('getplayeridsbygamertag', true, [$name, $platform])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -329,9 +482,15 @@ public function getPlayerIdsByGamertag(string $name, int $platform) */ public function getPlayerIdInfoForXboxAndSwitch(string $name) { - return $this->makeRequest($this->buildUrl('getplayeridinfoforxboxandswitch', $name)); + try { + return $this->makeRequest($this->buildUrl('getplayeridinfoforxboxandswitch', true, [$name])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } + // APIs - PlayerId Info + /** * Get all the friends for the requested player. * @@ -340,7 +499,11 @@ public function getPlayerIdInfoForXboxAndSwitch(string $name) */ public function getPlayerFriends(int $playerId) { - return $this->makeRequest($this->buildUrl('getfriends', $playerId)); + try { + return $this->makeRequest($this->buildUrl('getfriends', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -349,9 +512,28 @@ public function getPlayerFriends(int $playerId) * @param integer $playerId * @return mixed */ + public function getChampionRanks(int $playerId) + { + try { + return $this->makeRequest($this->buildUrl('getchampionranks', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get all the champion ranks for the requested player. (DEPRECATED) + * + * @param integer $playerId + * @return mixed + */ public function getPlayerChampionRanks(int $playerId) { - return $this->makeRequest($this->buildUrl('getchampionranks', $playerId)); + try { + return $this->makeRequest($this->buildUrl('getchampionranks', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -362,7 +544,26 @@ public function getPlayerChampionRanks(int $playerId) */ public function getPlayerLoadouts(int $playerId) { - return $this->makeRequest($this->buildUrl('getplayerloadouts', $playerId, $this->languageId)); + try { + return $this->makeRequest($this->buildUrl('getplayerloadouts', true, [$playerId, $this->languageId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get all the champion loadouts for the requested player. + * + * @param integer $playerId + * @return mixed + */ + public function getPlayerAchievements(int $playerId) + { + try { + return $this->makeRequest($this->buildUrl('getplayerachievements', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -373,7 +574,11 @@ public function getPlayerLoadouts(int $playerId) */ public function getPlayerStatus(int $playerId) { - return $this->makeRequest($this->buildUrl('getplayerstatus', $playerId)); + try { + return $this->makeRequest($this->buildUrl('getplayerstatus', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -384,7 +589,11 @@ public function getPlayerStatus(int $playerId) */ public function getPlayerMatchHistory(int $playerId) { - return $this->makeRequest($this->buildUrl('getmatchhistory', $playerId)); + try { + return $this->makeRequest($this->buildUrl('getmatchhistory', true, [$playerId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -397,19 +606,43 @@ public function getPlayerMatchHistory(int $playerId) */ public function getPlayerQueueStats(int $playerId, int $queue) { - return $this->makeRequest($this->buildUrl('getqueuestats', $playerId, null, null, null, $queue)); + try { + return $this->makeRequest($this->buildUrl('getqueuestats', true, [$playerId, $queue])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get the match history of the requested player. + * + * @param int $searchPlayer + * @return mixed + */ + public function searchPlayers(int $searchPlayer) + { + try { + return $this->makeRequest($this->buildUrl('searchplayers', true, [$searchPlayer])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } + // APIs - Match Info + /** - * Get the information for an ended match. + * Get the top 50 most watched/recent matches. * - * @param integer $matchId * @return mixed * @codeCoverageIgnore */ - public function getMatchModeDetails(int $matchId) + public function getDemoDetails(int $matchId) { - return $this->makeRequest($this->buildUrl('getmodedetails', $matchId)); + try { + return $this->makeRequest($this->buildUrl('getdemodetails', true, [$matchId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -421,7 +654,46 @@ public function getMatchModeDetails(int $matchId) */ public function getMatchDetails(int $matchId) { - return $this->makeRequest($this->buildUrl('getmatchdetails', null, null, $matchId)); + try { + return $this->makeRequest($this->buildUrl('getmatchdetails', true, [$matchId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get match details from an ended match. + * + * @param int $aMatchId + * @return mixed + * @codeCoverageIgnore + */ + public function getMatchDetailsBatch(int $aMatchId) + { + try { + return $this->makeRequest($this->buildUrl('getmatchdetails', true, [$aMatchId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get all the match ids in a selected queue based on date and hours + * + * @param string $hour + * @param mixed $date + * @param integer $queue + * @return mixed + * + * @codeCoverageIgnore + */ + public function getMatchIdsByQueue(int $queue = 424, string $date = '2021-01-01', string $hour = "1") + { + try { + return $this->makeRequest($this->buildUrl('getmatchidsbyqueue', true, [$queue, $date, $hour])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** @@ -433,97 +705,167 @@ public function getMatchDetails(int $matchId) */ public function getActiveMatchDetails(int $matchId) { - return $this->makeRequest($this->buildUrl('getmatchplayerdetails', null, null, $matchId)); + try { + return $this->makeRequest($this->buildUrl('getmatchplayerdetails', true, [$matchId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** - * Show the current usage and usage limits for the API. + * Get the top 50 most watched/recent matches. * * @return mixed + * @codeCoverageIgnore */ - public function getDataUsage() + public function getTopMatches() { - return $this->makeRequest($this->buildUrl('getdataused')); + try { + return $this->makeRequest($this->buildUrl('gettopmatches')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } + // APIs - Leagues, Seasons & Rounds + /** - * Get the current session id, or set it if it's not set. + * Get the ranked leaderboard for a tier and a season * - * @return string + * @param integer $tier + * @param integer $season + * @param integer $queue + * @return mixed + * @codeCoverageIgnore + */ + public function getRankedLeaderboard(int $queue = 400, int $tier = 3, int $season = 5) + { + try { + return $this->makeRequest($this->buildUrl('getleagueleaderboard', true, [$queue, $tier, $season])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get all the seasons and their state for ranked. * + * @param integer $queue + * @return mixed * @codeCoverageIgnore - * @throws SessionException|GuzzleException */ - private function getSession(): string + public function getRankedSeasons(int $queue = 400) { - if (isset($this->cache)) { - $cacheId = 'paladinsdev.php-api.sessionId'; - - if (!$this->cache->contains($cacheId) || $this->cache->fetch($cacheId) == null) { - try { - $response = $this->guzzleClient->get("$this->apiUrl/createsessionJson/$this->devId/{$this->getSignature('createsession')}/{$this->getTimestamp()}"); - $body = json_decode($response->getBody(), true); + try { + return $this->makeRequest($this->buildUrl('getleagueseasons', true, [$queue])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } - if ($body['ret_msg'] != 'Approved' || !isset($body['session_id'])) { - throw new SessionException($body['ret_msg']); - } else { - $this->cache->save($cacheId, $body['session_id'], Carbon::now()->addMinutes(12)); + // APIs - Team Info - return $this->cache->fetch($cacheId); - } - } catch (Exception $e) { - throw new SessionException($e->getMessage()); - } - } else { - return $this->cache->fetch($cacheId); - } - } else { - if (Carbon::now()->greaterThan($this->sessionExpiresAt) || !isset($this->sessionExpiresAt)) { - try { - $response = $this->guzzleClient->get("$this->apiUrl/createsessionJson/$this->devId/{$this->getSignature('createsession')}/{$this->getTimestamp()}"); - $body = json_decode($response->getBody(), true); + /** + * Get all the seasons and their state for ranked. + * + * @param int $clanId + * @return mixed + * @codeCoverageIgnore + */ + public function getTeamDetails(int $clanId) + { + try { + return $this->makeRequest($this->buildUrl('getteamdetails', true, [$clanId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } - if ($body['ret_msg'] != 'Approved' || !isset($body['session_id'])) { - throw new SessionException($body['ret_msg']); - } else { - $this->sessionId = $body['session_id']; - $this->sessionExpiresAt = Carbon::now()->addMinutes(12); + /** + * Get all the seasons and their state for ranked. + * + * @param int $clanId + * @return mixed + * @codeCoverageIgnore + */ + public function getTeamPlayers(int $clanId) + { + try { + return $this->makeRequest($this->buildUrl('getteamplayers', true, [$clanId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } - return $this->sessionId; - } - } catch (Exception $e) { - throw new SessionException($e->getMessage()); - } - } else { - return $this->sessionId; - } + /** + * Get all the seasons and their state for ranked. + * + * @param int $searchTeam + * @return mixed + * @codeCoverageIgnore + */ + public function searchTeams(int $searchTeam) + { + try { + return $this->makeRequest($this->buildUrl('searchteams', true, [$searchTeam])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); } + } - // if (!Cache::has($cacheId) || Cache::get($cacheId) == null) { - // try { - // $response = $this->guzzleClient->get("$this->apiUrl/createsessionJson/$this->devId/{$this->getSignature('createsession')}/{$this->getTimestamp()}"); - // $body = json_decode($response->getBody(), true); + // APIs - Other - // if ($body['ret_msg'] != 'Approved' || !isset($body['session_id'])) { - // throw new PaladinsException($body['ret_msg']); - // } else { - // Cache::put($cacheId, $body['session_id'], Carbon::now()->addMinutes(12)); + /** + * Get the information for an ended match. (DEPRECATED) + * + * @param integer $matchId + * @return mixed + * @codeCoverageIgnore + */ + public function getMatchModeDetails(int $matchId) + { + try { + return $this->makeRequest($this->buildUrl('getmodedetails', true, [$matchId])); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } + + /** + * Get the information for an ended match. (DEPRECATED) + * + * @return mixed + * @codeCoverageIgnore + */ + public function getEsportsProLeagueDetails() + { + try { + return $this->makeRequest($this->buildUrl('getesportsproleaguedetails')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } + } - // return Cache::get($cacheId); - // } - // } catch (\Exception $e) { - // throw new PaladinsException($e->getMessage()); - // } - // } else { - // return Cache::get($cacheId); - // } + /** + * Get the information for an ended match. (DEPRECATED) + * + * @return mixed + * @codeCoverageIgnore + */ + public function getMotd() + { + try { + return $this->makeRequest($this->buildUrl('getmotd')); + } catch (GuzzleException | NotFoundException | PaladinsException | SessionException $e) { + die($e->getMessage()); + } } /** * Get the current timestamp in a simple format for API calls. * * @return string - * + * * @codeCoverageIgnore */ private function getTimestamp(): string @@ -536,7 +878,7 @@ private function getTimestamp(): string * * @param string $method * @return string - * + * * @codeCoverageIgnore */ private function getSignature(string $method): string @@ -548,35 +890,32 @@ private function getSignature(string $method): string * Build the proper URL for a variety of methods. * * @param string|null $method - * @param mixed $player - * @param int|null $lang - * @param int|null $match_id - * @param int|null $champ_id - * @param int|null $queue - * @param int|null $tier - * @param int|null $season - * @param int|null $platform + * @param bool $defaultVars + * @param array $vars * @return string * - * @throws SessionException|GuzzleException + * @throws GuzzleException + * @throws SessionException * @codeCoverageIgnore */ - private function buildUrl(string $method = null, $player = null, int $lang = null, int $match_id = null, int $champ_id = null, int $queue = null, int $tier = null, int $season = null, int $platform = null): string + private function buildUrl(string $method = null, bool $defaultVars = true, array $vars = []): string { - $baseUrl = $this->apiUrl . '/' . $method . 'Json/' . $this->devId . '/' . $this->getSignature($method) . '/' . $this->getSession() . '/' . $this->getTimestamp(); - - $platform ? ($baseUrl .= '/' . $platform) : null; - $player ? ($baseUrl .= '/' . $player) : null; - $champ_id ? ($baseUrl .= '/' . $champ_id) : null; - $lang ? ($baseUrl .= '/' . $lang) : null; - $match_id ? ($baseUrl .= '/' . $match_id) : null; - $queue ? ($baseUrl .= '/' . $queue) : null; - $tier ? ($baseUrl .= '/' . $tier) : null; - $season ? ($baseUrl .= '/' . $season) : null; - + $baseUrl = $this->apiUrl . '/' . $method . 'Json'; + $defaultVars ? ($baseUrl .= '/' . $this->devId . '/' . $this->getSignature($method) . '/' . $this->getSession() . '/' . $this->getTimestamp()) : null; + $baseUrl = $this->appendVars($baseUrl, $vars); return $baseUrl; } + private function appendVars($url, $vars) + { + foreach ($vars as $var) { + if ($var) { + $url .= '/' . $var; + } + } + return $url; + } + /** * Makes the request to the API and error checks it as well. *