Move botan integration in to separate namespace

parent 457da719
...@@ -8,12 +8,13 @@ ...@@ -8,12 +8,13 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot; namespace Longman\TelegramBot\Extensions\Botan;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog;
/** /**
* Class Botan * Class Botan
...@@ -54,6 +55,13 @@ class Botan ...@@ -54,6 +55,13 @@ class Botan
*/ */
public static $command = ''; public static $command = '';
/**
* Enabled
*
* @var bool
*/
protected static $enabled = false;
/** /**
* Initialize Botan * Initialize Botan
* *
...@@ -74,7 +82,7 @@ class Botan ...@@ -74,7 +82,7 @@ class Botan
$options = array_merge($options_default, $options); $options = array_merge($options_default, $options);
if (!is_numeric($options['timeout'])) { if (! is_numeric($options['timeout'])) {
throw new TelegramException('Timeout must be a number!'); throw new TelegramException('Timeout must be a number!');
} }
...@@ -145,7 +153,7 @@ class Botan ...@@ -145,7 +153,7 @@ class Botan
if ($entity->getType() === 'bot_command' && $entity->getOffset() === 0) { if ($entity->getType() === 'bot_command' && $entity->getOffset() === 0) {
if ($command === 'generic') { if ($command === 'generic') {
$command = 'Generic'; $command = 'Generic';
} elseif ($command === 'genericmessage') { // This should not happen as it equals normal message but leaving it as a fail-safe } else if ($command === 'genericmessage') { // This should not happen as it equals normal message but leaving it as a fail-safe
$command = 'Generic Message'; $command = 'Generic Message';
} else { } else {
$command = '/' . $command; $command = '/' . $command;
...@@ -190,7 +198,7 @@ class Botan ...@@ -190,7 +198,7 @@ class Botan
$responseData = json_decode($result, true); $responseData = json_decode($result, true);
if (!$responseData || $responseData['status'] !== 'accepted') { if (! $responseData || $responseData['status'] !== 'accepted') {
TelegramLog::debug('Botan.io stats report failed: %s', $result ?: 'empty response'); TelegramLog::debug('Botan.io stats report failed: %s', $result ?: 'empty response');
return false; return false;
...@@ -247,4 +255,25 @@ class Botan ...@@ -247,4 +255,25 @@ class Botan
return $result; return $result;
} }
/**
* Return enabled status
*
* @return bool
*/
public static function isEnabled()
{
return self::$enabled;
}
/**
* Set enabled status
*
* @param $enabled bool
* @return void
*/
public static function setEnabled($enabled)
{
self::$enabled = $enabled;
}
} }
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot; namespace Longman\TelegramBot\Extensions\Botan;
use Exception; use Exception;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
/** /**
...@@ -23,7 +24,7 @@ class BotanDB extends DB ...@@ -23,7 +24,7 @@ class BotanDB extends DB
*/ */
public static function initializeBotanDb() public static function initializeBotanDb()
{ {
if (!defined('TB_BOTAN_SHORTENER')) { if (! defined('TB_BOTAN_SHORTENER')) {
define('TB_BOTAN_SHORTENER', self::$table_prefix . 'botan_shortener'); define('TB_BOTAN_SHORTENER', self::$table_prefix . 'botan_shortener');
} }
} }
...@@ -39,7 +40,7 @@ class BotanDB extends DB ...@@ -39,7 +40,7 @@ class BotanDB extends DB
*/ */
public static function selectShortUrl($url, $user_id) public static function selectShortUrl($url, $user_id)
{ {
if (!self::isDbConnected()) { if (! self::isDbConnected()) {
return false; return false;
} }
...@@ -75,7 +76,7 @@ class BotanDB extends DB ...@@ -75,7 +76,7 @@ class BotanDB extends DB
*/ */
public static function insertShortUrl($url, $user_id, $short_url) public static function insertShortUrl($url, $user_id, $short_url)
{ {
if (!self::isDbConnected()) { if (! self::isDbConnected()) {
return false; return false;
} }
......
...@@ -19,6 +19,7 @@ use Longman\TelegramBot\Commands\Command; ...@@ -19,6 +19,7 @@ use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Console\Kernel as ConsoleKernel; use Longman\TelegramBot\Console\Kernel as ConsoleKernel;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Extensions\Botan\Botan;
use Longman\TelegramBot\Http\Client; use Longman\TelegramBot\Http\Client;
use Longman\TelegramBot\Http\Kernel; use Longman\TelegramBot\Http\Kernel;
use Longman\TelegramBot\Http\Request; use Longman\TelegramBot\Http\Request;
...@@ -114,13 +115,6 @@ class Telegram ...@@ -114,13 +115,6 @@ class Telegram
*/ */
protected $last_command_response; protected $last_command_response;
/**
* Botan.io integration
*
* @var boolean
*/
protected $botan_enabled = false;
/** /**
* Check if runCommands() is running in this session * Check if runCommands() is running in this session
* *
...@@ -500,7 +494,7 @@ class Telegram ...@@ -500,7 +494,7 @@ class Telegram
$this->last_command_response = $this->executeCommand('generic'); $this->last_command_response = $this->executeCommand('generic');
} else { } else {
// Botan.io integration, make sure only the actual command user executed is reported // Botan.io integration, make sure only the actual command user executed is reported
if ($this->botan_enabled) { if (Botan::isEnabled()) {
Botan::lock($command); Botan::lock($command);
} }
...@@ -509,7 +503,7 @@ class Telegram ...@@ -509,7 +503,7 @@ class Telegram
$this->last_command_response = $command_obj->preExecute(); $this->last_command_response = $command_obj->preExecute();
// Botan.io integration, send report after executing the command // Botan.io integration, send report after executing the command
if ($this->botan_enabled) { if (Botan::isEnabled()) {
Botan::track($this->update, $command); Botan::track($this->update, $command);
} }
} }
...@@ -872,7 +866,7 @@ class Telegram ...@@ -872,7 +866,7 @@ class Telegram
public function enableBotan($token, array $options = []) public function enableBotan($token, array $options = [])
{ {
Botan::initializeBotan($token, $options); Botan::initializeBotan($token, $options);
$this->botan_enabled = true; Botan::setEnabled(true);
return $this; return $this;
} }
...@@ -905,7 +899,7 @@ class Telegram ...@@ -905,7 +899,7 @@ class Telegram
} }
$this->run_commands = true; $this->run_commands = true;
$this->botan_enabled = false; // Force disable Botan.io integration, we don't want to track self-executed commands! Botan::setEnabled(false); // Force disable Botan.io integration, we don't want to track self-executed commands!
$result = Client::getMe(); $result = Client::getMe();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment