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;
|
2016-07-17 20:55:24 +05:30
|
|
|
use common\components\RabbitMQ\Component as RabbitMQComponent;
|
2016-04-24 00:14:10 +05:30
|
|
|
use common\models\amqp\UsernameChanged;
|
|
|
|
use common\models\MojangUsername;
|
2016-04-12 03:16:44 +05:30
|
|
|
use console\controllers\base\AmqpController;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function getQueueName() {
|
2016-07-17 20:55:24 +05:30
|
|
|
return 'accounts-events';
|
2016-04-12 03:16:44 +05:30
|
|
|
}
|
|
|
|
|
2016-07-17 20:55:24 +05:30
|
|
|
protected function getExchangeDeclareArgs() {
|
2016-04-12 03:16:44 +05:30
|
|
|
return array_replace(parent::getExchangeDeclareArgs(), [
|
2016-07-17 20:55:24 +05:30
|
|
|
1 => RabbitMQComponent::TYPE_TOPIC, // type -> topic
|
2016-04-24 00:14:10 +05:30
|
|
|
3 => true, // durable -> true
|
2016-04-12 03:16:44 +05:30
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-07-17 20:55:24 +05:30
|
|
|
protected function getQueueBindArgs($exchangeName, $queueName) {
|
|
|
|
return [$queueName, $exchangeName, 'accounts.#'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoutesMap() {
|
|
|
|
return [
|
|
|
|
'accounts.username-changed' => 'routeUsernameChanged',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
public function routeUsernameChanged(UsernameChanged $body) {
|
|
|
|
$mojangApi = new MojangApi();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|