2016-04-12 03:16:44 +05:30
|
|
|
<?php
|
|
|
|
namespace console\controllers;
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\components\Mojang\Api as MojangApi;
|
|
|
|
use common\components\Mojang\exceptions\NoContentException;
|
|
|
|
use common\models\amqp\UsernameChanged;
|
|
|
|
use common\models\MojangUsername;
|
2016-11-16 01:15:30 +05:30
|
|
|
use Ely\Amqp\Builder\Configurator;
|
2016-10-26 00:50:44 +05:30
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2016-04-12 03:16:44 +05:30
|
|
|
|
|
|
|
class AccountQueueController extends AmqpController {
|
|
|
|
|
|
|
|
public function getExchangeName() {
|
2016-07-17 20:55:24 +05:30
|
|
|
return 'events';
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|
|
|
|
|
2016-11-16 01:15:30 +05:30
|
|
|
public function configure(Configurator $configurator) {
|
|
|
|
$configurator->exchange->topic()->durable();
|
|
|
|
$configurator->queue->name('accounts-accounts-events')->durable();
|
|
|
|
$configurator->bind->routingKey('accounts.username-changed');
|
2016-07-17 20:55:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoutesMap() {
|
|
|
|
return [
|
|
|
|
'accounts.username-changed' => 'routeUsernameChanged',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
public function routeUsernameChanged(UsernameChanged $body) {
|
2016-10-31 05:17:18 +05:30
|
|
|
$mojangApi = $this->createMojangApi();
|
2016-04-24 00:14:10 +05:30
|
|
|
try {
|
|
|
|
$response = $mojangApi->usernameToUUID($body->newUsername);
|
|
|
|
} catch (NoContentException $e) {
|
|
|
|
$response = false;
|
2016-10-26 00:50:44 +05:30
|
|
|
} catch (RequestException $e) {
|
|
|
|
return true;
|
2016-04-24 00:14:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/** @var MojangUsername|null $mojangUsername */
|
|
|
|
$mojangUsername = MojangUsername::findOne($body->newUsername);
|
|
|
|
if ($response === false) {
|
|
|
|
if ($mojangUsername !== null) {
|
|
|
|
$mojangUsername->delete();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($mojangUsername === null) {
|
|
|
|
$mojangUsername = new MojangUsername();
|
|
|
|
$mojangUsername->username = $response->name;
|
|
|
|
$mojangUsername->uuid = $response->id;
|
|
|
|
} else {
|
|
|
|
$mojangUsername->uuid = $response->id;
|
|
|
|
$mojangUsername->touch('last_pulled_at');
|
|
|
|
}
|
|
|
|
|
|
|
|
$mojangUsername->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|
|
|
|
|
2016-10-31 05:17:18 +05:30
|
|
|
/**
|
|
|
|
* @return MojangApi
|
|
|
|
*/
|
|
|
|
protected function createMojangApi() : MojangApi {
|
|
|
|
return new MojangApi();
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|