Apply fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-12-26 19:24:24 +00:00 committed by StyleCI Bot
parent 84abb27f18
commit 0a8c04998e
11 changed files with 71 additions and 44 deletions

View File

@ -164,7 +164,7 @@ class Connection extends Tools
} }
$packet = stream_get_contents($this->sock, $length); $packet = stream_get_contents($this->sock, $length);
if (strlen($packet) != $length) { if (strlen($packet) != $length) {
throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've read ".($length).", read ".strlen($packet).")!"); throw new \danog\MadelineProto\Exception("WARNING: Wrong length was read (should've read ".($length).', read '.strlen($packet).')!');
} }
return $packet; return $packet;

View File

@ -67,7 +67,9 @@ class MTProto extends PrimeModule
$this->get_updates_difference(); $this->get_updates_difference();
} }
} }
public function parse_settings($settings) {
public function parse_settings($settings)
{
// Detect ipv6 // Detect ipv6
$google = ''; $google = '';
try { try {
@ -194,8 +196,8 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
], ],
'updates' => [ 'updates' => [
'updates_array_limit' => 1000, // How big should be the array containing the updates processed with the default example_update_handler callback 'updates_array_limit' => 1000, // How big should be the array containing the updates processed with the default example_update_handler callback
'callback' => [$this, 'get_updates_update_handler'] // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter 'callback' => [$this, 'get_updates_update_handler'], // A callable function that will be called every time an update is received, must accept an array (for the update) as the only parameter
] ],
]; ];
foreach ($default_settings as $key => $param) { foreach ($default_settings as $key => $param) {
if (!isset($settings[$key])) { if (!isset($settings[$key])) {
@ -217,6 +219,7 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
} }
$this->settings = $settings; $this->settings = $settings;
} }
public function setup_logger() public function setup_logger()
{ {
if (!\danog\MadelineProto\Logger::$constructed) { if (!\danog\MadelineProto\Logger::$constructed) {

View File

@ -65,7 +65,6 @@ trait PeerHandler
$this->should_serialize = true; $this->should_serialize = true;
} }
public function get_info($id, $recursive = true) public function get_info($id, $recursive = true)
{ {
if (is_array($id)) { if (is_array($id)) {
@ -100,18 +99,23 @@ trait PeerHandler
$id = '-100'.$id['channel_id']; $id = '-100'.$id['channel_id'];
break; break;
default: default:
throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($id, true)); throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($id, true));
break; break;
} }
} }
if (preg_match('/^channel#/', $id)) $id = str_replace('channel#', '-100', $id); if (preg_match('/^channel#/', $id)) {
if (preg_match('/^chat#/', $id)) $id = str_replace('chat#', '-', $id); $id = str_replace('channel#', '-100', $id);
if (preg_match('/^user#/', $id)) $id = str_replace('user#', '', $id); }
if (preg_match('/^chat#/', $id)) {
$id = str_replace('chat#', '-', $id);
}
if (preg_match('/^user#/', $id)) {
$id = str_replace('user#', '', $id);
}
if (is_numeric($id)) { if (is_numeric($id)) {
$id = (int)$id; $id = (int) $id;
if (isset($this->chats[$id])) { if (isset($this->chats[$id])) {
return $this->gen_all($this->chats[$id]); return $this->gen_all($this->chats[$id]);
} }
@ -134,7 +138,8 @@ trait PeerHandler
throw new \danog\MadelineProto\Exception("Couldn't find peer by provided username ".$id); throw new \danog\MadelineProto\Exception("Couldn't find peer by provided username ".$id);
} }
public function gen_all($constructor) { public function gen_all($constructor)
{
switch ($constructor['_']) { switch ($constructor['_']) {
case 'user': case 'user':
$inputPeer = $constructor['self'] ? ['_' => 'inputPeerSelf'] : ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $inputPeer = $constructor['self'] ? ['_' => 'inputPeerSelf'] : ['_' => 'inputPeerUser', 'user_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
@ -155,10 +160,10 @@ trait PeerHandler
$inputType = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']]; $inputType = ['_' => 'inputChannel', 'channel_id' => $constructor['id'], 'access_hash' => $constructor['access_hash']];
$Peer = ['_' => 'peerChannel', 'channel_id' => $constructor['id']]; $Peer = ['_' => 'peerChannel', 'channel_id' => $constructor['id']];
$id = $constructor['id']; $id = $constructor['id'];
$bot_api_id = (int)('-100'.$constructor['id']); $bot_api_id = (int) ('-100'.$constructor['id']);
break; break;
default: default:
throw new \danog\MadelineProto\Exception('Invalid constructor given ' . var_export($constructor, true)); throw new \danog\MadelineProto\Exception('Invalid constructor given '.var_export($constructor, true));
break; break;
} }

View File

@ -22,7 +22,8 @@ trait UpdateHandler
public $updates = []; public $updates = [];
public function get_updates_update_handler($update) { public function get_updates_update_handler($update)
{
if (count($this->updates) > $this->settings['updates']['updates_array_limit']) { if (count($this->updates) > $this->settings['updates']['updates_array_limit']) {
array_shift($this->updates); array_shift($this->updates);
} }
@ -30,7 +31,8 @@ trait UpdateHandler
\danog\MadelineProto\Logger::log('Stored ', $update); \danog\MadelineProto\Logger::log('Stored ', $update);
} }
public function get_updates($offset, $limit = null, $timeout = 0) { public function get_updates($offset, $limit = null, $timeout = 0)
{
sleep($timeout); sleep($timeout);
$this->get_updates_difference(); $this->get_updates_difference();
$result = array_slice($this->updates, $offset, $limit, true); $result = array_slice($this->updates, $offset, $limit, true);
@ -39,15 +41,19 @@ trait UpdateHandler
$updates[] = ['update_id' => $key, 'update' => $value]; $updates[] = ['update_id' => $key, 'update' => $value];
unset($this->updates[$key]); unset($this->updates[$key]);
} }
return $updates; return $updates;
} }
public function &get_channel_state($channel, $pts = 0) { public function &get_channel_state($channel, $pts = 0)
{
if (!isset($this->channels_state[$channel])) { if (!isset($this->channels_state[$channel])) {
$this->channels_state[$channel] = ['pts' => $pts, 'pop_pts' => [], 'pending_seq_updates' =>[]]; $this->channels_state[$channel] = ['pts' => $pts, 'pop_pts' => [], 'pending_seq_updates' =>[]];
} }
return $this->channels_state[$channel]; return $this->channels_state[$channel];
} }
public function update_channel_state($channel, $data) public function update_channel_state($channel, $data)
{ {
$this->get_channel_state($channel); $this->get_channel_state($channel);
@ -59,7 +65,7 @@ trait UpdateHandler
{ {
$this->get_channel_state($channel); $this->get_channel_state($channel);
$difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['inputType'], 'filter' => ['_' => 'channelMessagesFilterEmpty'],'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]); $difference = $this->method_call('updates.getChannelDifference', ['channel' => $this->get_info('channel#'.$channel)['inputType'], 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->get_channel_state($channel)['pts'], 'limit' => 30]);
switch ($difference['_']) { switch ($difference['_']) {
case 'updates.channelDifferenceEmpty': case 'updates.channelDifferenceEmpty':
$this->update_channel_state($difference); $this->update_channel_state($difference);
@ -81,6 +87,7 @@ trait UpdateHandler
break; break;
} }
} }
public function update_state($data) public function update_state($data)
{ {
if (empty($this->updates_state)) { if (empty($this->updates_state)) {
@ -92,7 +99,6 @@ trait UpdateHandler
$this->updates_state['date'] = (!isset($data['date']) || $data['date'] < $this->updates_state['date']) ? $this->updates_state['date'] : $data['date']; $this->updates_state['date'] = (!isset($data['date']) || $data['date'] < $this->updates_state['date']) ? $this->updates_state['date'] : $data['date'];
} }
public function get_updates_difference() public function get_updates_difference()
{ {
if (empty($this->updates_state)) { if (empty($this->updates_state)) {
@ -177,6 +183,7 @@ trait UpdateHandler
if (!isset($this->channels_state[$channel_id])) { if (!isset($this->channels_state[$channel_id])) {
return false; return false;
} }
return $this->get_channel_difference($channel_id); return $this->get_channel_difference($channel_id);
break; break;
} }
@ -289,7 +296,8 @@ trait UpdateHandler
} }
} }
public function save_update($update) { public function save_update($update)
{
$this->settings['updates']['callback']($update); $this->settings['updates']['callback']($update);
} }
} }

View File

@ -13,19 +13,23 @@ If not, see <http://www.gnu.org/licenses/>.
namespace danog\MadelineProto; namespace danog\MadelineProto;
/** /**
* Manages serialization of the MadelineProto instance * Manages serialization of the MadelineProto instance.
*/ */
class Serialization { class Serialization
{
public static function serialize($filename, $instance, $force = false) { public static function serialize($filename, $instance, $force = false)
{
if ($instance->API->should_serialize || !(file_exists($filename) && !empty(file_get_contents($filename))) || $force) { if ($instance->API->should_serialize || !(file_exists($filename) && !empty(file_get_contents($filename))) || $force) {
$instance->API->should_serialize = false; $instance->API->should_serialize = false;
return file_put_contents($filename, serialize($instance)); return file_put_contents($filename, serialize($instance));
} }
return false; return false;
} }
public static function deserialize($filename) { public static function deserialize($filename)
{
return file_exists($filename) ? unserialize(file_get_contents($filename)) : false; return file_exists($filename) ? unserialize(file_get_contents($filename)) : false;
} }
} }

View File

@ -364,6 +364,7 @@ class TL extends \danog\MadelineProto\Tools
if (isset($x['flags'])) { // I don't think we need this anymore if (isset($x['flags'])) { // I don't think we need this anymore
unset($x['flags']); unset($x['flags']);
} }
return $x; return $x;
} }

