2016-09-06 15:26:39 +05:30
|
|
|
<?php
|
2019-05-13 22:09:11 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-06 15:26:39 +05:30
|
|
|
namespace common\models;
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
use common\components\SkinsSystemApi;
|
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient;
|
2019-05-13 22:09:11 +05:30
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2016-12-23 04:20:34 +05:30
|
|
|
use Yii;
|
2016-09-06 15:26:39 +05:30
|
|
|
|
|
|
|
class Textures {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private const int MAX_RETRIES = 3;
|
2024-06-11 07:20:10 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(protected Account $account) {
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
public function getMinecraftResponse(bool $signed = false): array {
|
|
|
|
$uuid = str_replace('-', '', $this->account->uuid);
|
2024-06-11 07:20:10 +05:30
|
|
|
$profile = $this->getProfile($uuid, $signed);
|
|
|
|
if ($profile['id'] !== $uuid) {
|
2021-03-03 19:34:42 +05:30
|
|
|
// Also a case that shouldn't happen, but is technically possible
|
|
|
|
$profile['id'] = $uuid;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2021-03-03 19:34:42 +05:30
|
|
|
return $profile;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
2024-06-11 07:20:10 +05:30
|
|
|
/**
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
private function getProfile(string $uuid, bool $signed): array {
|
2021-03-03 19:34:42 +05:30
|
|
|
/** @var SkinsSystemApi $api */
|
|
|
|
$api = Yii::$container->get(SkinsSystemApi::class);
|
2019-11-09 20:16:22 +05:30
|
|
|
if (YII_ENV_PROD) {
|
2021-03-03 19:34:42 +05:30
|
|
|
$api->setClient(new GuzzleHttpClient([
|
2019-11-09 20:16:22 +05:30
|
|
|
'connect_timeout' => 2,
|
2021-03-03 19:34:42 +05:30
|
|
|
'read_timeout' => 7,
|
2019-11-09 20:16:22 +05:30
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2024-06-11 07:20:10 +05:30
|
|
|
// It will be better to handle retries with Guzzle middleware, but for speed I'll do it in place
|
|
|
|
$lastException = null;
|
|
|
|
for ($i = 0; $i < self::MAX_RETRIES; $i++) {
|
|
|
|
try {
|
|
|
|
return $api->profile($this->account->username, $signed, $uuid);
|
|
|
|
} catch (GuzzleException $e) {
|
|
|
|
$lastException = $e;
|
|
|
|
sleep(1);
|
|
|
|
}
|
2016-12-23 04:20:34 +05:30
|
|
|
}
|
|
|
|
|
2024-06-11 07:20:10 +05:30
|
|
|
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $lastException->getMessage());
|
|
|
|
|
|
|
|
throw $lastException;
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|