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'); $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); $decoded = json_decode(base64_decode($value), true);
$this->assertArrayHasKey('timestamp', $decoded); $this->assertArrayHasKey('timestamp', $decoded);
$this->assertArrayHasKey('textures', $decoded); $this->assertArrayHasKey('textures', $decoded);
@ -61,7 +61,6 @@ class SessionServerSteps extends FunctionalTester {
$this->assertArrayHasKey('SKIN', $textures); $this->assertArrayHasKey('SKIN', $textures);
$skinTextures = $textures['SKIN']; $skinTextures = $textures['SKIN'];
$this->assertArrayHasKey('url', $skinTextures); $this->assertArrayHasKey('url', $skinTextures);
$this->assertArrayHasKey('hash', $skinTextures);
} }
} }

View File

@ -1,34 +1,48 @@
<?php <?php
declare(strict_types=1);
namespace common\components\SkinSystem; namespace common\components\SkinSystem;
use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\ClientInterface;
use Yii; use Yii;
// TODO: convert to complete Chrly client library
class Api { class Api {
private const BASE_DOMAIN = 'http://skinsystem.ely.by'; private const BASE_DOMAIN = 'http://skinsystem.ely.by';
/** @var ClientInterface */
private $client;
/** /**
* @param string $username * @param string $username
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
* *
* @return array * @return array
*/ */
public function textures(string $username): array { public function textures(string $username): ?array {
$response = $this->getClient()->get($this->getBuildUrl('/textures/' . $username)); $response = $this->getClient()->request('GET', $this->buildUrl('/textures/' . $username));
if ($response->getStatusCode() !== 204) {
return null;
}
return json_decode($response->getBody(), true); 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 self::BASE_DOMAIN . $url;
} }
/** private function getClient(): ClientInterface {
* @return GuzzleClient if ($this->client === null) {
*/ $this->client = Yii::$container->get(ClientInterface::class);
protected function getClient(): GuzzleClient { }
return Yii::$app->guzzle;
return $this->client;
} }
} }

View File

@ -19,6 +19,7 @@ return [
'definitions' => [ 'definitions' => [
GuzzleHttp\ClientInterface::class => GuzzleHttp\Client::class, GuzzleHttp\ClientInterface::class => GuzzleHttp\Client::class,
Ely\Mojang\Api::class => Ely\Mojang\Api::class, Ely\Mojang\Api::class => Ely\Mojang\Api::class,
common\components\SkinSystem\Api::class => common\components\SkinSystem\Api::class,
], ],
], ],
'components' => [ 'components' => [

View File

@ -1,9 +1,12 @@
<?php <?php
declare(strict_types=1);
namespace common\models; namespace common\models;
use common\components\SkinSystem\Api as SkinSystemApi; use common\components\SkinSystem\Api as SkinSystemApi;
use DateInterval; use DateInterval;
use DateTime; use DateTime;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Yii; use Yii;
@ -20,7 +23,7 @@ class Textures {
$this->account = $account; $this->account = $account;
} }
public function getMinecraftResponse() { public function getMinecraftResponse(): array {
$response = [ $response = [
'name' => $this->account->username, 'name' => $this->account->username,
'id' => str_replace('-', '', $this->account->uuid), 'id' => str_replace('-', '', $this->account->uuid),
@ -60,22 +63,20 @@ class Textures {
} }
public function getTextures(): array { public function getTextures(): array {
/** @var SkinSystemApi $api */
$api = Yii::$container->get(SkinSystemApi::class);
try { try {
$textures = $this->getSkinsystemApi()->textures($this->account->username); $textures = $api->textures($this->account->username);
} catch (RequestException $e) { } catch (RequestException $e) {
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage()); Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage());
$textures = [ } catch (GuzzleException $e) {
'SKIN' => [ Yii::error($e);
'url' => 'http://skins.minecraft.net/MinecraftSkins/' . $this->account->username . '.png',
'hash' => md5(uniqid('random, please', true)),
],
];
} }
return $textures; return $textures ?? [];
} }
public static function encrypt(array $data) { public static function encrypt(array $data): string {
return base64_encode(stripcslashes(json_encode($data))); return base64_encode(stripcslashes(json_encode($data)));
} }
@ -83,8 +84,4 @@ class Textures {
return json_decode(base64_decode($string), $assoc); return json_decode(base64_decode($string), $assoc);
} }
protected function getSkinsystemApi(): SkinSystemApi {
return new SkinSystemApi();
}
} }