View File

@ -50,7 +50,6 @@ trait Login
$this->API->datacenter->authorized = true; $this->API->datacenter->authorized = true;
$this->API->get_updates_difference(); $this->API->get_updates_difference();
$this->API->should_serialize = true; $this->API->should_serialize = true;
$this->API->updates = []; $this->API->updates = [];

View File

@ -17,15 +17,18 @@ namespace danog\MadelineProto\Wrappers;
*/ */
trait PeerHandler trait PeerHandler
{ {
public function get_info($id, $recursive = true) { public function get_info($id, $recursive = true)
{
return $this->API->get_info($id, $recursive); return $this->API->get_info($id, $recursive);
} }
public function gen_all($constructor) { public function gen_all($constructor)
{
return $this->API->gen_all($constructor); return $this->API->gen_all($constructor);
} }
public function resolve_username($username) { public function resolve_username($username)
{
return $this->API->resolve_username($username); return $this->API->resolve_username($username);
} }
} }

View File

@ -15,11 +15,15 @@ namespace danog\MadelineProto\Wrappers;
/** /**
* Manages changing API instance settings. * Manages changing API instance settings.
*/ */
trait SettingsManager { trait SettingsManager
public function get_settings() { {
public function get_settings()
{
return $this->API->settings; return $this->API->settings;
} }
public function update_settings($settings) {
public function update_settings($settings)
{
$this->API->parse_settings($settings); $this->API->parse_settings($settings);
} }
} }

View File

@ -14,7 +14,7 @@ If not, see <http://www.gnu.org/licenses/>.
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
$settings = []; $settings = [];
if (file_exists('web_data.php')) { if (file_exists('web_data.php')) {
require_once('web_data.php'); require_once 'web_data.php';
} }
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline'); $MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline');