Fixes ACCOUNTS-5FF. Handle 204 response from Chrly.

This commit is contained in:
ErickSkrauch 2019-05-13 19:39:11 +03:00
parent 02a7d3815d
commit 194a7acd2a
5 changed files with 47 additions and 25 deletions

View File

@ -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',
],
];
}
};
},
],
],
];

View File

@ -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);
}
}

View File

@ -1,34 +1,48 @@
<?php
declare(strict_types=1);
namespace common\components\SkinSystem;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use Yii;
// TODO: convert to complete Chrly client library
class Api {
private const BASE_DOMAIN = 'http://skinsystem.ely.by';
/** @var ClientInterface */
private $client;
/**
* @param string $username
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return array
*/
public function textures(string $username): array {
$response = $this->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;
}
}

View File

@ -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' => [

View File

@ -1,9 +1,12 @@
<?php
declare(strict_types=1);
namespace common\models;
use common\components\SkinSystem\Api as SkinSystemApi;
use DateInterval;
use DateTime;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use Yii;
@ -20,7 +23,7 @@ class Textures {
$this->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();
}
}