Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BotManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BotManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 21 | class BotManager |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string Telegram post servers IP range |
||
| 25 | */ |
||
| 26 | const TELEGRAM_IP_RANGE = '149.154.167.197-149.154.167.233'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string The output for testing, instead of echoing |
||
| 30 | */ |
||
| 31 | private $output = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \Longman\TelegramBot\Telegram |
||
| 35 | */ |
||
| 36 | private $telegram; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \NPM\TelegramBotManager\Params Object that manages the parameters. |
||
| 40 | */ |
||
| 41 | private $params; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \NPM\TelegramBotManager\Action Object that contains the current action. |
||
| 45 | */ |
||
| 46 | private $action; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * BotManager constructor. |
||
| 50 | * |
||
| 51 | * @param array $params |
||
| 52 | * |
||
| 53 | * @throws \NPM\TelegramBotManager\Exception\InvalidParamsException |
||
| 54 | * @throws \NPM\TelegramBotManager\Exception\InvalidActionException |
||
| 55 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 56 | */ |
||
| 57 | public function __construct(array $params) |
||
| 58 | { |
||
| 59 | // Initialise logging before anything else, to allow errors to be logged. |
||
| 60 | $this->initLogging($params['logging'] ?? []); |
||
| 61 | |||
| 62 | $this->params = new Params($params); |
||
| 63 | $this->action = new Action($this->params->getScriptParam('a')); |
||
| 64 | |||
| 65 | // Set up a new Telegram instance. |
||
| 66 | $this->telegram = new Telegram( |
||
| 67 | $this->params->getBotParam('api_key'), |
||
| 68 | $this->params->getBotParam('bot_username') |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Check if we're busy running the PHPUnit tests. |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public static function inTest(): bool |
||
| 78 | { |
||
| 79 | return defined('PHPUNIT_TEST') && PHPUNIT_TEST === true; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return the Telegram object. |
||
| 84 | * |
||
| 85 | * @return \Longman\TelegramBot\Telegram |
||
| 86 | */ |
||
| 87 | public function getTelegram(): Telegram |
||
| 88 | { |
||
| 89 | return $this->telegram; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the Params object. |
||
| 94 | * |
||
| 95 | * @return \NPM\TelegramBotManager\Params |
||
| 96 | */ |
||
| 97 | public function getParams(): Params |
||
| 98 | { |
||
| 99 | return $this->params; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the Action object. |
||
| 104 | * |
||
| 105 | * @return \NPM\TelegramBotManager\Action |
||
| 106 | */ |
||
| 107 | public function getAction(): Action |
||
| 108 | { |
||
| 109 | return $this->action; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Run this thing in all its glory! |
||
| 114 | * |
||
| 115 | * @return \NPM\TelegramBotManager\BotManager |
||
| 116 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 117 | * @throws \NPM\TelegramBotManager\Exception\InvalidAccessException |
||
| 118 | * @throws \NPM\TelegramBotManager\Exception\InvalidWebhookException |
||
| 119 | * @throws \Exception |
||
| 120 | */ |
||
| 121 | public function run(): self |
||
| 122 | { |
||
| 123 | // Make sure this is a valid call. |
||
| 124 | $this->validateSecret(); |
||
| 125 | |||
| 126 | if (!$this->isValidRequest()) { |
||
| 127 | throw new InvalidAccessException('Invalid access'); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($this->action->isAction(['set', 'unset', 'reset'])) { |
||
| 131 | $this->validateAndSetWebhook(); |
||
| 132 | } elseif ($this->action->isAction('handle')) { |
||
| 133 | $this->setBotExtras(); |
||
| 134 | $this->handleRequest(); |
||
| 135 | } elseif ($this->action->isAction('cron')) { |
||
| 136 | $this->setBotExtras(); |
||
| 137 | $this->handleCron(); |
||
| 138 | } |
||
| 139 | |||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Initialise all loggers. |
||
| 145 | * |
||
| 146 | * @param array $log_paths |
||
| 147 | * |
||
| 148 | * @return \NPM\TelegramBotManager\BotManager |
||
| 149 | * @throws \Exception |
||
| 150 | */ |
||
| 151 | public function initLogging(array $log_paths): self |
||
| 152 | { |
||
| 153 | foreach ($log_paths as $logger => $logfile) { |
||
| 154 | ('debug' === $logger) && TelegramLog::initDebugLog($logfile); |
||
| 155 | ('error' === $logger) && TelegramLog::initErrorLog($logfile); |
||
| 156 | ('update' === $logger) && TelegramLog::initUpdateLog($logfile); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Make sure the passed secret is valid. |
||
| 164 | * |
||
| 165 | * @param bool $force Force validation, even on CLI. |
||
| 166 | * |
||
| 167 | * @return \NPM\TelegramBotManager\BotManager |
||
| 168 | * @throws \NPM\TelegramBotManager\Exception\InvalidAccessException |
||
| 169 | */ |
||
| 170 | public function validateSecret(bool $force = false): self |
||
| 171 | { |
||
| 172 | // If we're running from CLI, secret isn't necessary. |
||
| 173 | if ($force || 'cli' !== PHP_SAPI) { |
||
| 174 | $secret = $this->params->getBotParam('secret'); |
||
| 175 | $secret_get = $this->params->getScriptParam('s'); |
||
| 176 | if (!isset($secret, $secret_get) || $secret !== $secret_get) { |
||
| 177 | throw new InvalidAccessException('Invalid access'); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Make sure the webhook is valid and perform the requested webhook operation. |
||
| 186 | * |
||
| 187 | * @return \NPM\TelegramBotManager\BotManager |
||
| 188 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 189 | * @throws \NPM\TelegramBotManager\Exception\InvalidWebhookException |
||
| 190 | */ |
||
| 191 | public function validateAndSetWebhook(): self |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Save the test output and echo it if we're not in a test. |
||
| 224 | * |
||
| 225 | * @param string $output |
||
| 226 | * |
||
| 227 | * @return \NPM\TelegramBotManager\BotManager |
||
| 228 | */ |
||
| 229 | private function handleOutput(string $output): self |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set any extra bot features that have been assigned on construction. |
||
| 242 | * |
||
| 243 | * @return \NPM\TelegramBotManager\BotManager |
||
| 244 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 245 | */ |
||
| 246 | public function setBotExtras(): self |
||
| 247 | { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set extra bot parameters for Telegram object. |
||
| 256 | * |
||
| 257 | * @return \NPM\TelegramBotManager\BotManager |
||
| 258 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 259 | */ |
||
| 260 | protected function setBotExtrasTelegram(): self |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set extra bot parameters for Request class. |
||
| 295 | * |
||
| 296 | * @return \NPM\TelegramBotManager\BotManager |
||
| 297 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 298 | */ |
||
| 299 | protected function setBotExtrasRequest(): self |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Handle the request, which calls either the Webhook or getUpdates method respectively. |
||
| 324 | * |
||
| 325 | * @return \NPM\TelegramBotManager\BotManager |
||
| 326 | * @throws \NPM\TelegramBotManager\Exception\InvalidAccessException |
||
| 327 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 328 | */ |
||
| 329 | public function handleRequest(): self |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Handle cron. |
||
| 346 | * |
||
| 347 | * @return \NPM\TelegramBotManager\BotManager |
||
| 348 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 349 | */ |
||
| 350 | public function handleCron(): self |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the number of seconds the script should loop. |
||
| 365 | * |
||
| 366 | * @return int |
||
| 367 | */ |
||
| 368 | View Code Duplication | public function getLoopTime(): int |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get the number of seconds the script should wait after each getUpdates request. |
||
| 385 | * |
||
| 386 | * @return int |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function getLoopInterval(): int |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Loop the getUpdates method for the passed amount of seconds. |
||
| 402 | * |
||
| 403 | * @param int $loop_time_in_seconds |
||
| 404 | * @param int $loop_interval_in_seconds |
||
| 405 | * |
||
| 406 | * @return \NPM\TelegramBotManager\BotManager |
||
| 407 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 408 | */ |
||
| 409 | public function handleGetUpdatesLoop(int $loop_time_in_seconds, int $loop_interval_in_seconds = 2): self |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Handle the updates using the getUpdates method. |
||
| 428 | * |
||
| 429 | * @return \NPM\TelegramBotManager\BotManager |
||
| 430 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 431 | */ |
||
| 432 | public function handleGetUpdates(): self |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Handle the updates using the Webhook method. |
||
| 475 | * |
||
| 476 | * @return \NPM\TelegramBotManager\BotManager |
||
| 477 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 478 | * @throws \NPM\TelegramBotManager\Exception\InvalidAccessException |
||
| 479 | */ |
||
| 480 | public function handleWebhook(): self |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Return the current test output and clear it. |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public function getOutput(): string |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Check if this is a valid request coming from a Telegram API IP address. |
||
| 502 | * |
||
| 503 | * @link https://core.telegram.org/bots/webhooks#the-short-version |
||
| 504 | * |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | public function isValidRequest(): bool |
||
| 534 | } |
||
| 535 |