Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2018-07-11 13:16:12 +00:00 committed by StyleCI Bot
parent 269ece309a
commit 4d63ec408c
2 changed files with 24 additions and 9 deletions

View File

@ -56,17 +56,17 @@ class CombinedAPI
$this->event_handler = $deserialized['event_handler']; $this->event_handler = $deserialized['event_handler'];
$this->event_handler_instance = $deserialized['event_handler_instance']; $this->event_handler_instance = $deserialized['event_handler_instance'];
} }
foreach ($paths as $path => $settings) { foreach ($paths as $path => $settings) {
$this->addInstance($path, $settings); $this->addInstance($path, $settings);
} }
} }
public function addInstance($path, $settings = []) public function addInstance($path, $settings = [])
{ {
if (isset($this->instances[$path]) && isset($this->instance_paths[$path])) return; if (isset($this->instances[$path]) && isset($this->instance_paths[$path])) {
return;
}
\danog\MadelineProto\Logger::constructor(3); \danog\MadelineProto\Logger::constructor(3);
\danog\MadelineProto\Logger::log("INSTANTIATING $path..."); \danog\MadelineProto\Logger::log("INSTANTIATING $path...");
@ -79,6 +79,7 @@ class CombinedAPI
$this->event_handler_instance->referenceInstance($path); $this->event_handler_instance->referenceInstance($path);
} }
} }
public function removeInstance($path) public function removeInstance($path)
{ {
if (isset($this->instance_paths[$path])) { if (isset($this->instance_paths[$path])) {
@ -91,8 +92,8 @@ class CombinedAPI
if (isset($this->event_handler_instance)) { if (isset($this->event_handler_instance)) {
$this->event_handler_instance->removeInstance($path); $this->event_handler_instance->removeInstance($path);
} }
} }
public function __destruct() public function __destruct()
{ {
if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread()) || Magic::is_fork()) { if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread()) || Magic::is_fork()) {
@ -134,6 +135,7 @@ class CombinedAPI
} }
$this->serialized = time(); $this->serialized = time();
return $wrote; return $wrote;
} }
@ -151,7 +153,9 @@ class CombinedAPI
if (!($this->event_handler_instance instanceof $this->event_handler)) { if (!($this->event_handler_instance instanceof $this->event_handler)) {
$class_name = $this->event_handler; $class_name = $this->event_handler;
$this->event_handler_instance = new $class_name($this); $this->event_handler_instance = new $class_name($this);
} else if ($this->event_handler_instance) $this->event_handler_instance->__construct($this); } elseif ($this->event_handler_instance) {
$this->event_handler_instance->__construct($this);
}
if (method_exists($this->event_handler_instance, 'onLoop')) { if (method_exists($this->event_handler_instance, 'onLoop')) {
$this->loop_callback = [$this->event_handler_instance, 'onLoop']; $this->loop_callback = [$this->event_handler_instance, 'onLoop'];
} }
@ -178,7 +182,6 @@ class CombinedAPI
{ {
} }
public function loop($max_forks = 0) public function loop($max_forks = 0)
{ {
if (php_sapi_name() !== 'cli') { if (php_sapi_name() !== 'cli') {
@ -193,7 +196,9 @@ class CombinedAPI
} }
} }
foreach ($this->instances as $path => $instance) { foreach ($this->instances as $path => $instance) {
if ($instance->API->authorized !== MTProto::LOGGED_IN) continue; if ($instance->API->authorized !== MTProto::LOGGED_IN) {
continue;
}
if (!$instance->API->settings['updates']['handle_updates']) { if (!$instance->API->settings['updates']['handle_updates']) {
$instance->API->settings['updates']['handle_updates'] = true; $instance->API->settings['updates']['handle_updates'] = true;
} }
@ -210,7 +215,9 @@ class CombinedAPI
$write = []; $write = [];
$except = []; $except = [];
foreach ($this->instances as $path => $instance) { foreach ($this->instances as $path => $instance) {
if ($instance->API->authorized !== MTProto::LOGGED_IN) continue; if ($instance->API->authorized !== MTProto::LOGGED_IN) {
continue;
}
if (time() - $instance->API->last_getdifference > $instance->API->settings['updates']['getdifference_interval']) { if (time() - $instance->API->last_getdifference > $instance->API->settings['updates']['getdifference_interval']) {
$instance->API->get_updates_difference(); $instance->API->get_updates_difference();
} }
@ -227,6 +234,7 @@ class CombinedAPI
\danog\MadelineProto\Logger::log('Serializing combined event handler'); \danog\MadelineProto\Logger::log('Serializing combined event handler');
$this->serialize(); $this->serialize();
} }
try { try {
\Socket::select($read, $write, $except, $this->timeout); \Socket::select($read, $write, $except, $this->timeout);
if (count($read)) { if (count($read)) {
@ -238,9 +246,11 @@ class CombinedAPI
$this->instances[$path]->API->logger->logger('WARNING: Resetting auth key...', \danog\MadelineProto\Logger::WARNING); $this->instances[$path]->API->logger->logger('WARNING: Resetting auth key...', \danog\MadelineProto\Logger::WARNING);
$this->instances[$path]->API->datacenter->sockets[$dc]->temp_auth_key = null; $this->instances[$path]->API->datacenter->sockets[$dc]->temp_auth_key = null;
$this->instances[$path]->API->init_authorization(); $this->instances[$path]->API->init_authorization();
throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key'); throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key');
} }
} }
throw new \danog\MadelineProto\RPCErrorException($error, $error); throw new \danog\MadelineProto\RPCErrorException($error, $error);
} }
$only_updates = $this->instances[$path]->API->handle_messages($dc); $only_updates = $this->instances[$path]->API->handle_messages($dc);

View File

@ -16,6 +16,7 @@ namespace danog\MadelineProto;
abstract class CombinedEventHandler abstract class CombinedEventHandler
{ {
private $CombinedAPI; private $CombinedAPI;
public function __construct($CombinedAPI) public function __construct($CombinedAPI)
{ {
$this->CombinedAPI = $CombinedAPI; $this->CombinedAPI = $CombinedAPI;
@ -23,6 +24,7 @@ abstract class CombinedEventHandler
$this->referenceInstance($path); $this->referenceInstance($path);
} }
} }
final public function __sleep() final public function __sleep()
{ {
$keys = get_object_vars($this); $keys = get_object_vars($this);
@ -30,12 +32,15 @@ abstract class CombinedEventHandler
foreach ($this->CombinedAPI->instance_paths as $path) { foreach ($this->CombinedAPI->instance_paths as $path) {
unset($keys[$path]); unset($keys[$path]);
} }
return array_keys($keys); return array_keys($keys);
} }
final public function referenceInstance($path) final public function referenceInstance($path)
{ {
$this->{$path} = $this->CombinedAPI->instances[$path]; $this->{$path} = $this->CombinedAPI->instances[$path];
} }
final public function removeInstance($path) final public function removeInstance($path)
{ {
if (isset($this->{$path})) { if (isset($this->{$path})) {