Skip to content

Commit

Permalink
[Laravel 10] Resolve current test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
nicekiwi committed Apr 27, 2024
1 parent 8d4d022 commit 932454e
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 81 deletions.
65 changes: 50 additions & 15 deletions src/Syntax/SteamApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Syntax\SteamApi\Exceptions\ApiCallFailedException;
use Syntax\SteamApi\Exceptions\ClassNotFoundException;
use Syntax\SteamApi\Exceptions\InvalidApiKeyException;
use Syntax\SteamApi\Exceptions\UnrecognizedId;
use Syntax\SteamApi\Steam\App;
use Syntax\SteamApi\Steam\Group;
use Syntax\SteamApi\Steam\Item;
Expand All @@ -32,31 +33,31 @@
* @method App app()
* @method Package package()
* @method Group group()
* @method Item item($appId)
* @method Item item()
*/
class Client
{
use SteamId;

public $validFormats = ['json', 'xml', 'vdf'];
public array $validFormats = ['json', 'xml', 'vdf'];

protected $url = 'http://api.steampowered.com/';
protected string $url = 'http://api.steampowered.com/';

protected $client;
protected GuzzleClient $client;

protected $interface;
protected ?string $interface;

protected $method;
protected string $method;

protected $version = 'v0002';
protected ?string $version = 'v0002';

protected $apiKey;
protected string $apiKey;

protected $apiFormat = 'json';
protected string $apiFormat = 'json';

protected $steamId;

protected $isService = false;
protected bool $isService = false;

/**
* @throws InvalidApiKeyException
Expand All @@ -72,7 +73,7 @@ public function __construct()
$this->setUpFormatted();
}

public function get()
public function get(): static
{
return $this;
}
Expand Down Expand Up @@ -199,7 +200,7 @@ protected function sendRequest(Request $request): stdClass

$result = new stdClass();
$result->code = $response->getStatusCode();
$result->body = json_decode((string) $response->getBody(true), null, 512, JSON_THROW_ON_ERROR);
$result->body = json_decode((string) $response->getBody(), null, 512, JSON_THROW_ON_ERROR);
} catch (ClientException $e) {
throw new ApiCallFailedException($e->getMessage(), $e->getResponse()->getStatusCode(), $e);
} catch (ServerException $e) {
Expand All @@ -208,14 +209,18 @@ protected function sendRequest(Request $request): stdClass
throw new ApiCallFailedException($e->getMessage(), $e->getCode(), $e);
}

if (empty((array)$result->body)) {
throw new ApiCallFailedException('Api call failed to complete due to an empty response.', $result->code);
}

// If all worked out, return the result
return $result;
}

private function buildUrl($version = false): string
{
// Set up the basic url
$url = $this->url . $this->interface . '/' . $this->method . '/';
$url = $this->url . ($this->interface ?? '') . '/' . $this->method . '/';

// If we have a version, add it
if ($version) {
Expand All @@ -227,6 +232,7 @@ private function buildUrl($version = false): string

/**
* @throws ClassNotFoundException
* @throws UnrecognizedId
*/
public function __call($name, $arguments)
{
Expand Down Expand Up @@ -288,7 +294,29 @@ protected function getServiceResponse($arguments)
$arguments = json_encode($arguments, JSON_THROW_ON_ERROR);

// Get the client
return $this->setUpService($arguments)->response;
$body = $this->setUpService($arguments);

if (!isset($body->response) || empty((array)$body->response)) {
throw new ApiCallFailedException('Api call failed to complete due to an empty response.', 500);
}

return $body->response;
}

/**
* @throws ApiCallFailedException
* @throws GuzzleException
*/
protected function getClientResponse($arguments)
{
// Get the client
$body = $this->setUpClient($arguments);

if (!isset($body->response) || empty((array)$body->response)) {
throw new ApiCallFailedException('Api call failed to complete due to an empty response.', 500);
}

return $body->response;
}

/**
Expand All @@ -310,10 +338,17 @@ protected function getApiKey(): string
return $apiKey;
}

/**
* @throws UnrecognizedId
*/
private function convertSteamIdTo64(): void
{
if (is_array($this->steamId)) {
array_walk($this->steamId, function (&$id) {
array_walk(
/**
* @throws UnrecognizedId
*/
$this->steamId, function (&$id) {
// Convert the id to all types and grab the 64 bit version
$id = $this->convertToAll($id)->id64;
});
Expand Down
10 changes: 6 additions & 4 deletions src/Syntax/SteamApi/Steam/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
use Illuminate\Support\Collection;
use Syntax\SteamApi\Containers\App as AppContainer;
use Syntax\SteamApi\Exceptions\ApiCallFailedException;
use Syntax\SteamApi\Exceptions\InvalidApiKeyException;

class App extends Client
{
/**
* @var bool
* @throws InvalidApiKeyException
*/

public function __construct()
{
parent::__construct();

$this->url = 'http://store.steampowered.com/';
$this->interface = 'api';
}
Expand All @@ -26,6 +28,8 @@ public function __construct()
* @param null $country
* @param null $language
* @return Collection
* @throws ApiCallFailedException
* @throws GuzzleException
*/
public function appDetails($appIds, $country = null, $language = null): Collection
{
Expand Down Expand Up @@ -68,9 +72,7 @@ protected function convertToObjects($apps): Collection
{
$convertedApps = $this->convertGames($apps);

$apps = $this->sortObjects($convertedApps);

return $apps;
return $this->sortObjects($convertedApps);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Syntax/SteamApi/Steam/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Item extends Client
public function __construct()
{
parent::__construct();

$this->url = 'http://store.steampowered.com/';
$this->isService = true;
$this->interface = 'api';
Expand Down
1 change: 1 addition & 0 deletions src/Syntax/SteamApi/Steam/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Package extends Client
public function __construct()
{
parent::__construct();

$this->url = 'http://store.steampowered.com/';
$this->interface = 'api';
}
Expand Down
7 changes: 3 additions & 4 deletions src/Syntax/SteamApi/Steam/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ public function GetRecentlyPlayedGames($count = null): ?Collection
* @throws ApiArgumentRequired
* @throws GuzzleException
* @throws \JsonException
* @deprecated - use ISteamUser.CheckAppOwnership
*/
public function IsPlayingSharedGame($appIdPlaying)
public function IsPlayingSharedGame($appIdPlaying): string
{
// Set up the api details
$this->setApiDetails(__FUNCTION__, 'v0001');
Expand All @@ -184,9 +185,7 @@ protected function convertToObjects($games): Collection
{
$convertedGames = $this->convertGames($games);

$games = $this->sortObjects($convertedGames);

return $games;
return $this->sortObjects($convertedGames);
}

private function convertGames($games): Collection
Expand Down
11 changes: 10 additions & 1 deletion src/Syntax/SteamApi/Steam/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InvalidArgumentException;
use Syntax\SteamApi\Client;
use Syntax\SteamApi\Containers\Player as PlayerContainer;
use Syntax\SteamApi\Exceptions\ApiArgumentRequired;
use Syntax\SteamApi\Exceptions\ApiCallFailedException;
use Syntax\SteamApi\Exceptions\UnrecognizedId;

Expand All @@ -30,7 +31,11 @@ public function __construct($steamId)
*
* @return mixed
*
* @throws ApiArgumentRequired
* @throws ApiCallFailedException
* @throws GuzzleException
* @throws UnrecognizedId
* @throws \JsonException
*/
public function ResolveVanityURL($displayName = null): mixed
{
Expand All @@ -43,7 +48,7 @@ public function ResolveVanityURL($displayName = null): mixed
$this->method = __FUNCTION__;
$this->version = 'v0001';

$results = $this->setUpClient(['vanityurl' => $displayName])->response;
$results = $this->getClientResponse(['vanityurl' => $displayName]);

// Return the full steam ID object for the display name.
return $results->message ?? $this->convertId($results->steamid);
Expand Down Expand Up @@ -74,8 +79,12 @@ public function GetPlayerSummaries(string $steamId = null): array
}

/**
* @param $chunk
* @return array
* @throws ApiCallFailedException
* @throws GuzzleException
* @throws \JsonException
* @throws ApiArgumentRequired
*/
private function getChunkedPlayerSummaries($chunk): array
{
Expand Down
31 changes: 19 additions & 12 deletions src/Syntax/SteamApi/SteamId.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ trait SteamId

private $rawValue;

private static $ID32 = 'id32';
private static string $ID32 = 'id32';

private static $ID64 = 'id64';
private static string $ID64 = 'id64';

private static $ID3 = 'id3';
private static string $ID3 = 'id3';

private static $id64Base = '76561197960265728';
private static string $id64Base = '76561197960265728';

/**
* @param string|int $id
* @param int|string $id
* @param string|null $format
*
* @return mixed
* @throws UnrecognizedId
*/
public function convertId($id, $format = null)
public function convertId(int|string $id, string $format = null)
{
$this->convertToAll($id);

Expand All @@ -46,14 +47,17 @@ public function convertId($id, $format = null)
}
}

protected function setUpFormatted()
protected function setUpFormatted(): void
{
$this->formatted = new \stdClass();
$this->formatted->{self::$ID32} = null;
$this->formatted->{self::$ID64} = null;
$this->formatted->{self::$ID3} = null;
}

/**
* @throws UnrecognizedId
*/
private function convertToAll($id)
{
[$type, $matches] = $this->determineIDType($id);
Expand All @@ -68,7 +72,7 @@ private function convertToAll($id)
return $this->formatted;
}

private function convertToID32()
private function convertToID32(): void
{
$z = bcdiv($this->rawValue, '2', 0);
$y = bcmul($z, '2', 0);
Expand All @@ -77,19 +81,22 @@ private function convertToID32()
$this->formatted->{self::$ID32} = $formatted;
}

private function convertToID64()
private function convertToID64(): void
{
$formatted = bcadd($this->rawValue, self::$id64Base, 0);
$this->formatted->{self::$ID64} = $formatted;
}

private function convertToID3()
private function convertToID3(): void
{
$formatted = "[U:1:$this->rawValue]";
$this->formatted->{self::$ID3} = $formatted;
}

private function determineIDType($id)
/**
* @throws UnrecognizedId
*/
private function determineIDType($id): array
{
$id = trim((string) $id);

Expand All @@ -113,7 +120,7 @@ private function determineIDType($id)
* @param $type
* @param $matches
*/
private function getRawValue($id, $type, $matches)
private function getRawValue($id, $type, $matches): void
{
switch ($type) {
case 'ID32':
Expand Down
Loading

0 comments on commit 932454e

Please sign in to comment.