From 194a7acd2a8e032a1adc13b2180b1cd047155c44 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 13 May 2019 19:39:11 +0300 Subject: [PATCH] Fixes ACCOUNTS-5FF. Handle 204 response from Chrly. --- api/config/config-test.php | 11 +++++++ .../functional/_steps/SessionServerSteps.php | 3 +- common/components/SkinSystem/Api.php | 32 +++++++++++++------ common/config/config.php | 1 + common/models/Textures.php | 25 +++++++-------- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/api/config/config-test.php b/api/config/config-test.php index 7e5ff0c..02ad46e 100644 --- a/api/config/config-test.php +++ b/api/config/config-test.php @@ -21,6 +21,17 @@ return [ } }; }, + common\components\SkinSystem\Api::class => function() { + return new class extends common\components\SkinSystem\Api { + public function textures(string $username): ?array { + return [ + 'SKIN' => [ + 'url' => 'http://localhost/skin.png', + ], + ]; + } + }; + }, ], ], ]; diff --git a/api/tests/functional/_steps/SessionServerSteps.php b/api/tests/functional/_steps/SessionServerSteps.php index 65bd9c9..f33e2c8 100644 --- a/api/tests/functional/_steps/SessionServerSteps.php +++ b/api/tests/functional/_steps/SessionServerSteps.php @@ -50,7 +50,7 @@ class SessionServerSteps extends FunctionalTester { ], ]); $this->canSeeResponseJsonMatchesJsonPath('$.properties[0].value'); - $value = json_decode($this->grabResponse(), true)['properties'][0]['value']; + $value = $this->grabDataFromResponseByJsonPath('$.properties[0].value')[0]; $decoded = json_decode(base64_decode($value), true); $this->assertArrayHasKey('timestamp', $decoded); $this->assertArrayHasKey('textures', $decoded); @@ -61,7 +61,6 @@ class SessionServerSteps extends FunctionalTester { $this->assertArrayHasKey('SKIN', $textures); $skinTextures = $textures['SKIN']; $this->assertArrayHasKey('url', $skinTextures); - $this->assertArrayHasKey('hash', $skinTextures); } } diff --git a/common/components/SkinSystem/Api.php b/common/components/SkinSystem/Api.php index 4854354..5df933e 100644 --- a/common/components/SkinSystem/Api.php +++ b/common/components/SkinSystem/Api.php @@ -1,34 +1,48 @@ getClient()->get($this->getBuildUrl('/textures/' . $username)); + public function textures(string $username): ?array { + $response = $this->getClient()->request('GET', $this->buildUrl('/textures/' . $username)); + if ($response->getStatusCode() !== 204) { + return null; + } return json_decode($response->getBody(), true); } - protected function getBuildUrl(string $url): string { + public function setClient(ClientInterface $client): void { + $this->client = $client; + } + + private function buildUrl(string $url): string { return self::BASE_DOMAIN . $url; } - /** - * @return GuzzleClient - */ - protected function getClient(): GuzzleClient { - return Yii::$app->guzzle; + private function getClient(): ClientInterface { + if ($this->client === null) { + $this->client = Yii::$container->get(ClientInterface::class); + } + + return $this->client; } } diff --git a/common/config/config.php b/common/config/config.php index 6b1020c..5cd970d 100644 --- a/common/config/config.php +++ b/common/config/config.php @@ -19,6 +19,7 @@ return [ 'definitions' => [ GuzzleHttp\ClientInterface::class => GuzzleHttp\Client::class, Ely\Mojang\Api::class => Ely\Mojang\Api::class, + common\components\SkinSystem\Api::class => common\components\SkinSystem\Api::class, ], ], 'components' => [ diff --git a/common/models/Textures.php b/common/models/Textures.php index 6046fff..2077cfc 100644 --- a/common/models/Textures.php +++ b/common/models/Textures.php @@ -1,9 +1,12 @@ account = $account; } - public function getMinecraftResponse() { + public function getMinecraftResponse(): array { $response = [ 'name' => $this->account->username, 'id' => str_replace('-', '', $this->account->uuid), @@ -60,22 +63,20 @@ class Textures { } public function getTextures(): array { + /** @var SkinSystemApi $api */ + $api = Yii::$container->get(SkinSystemApi::class); try { - $textures = $this->getSkinsystemApi()->textures($this->account->username); + $textures = $api->textures($this->account->username); } catch (RequestException $e) { Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage()); - $textures = [ - 'SKIN' => [ - 'url' => 'http://skins.minecraft.net/MinecraftSkins/' . $this->account->username . '.png', - 'hash' => md5(uniqid('random, please', true)), - ], - ]; + } catch (GuzzleException $e) { + Yii::error($e); } - return $textures; + return $textures ?? []; } - public static function encrypt(array $data) { + public static function encrypt(array $data): string { return base64_encode(stripcslashes(json_encode($data))); } @@ -83,8 +84,4 @@ class Textures { return json_decode(base64_decode($string), $assoc); } - protected function getSkinsystemApi(): SkinSystemApi { - return new SkinSystemApi(); - } - }