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 DB 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 DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class DB |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * MySQL credentials |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | static protected $mysql_credentials = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * PDO object |
||
| 36 | * |
||
| 37 | * @var PDO |
||
| 38 | */ |
||
| 39 | static protected $pdo; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Table prefix |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | static protected $table_prefix; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Telegram class object |
||
| 50 | * |
||
| 51 | * @var \Longman\TelegramBot\Telegram |
||
| 52 | */ |
||
| 53 | static protected $telegram; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Initialize |
||
| 57 | * |
||
| 58 | * @param array $credentials Database connection details |
||
| 59 | * @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
||
| 60 | * @param string $table_prefix Table prefix |
||
| 61 | * @param string $encoding Database character encoding |
||
| 62 | * |
||
| 63 | * @return PDO PDO database object |
||
| 64 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 65 | */ |
||
| 66 | 9 | public static function initialize( |
|
| 67 | array $credentials, |
||
| 68 | Telegram $telegram, |
||
| 69 | $table_prefix = null, |
||
| 70 | $encoding = 'utf8mb4' |
||
| 71 | ) { |
||
| 72 | 9 | if (empty($credentials)) { |
|
| 73 | throw new TelegramException('MySQL credentials not provided!'); |
||
| 74 | } |
||
| 75 | |||
| 76 | 9 | $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database']; |
|
| 77 | 9 | $options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding]; |
|
| 78 | try { |
||
| 79 | 9 | $pdo = new PDO($dsn, $credentials['user'], $credentials['password'], $options); |
|
| 80 | 9 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); |
|
| 81 | } catch (PDOException $e) { |
||
| 82 | throw new TelegramException($e->getMessage()); |
||
| 83 | } |
||
| 84 | |||
| 85 | 9 | self::$pdo = $pdo; |
|
| 86 | 9 | self::$telegram = $telegram; |
|
| 87 | 9 | self::$mysql_credentials = $credentials; |
|
| 88 | 9 | self::$table_prefix = $table_prefix; |
|
| 89 | |||
| 90 | 9 | self::defineTable(); |
|
| 91 | |||
| 92 | 9 | return self::$pdo; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * External Initialize |
||
| 97 | * |
||
| 98 | * Let you use the class with an external already existing Pdo Mysql connection. |
||
| 99 | * |
||
| 100 | * @param PDO $external_pdo_connection PDO database object |
||
| 101 | * @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
||
| 102 | * @param string $table_prefix Table prefix |
||
| 103 | * |
||
| 104 | * @return PDO PDO database object |
||
| 105 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 106 | */ |
||
| 107 | public static function externalInitialize($external_pdo_connection, Telegram $telegram, $table_prefix = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Define all the table with the proper prefix |
||
| 125 | */ |
||
| 126 | 9 | protected static function defineTable() |
|
| 127 | { |
||
| 128 | 9 | if (!defined('TB_TELEGRAM_UPDATE')) { |
|
| 129 | 1 | define('TB_TELEGRAM_UPDATE', self::$table_prefix . 'telegram_update'); |
|
| 130 | } |
||
| 131 | 9 | if (!defined('TB_MESSAGE')) { |
|
| 132 | 1 | define('TB_MESSAGE', self::$table_prefix . 'message'); |
|
| 133 | } |
||
| 134 | 9 | if (!defined('TB_EDITED_MESSAGE')) { |
|
| 135 | 1 | define('TB_EDITED_MESSAGE', self::$table_prefix . 'edited_message'); |
|
| 136 | } |
||
| 137 | 9 | if (!defined('TB_INLINE_QUERY')) { |
|
| 138 | 1 | define('TB_INLINE_QUERY', self::$table_prefix . 'inline_query'); |
|
| 139 | } |
||
| 140 | 9 | if (!defined('TB_CHOSEN_INLINE_RESULT')) { |
|
| 141 | 1 | define('TB_CHOSEN_INLINE_RESULT', self::$table_prefix . 'chosen_inline_result'); |
|
| 142 | } |
||
| 143 | 9 | if (!defined('TB_CALLBACK_QUERY')) { |
|
| 144 | 1 | define('TB_CALLBACK_QUERY', self::$table_prefix . 'callback_query'); |
|
| 145 | } |
||
| 146 | 9 | if (!defined('TB_USER')) { |
|
| 147 | 1 | define('TB_USER', self::$table_prefix . 'user'); |
|
| 148 | } |
||
| 149 | 9 | if (!defined('TB_CHAT')) { |
|
| 150 | 1 | define('TB_CHAT', self::$table_prefix . 'chat'); |
|
| 151 | } |
||
| 152 | 9 | if (!defined('TB_USER_CHAT')) { |
|
| 153 | 1 | define('TB_USER_CHAT', self::$table_prefix . 'user_chat'); |
|
| 154 | } |
||
| 155 | 9 | } |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Check if database connection has been created |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | 9 | public static function isDbConnected() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Fetch update(s) from DB |
||
| 173 | * |
||
| 174 | * @param int $limit Limit the number of updates to fetch |
||
| 175 | * |
||
| 176 | * @return array|bool Fetched data or false if not connected |
||
| 177 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 178 | */ |
||
| 179 | public static function selectTelegramUpdate($limit = null) |
||
| 180 | { |
||
| 181 | if (!self::isDbConnected()) { |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | try { |
||
| 186 | $query = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '` '; |
||
| 187 | $query .= 'ORDER BY `id` DESC'; |
||
| 188 | |||
| 189 | if (!is_null($limit)) { |
||
| 190 | $query .= ' LIMIT :limit '; |
||
| 191 | } |
||
| 192 | |||
| 193 | $sth_select_telegram_update = self::$pdo->prepare($query); |
||
| 194 | $sth_select_telegram_update->bindParam(':limit', $limit, PDO::PARAM_INT); |
||
| 195 | $sth_select_telegram_update->execute(); |
||
| 196 | $results = $sth_select_telegram_update->fetchAll(PDO::FETCH_ASSOC); |
||
| 197 | } catch (PDOException $e) { |
||
| 198 | throw new TelegramException($e->getMessage()); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $results; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Fetch message(s) from DB |
||
| 206 | * |
||
| 207 | * @param int $limit Limit the number of messages to fetch |
||
| 208 | * |
||
| 209 | * @return array|bool Fetched data or false if not connected |
||
| 210 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 211 | */ |
||
| 212 | public static function selectMessages($limit = null) |
||
| 213 | { |
||
| 214 | if (!self::isDbConnected()) { |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | |||
| 218 | try { |
||
| 219 | //message table |
||
| 220 | $query = 'SELECT * FROM `' . TB_MESSAGE . '` '; |
||
| 221 | $query .= 'WHERE ' . TB_MESSAGE . '.`update_id` != 0 '; |
||
| 222 | $query .= 'ORDER BY ' . TB_MESSAGE . '.`message_id` DESC'; |
||
| 223 | |||
| 224 | if (!is_null($limit)) { |
||
| 225 | $query .= ' LIMIT :limit '; |
||
| 226 | } |
||
| 227 | |||
| 228 | $sth = self::$pdo->prepare($query); |
||
| 229 | $sth->bindParam(':limit', $limit, PDO::PARAM_INT); |
||
| 230 | $sth->execute(); |
||
| 231 | $results = $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 232 | } catch (PDOException $e) { |
||
| 233 | throw new TelegramException($e->getMessage()); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $results; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Convert from unix timestamp to timestamp |
||
| 241 | * |
||
| 242 | * @param int $time Unix timestamp |
||
| 243 | * |
||
| 244 | * @return null|string Timestamp if a time has been passed, else null |
||
| 245 | */ |
||
| 246 | 7 | protected static function getTimestamp($time = null) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Insert entry to telegram_update table |
||
| 256 | * |
||
| 257 | * @param int $id |
||
| 258 | * @param int $chat_id |
||
| 259 | * @param int $message_id |
||
| 260 | * @param int $inline_query_id |
||
| 261 | * @param int $chosen_inline_result_id |
||
| 262 | * @param int $callback_query_id |
||
| 263 | * @param int $edited_message_id |
||
| 264 | * |
||
| 265 | * @return bool If the insert was successful |
||
| 266 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 267 | */ |
||
| 268 | public static function insertTelegramUpdate( |
||
| 269 | $id, |
||
| 270 | $chat_id, |
||
| 271 | $message_id, |
||
| 272 | $inline_query_id, |
||
| 273 | $chosen_inline_result_id, |
||
| 274 | $callback_query_id, |
||
| 275 | $edited_message_id |
||
| 276 | ) { |
||
| 277 | if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id) && is_null($edited_message_id)) { |
||
| 278 | throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null'); |
||
| 279 | } |
||
| 280 | |||
| 281 | if (!self::isDbConnected()) { |
||
| 282 | return false; |
||
| 283 | } |
||
| 284 | |||
| 285 | try { |
||
| 286 | $sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '` |
||
| 287 | ( |
||
| 288 | `id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id` |
||
| 289 | ) |
||
| 290 | VALUES ( |
||
| 291 | :id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id |
||
| 292 | ) |
||
| 293 | '); |
||
| 294 | |||
| 295 | $sth_insert_telegram_update->bindParam(':id', $id, PDO::PARAM_INT); |
||
| 296 | $sth_insert_telegram_update->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
||
| 297 | $sth_insert_telegram_update->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
||
| 298 | $sth_insert_telegram_update->bindParam(':inline_query_id', $inline_query_id, PDO::PARAM_INT); |
||
| 299 | $sth_insert_telegram_update->bindParam( |
||
| 300 | ':chosen_inline_result_id', |
||
| 301 | $chosen_inline_result_id, |
||
| 302 | PDO::PARAM_INT |
||
| 303 | ); |
||
| 304 | $sth_insert_telegram_update->bindParam(':callback_query_id', $callback_query_id, PDO::PARAM_INT); |
||
| 305 | $sth_insert_telegram_update->bindParam(':edited_message_id', $edited_message_id, PDO::PARAM_INT); |
||
| 306 | |||
| 307 | return $sth_insert_telegram_update->execute(); |
||
| 308 | } catch (PDOException $e) { |
||
| 309 | throw new TelegramException($e->getMessage()); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Insert users and save their connection to chats |
||
| 315 | * |
||
| 316 | * @param \Longman\TelegramBot\Entities\User $user |
||
| 317 | * @param string $date |
||
| 318 | * @param \Longman\TelegramBot\Entities\Chat $chat |
||
| 319 | * |
||
| 320 | * @return bool If the insert was successful |
||
| 321 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 322 | */ |
||
| 323 | 6 | public static function insertUser(User $user, $date, Chat $chat = null) |
|
| 324 | { |
||
| 325 | 6 | if (!self::isDbConnected()) { |
|
| 326 | return false; |
||
| 327 | } |
||
| 328 | |||
| 329 | 6 | $user_id = $user->getId(); |
|
| 330 | 6 | $username = $user->getUsername(); |
|
| 331 | 6 | $first_name = $user->getFirstName(); |
|
| 332 | 6 | $last_name = $user->getLastName(); |
|
| 333 | |||
| 334 | try { |
||
| 335 | 6 | $sth1 = self::$pdo->prepare('INSERT INTO `' . TB_USER . '` |
|
| 336 | ( |
||
| 337 | `id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at` |
||
| 338 | ) |
||
| 339 | VALUES ( |
||
| 340 | :id, :username, :first_name, :last_name, :date, :date |
||
| 341 | ) |
||
| 342 | ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name, |
||
| 343 | `last_name`=:last_name, `updated_at`=:date |
||
| 344 | 6 | '); |
|
| 345 | |||
| 346 | 6 | $sth1->bindParam(':id', $user_id, PDO::PARAM_INT); |
|
| 347 | 6 | $sth1->bindParam(':username', $username, PDO::PARAM_STR, 255); |
|
| 348 | 6 | $sth1->bindParam(':first_name', $first_name, PDO::PARAM_STR, 255); |
|
| 349 | 6 | $sth1->bindParam(':last_name', $last_name, PDO::PARAM_STR, 255); |
|
| 350 | 6 | $sth1->bindParam(':date', $date, PDO::PARAM_STR); |
|
| 351 | |||
| 352 | 6 | $status = $sth1->execute(); |
|
| 353 | } catch (PDOException $e) { |
||
| 354 | throw new TelegramException($e->getMessage()); |
||
| 355 | } |
||
| 356 | |||
| 357 | //insert also the relationship to the chat into user_chat table |
||
| 358 | 6 | if (!is_null($chat)) { |
|
| 359 | 6 | $chat_id = $chat->getId(); |
|
| 360 | try { |
||
| 361 | 6 | $sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USER_CHAT . '` |
|
| 362 | ( |
||
| 363 | `user_id`, `chat_id` |
||
| 364 | ) |
||
| 365 | VALUES ( |
||
| 366 | :user_id, :chat_id |
||
| 367 | 6 | )'); |
|
| 368 | |||
| 369 | 6 | $sth3->bindParam(':user_id', $user_id, PDO::PARAM_INT); |
|
| 370 | 6 | $sth3->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
|
| 371 | |||
| 372 | 6 | $status = $sth3->execute(); |
|
| 373 | } catch (PDOException $e) { |
||
| 374 | throw new TelegramException($e->getMessage()); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | 6 | return $status; |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Insert chat |
||
| 383 | * |
||
| 384 | * @param \Longman\TelegramBot\Entities\Chat $chat |
||
| 385 | * @param string $date |
||
| 386 | * @param int $migrate_to_chat_id |
||
| 387 | * |
||
| 388 | * @return bool If the insert was successful |
||
| 389 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 390 | */ |
||
| 391 | 6 | public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Insert request into database |
||
| 434 | * |
||
| 435 | * @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails? |
||
| 436 | * |
||
| 437 | * @param \Longman\TelegramBot\Entities\Update $update |
||
| 438 | * |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public static function insertRequest(Update $update) |
||
| 442 | { |
||
| 443 | $update_id = $update->getUpdateId(); |
||
| 444 | if ($update->getUpdateType() == 'message') { |
||
| 445 | $message = $update->getMessage(); |
||
| 446 | |||
| 447 | if (self::insertMessageRequest($message)) { |
||
| 448 | $message_id = $message->getMessageId(); |
||
| 449 | $chat_id = $message->getChat()->getId(); |
||
| 450 | return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null, null); |
||
| 451 | } |
||
| 452 | View Code Duplication | } elseif ($update->getUpdateType() == 'inline_query') { |
|
| 453 | $inline_query = $update->getInlineQuery(); |
||
| 454 | |||
| 455 | if (self::insertInlineQueryRequest($inline_query)) { |
||
| 456 | $inline_query_id = $inline_query->getId(); |
||
| 457 | return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null, null); |
||
| 458 | } |
||
| 459 | } elseif ($update->getUpdateType() == 'chosen_inline_result') { |
||
| 460 | $chosen_inline_result = $update->getChosenInlineResult(); |
||
| 461 | |||
| 462 | if (self::insertChosenInlineResultRequest($chosen_inline_result)) { |
||
| 463 | $chosen_inline_result_local_id = self::$pdo->lastInsertId(); |
||
| 464 | return self::insertTelegramUpdate( |
||
| 465 | $update_id, |
||
| 466 | null, |
||
| 467 | null, |
||
| 468 | null, |
||
| 469 | $chosen_inline_result_local_id, |
||
| 470 | null, |
||
| 471 | null |
||
| 472 | ); |
||
| 473 | } |
||
| 474 | View Code Duplication | } elseif ($update->getUpdateType() == 'callback_query') { |
|
| 475 | $callback_query = $update->getCallbackQuery(); |
||
| 476 | |||
| 477 | if (self::insertCallbackQueryRequest($callback_query)) { |
||
| 478 | $callback_query_id = $callback_query->getId(); |
||
| 479 | return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null); |
||
| 480 | } |
||
| 481 | } elseif ($update->getUpdateType() == 'edited_message') { |
||
| 482 | $edited_message = $update->getEditedMessage(); |
||
| 483 | |||
| 484 | if (self::insertEditedMessageRequest($edited_message)) { |
||
| 485 | $chat_id = $edited_message->getChat()->getId(); |
||
| 486 | $edited_message_local_id = self::$pdo->lastInsertId(); |
||
| 487 | return self::insertTelegramUpdate( |
||
| 488 | $update_id, |
||
| 489 | $chat_id, |
||
| 490 | null, |
||
| 491 | null, |
||
| 492 | null, |
||
| 493 | null, |
||
| 494 | $edited_message_local_id |
||
| 495 | ); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | return false; |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Insert inline query request into database |
||
| 504 | * |
||
| 505 | * @param \Longman\TelegramBot\Entities\InlineQuery $inline_query |
||
| 506 | * |
||
| 507 | * @return bool If the insert was successful |
||
| 508 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 509 | */ |
||
| 510 | View Code Duplication | public static function insertInlineQueryRequest(InlineQuery $inline_query) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Insert chosen inline result request into database |
||
| 555 | * |
||
| 556 | * @param \Longman\TelegramBot\Entities\ChosenInlineResult $chosen_inline_result |
||
| 557 | * |
||
| 558 | * @return bool If the insert was successful |
||
| 559 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 560 | */ |
||
| 561 | View Code Duplication | public static function insertChosenInlineResultRequest(ChosenInlineResult $chosen_inline_result) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Insert callback query request into database |
||
| 606 | * |
||
| 607 | * @param \Longman\TelegramBot\Entities\CallbackQuery $callback_query |
||
| 608 | * |
||
| 609 | * @return bool If the insert was successful |
||
| 610 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 611 | */ |
||
| 612 | public static function insertCallbackQueryRequest(CallbackQuery $callback_query) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Insert Message request in db |
||
| 676 | * |
||
| 677 | * @param \Longman\TelegramBot\Entities\Message $message |
||
| 678 | * |
||
| 679 | * @return bool If the insert was successful |
||
| 680 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 681 | */ |
||
| 682 | 6 | public static function insertMessageRequest(Message $message) |
|
| 683 | { |
||
| 684 | 6 | if (!self::isDbConnected()) { |
|
| 685 | return false; |
||
| 686 | } |
||
| 687 | |||
| 688 | 6 | $from = $message->getFrom(); |
|
| 689 | 6 | $chat = $message->getChat(); |
|
| 690 | |||
| 691 | 6 | $chat_id = $chat->getId(); |
|
| 692 | |||
| 693 | 6 | $date = self::getTimestamp($message->getDate()); |
|
| 694 | |||
| 695 | 6 | $forward_from = $message->getForwardFrom(); |
|
| 696 | 6 | $forward_from_chat = $message->getForwardFromChat(); |
|
| 697 | 6 | $photo = $message->getPhoto(); |
|
| 698 | 6 | $entities = $message->getEntities(); |
|
| 699 | 6 | $new_chat_member = $message->getNewChatMember(); |
|
| 700 | 6 | $new_chat_photo = $message->getNewChatPhoto(); |
|
| 701 | 6 | $left_chat_member = $message->getLeftChatMember(); |
|
| 702 | 6 | $migrate_to_chat_id = $message->getMigrateToChatId(); |
|
| 703 | |||
| 704 | //Insert chat, update chat id in case it migrated |
||
| 705 | 6 | self::insertChat($chat, $date, $migrate_to_chat_id); |
|
| 706 | |||
| 707 | //Insert user and the relation with the chat |
||
| 708 | 6 | self::insertUser($from, $date, $chat); |
|
| 709 | |||
| 710 | //Insert the forwarded message user in users table |
||
| 711 | 6 | if (is_object($forward_from)) { |
|
| 712 | $forward_date = self::getTimestamp($message->getForwardDate()); |
||
| 713 | self::insertUser($forward_from, $forward_date); |
||
| 714 | $forward_from = $forward_from->getId(); |
||
| 715 | } |
||
| 716 | |||
| 717 | 6 | if (is_object($forward_from_chat)) { |
|
| 718 | $forward_date = self::getTimestamp($message->getForwardDate()); |
||
| 719 | self::insertChat($forward_from_chat, $forward_date); |
||
| 720 | $forward_from_chat = $forward_from_chat->getId(); |
||
| 721 | } |
||
| 722 | |||
| 723 | //New and left chat member |
||
| 724 | 6 | if ($new_chat_member) { |
|
| 725 | //Insert the new chat user |
||
| 726 | self::insertUser($new_chat_member, $date, $chat); |
||
| 727 | $new_chat_member = $new_chat_member->getId(); |
||
| 728 | 6 | } elseif ($left_chat_member) { |
|
| 729 | //Insert the left chat user |
||
| 730 | self::insertUser($left_chat_member, $date, $chat); |
||
| 731 | $left_chat_member = $left_chat_member->getId(); |
||
| 732 | } |
||
| 733 | |||
| 734 | try { |
||
| 735 | 6 | $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '` |
|
| 736 | ( |
||
| 737 | `id`, `user_id`, `chat_id`, `date`, `forward_from`, `forward_from_chat`, |
||
| 738 | `forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`, |
||
| 739 | `photo`, `sticker`, `video`, `voice`, `caption`, `contact`, |
||
| 740 | `location`, `venue`, `new_chat_member`, `left_chat_member`, |
||
| 741 | `new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`, |
||
| 742 | `supergroup_chat_created`, `channel_chat_created`, |
||
| 743 | `migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message` |
||
| 744 | ) |
||
| 745 | VALUES ( |
||
| 746 | :message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, |
||
| 747 | :forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document, |
||
| 748 | :photo, :sticker, :video, :voice, :caption, :contact, |
||
| 749 | :location, :venue, :new_chat_member, :left_chat_member, |
||
| 750 | :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, |
||
| 751 | :supergroup_chat_created, :channel_chat_created, |
||
| 752 | :migrate_from_chat_id, :migrate_to_chat_id, :pinned_message |
||
| 753 | ) |
||
| 754 | 6 | '); |
|
| 755 | |||
| 756 | 6 | $message_id = $message->getMessageId(); |
|
| 757 | 6 | $from_id = $from->getId(); |
|
| 758 | |||
| 759 | 6 | $reply_to_message = $message->getReplyToMessage(); |
|
| 760 | 6 | $reply_to_message_id = null; |
|
| 761 | 6 | if (is_object($reply_to_message)) { |
|
| 762 | $reply_to_message_id = $reply_to_message->getMessageId(); |
||
| 763 | // please notice that, as explained in the documentation, reply_to_message don't contain other |
||
| 764 | // reply_to_message field so recursion deep is 1 |
||
| 765 | self::insertMessageRequest($reply_to_message); |
||
| 766 | } |
||
| 767 | |||
| 768 | 6 | $text = $message->getText(); |
|
| 769 | 6 | $audio = $message->getAudio(); |
|
| 770 | 6 | $document = $message->getDocument(); |
|
| 771 | 6 | $sticker = $message->getSticker(); |
|
| 772 | 6 | $video = $message->getVideo(); |
|
| 773 | 6 | $voice = $message->getVoice(); |
|
| 774 | 6 | $caption = $message->getCaption(); |
|
| 775 | 6 | $contact = $message->getContact(); |
|
| 776 | 6 | $location = $message->getLocation(); |
|
| 777 | 6 | $venue = $message->getVenue(); |
|
| 778 | 6 | $new_chat_title = $message->getNewChatTitle(); |
|
| 779 | 6 | $delete_chat_photo = $message->getDeleteChatPhoto(); |
|
| 780 | 6 | $group_chat_created = $message->getGroupChatCreated(); |
|
| 781 | 6 | $supergroup_chat_created = $message->getSupergroupChatCreated(); |
|
| 782 | 6 | $channel_chat_created = $message->getChannelChatCreated(); |
|
| 783 | 6 | $migrate_from_chat_id = $message->getMigrateFromChatId(); |
|
| 784 | 6 | $migrate_to_chat_id = $message->getMigrateToChatId(); |
|
| 785 | 6 | $pinned_message = $message->getPinnedMessage(); |
|
| 786 | |||
| 787 | 6 | $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_INT); |
|
| 788 | 6 | $sth->bindParam(':message_id', $message_id, PDO::PARAM_INT); |
|
| 789 | 6 | $sth->bindParam(':user_id', $from_id, PDO::PARAM_INT); |
|
| 790 | 6 | $sth->bindParam(':date', $date, PDO::PARAM_STR); |
|
| 791 | 6 | $sth->bindParam(':forward_from', $forward_from, PDO::PARAM_INT); |
|
| 792 | 6 | $sth->bindParam(':forward_from_chat', $forward_from_chat, PDO::PARAM_INT); |
|
| 793 | 6 | $sth->bindParam(':forward_date', $forward_date, PDO::PARAM_STR); |
|
| 794 | |||
| 795 | 6 | $reply_chat_id = null; |
|
| 796 | 6 | if ($reply_to_message_id) { |
|
| 797 | $reply_chat_id = $chat_id; |
||
| 798 | } |
||
| 799 | |||
| 800 | 6 | $var = []; |
|
| 801 | 6 | View Code Duplication | if (is_array($entities)) { |
| 802 | 6 | foreach ($entities as $elm) { |
|
| 803 | $var[] = json_decode($elm, true); |
||
| 804 | } |
||
| 805 | |||
| 806 | 6 | $entities = json_encode($var); |
|
| 807 | } else { |
||
| 808 | $entities = null; |
||
| 809 | } |
||
| 810 | |||
| 811 | 6 | $sth->bindParam(':reply_to_chat', $reply_chat_id, PDO::PARAM_INT); |
|
| 812 | 6 | $sth->bindParam(':reply_to_message', $reply_to_message_id, PDO::PARAM_INT); |
|
| 813 | 6 | $sth->bindParam(':text', $text, PDO::PARAM_STR); |
|
| 814 | 6 | $sth->bindParam(':entities', $entities, PDO::PARAM_STR); |
|
| 815 | 6 | $sth->bindParam(':audio', $audio, PDO::PARAM_STR); |
|
| 816 | 6 | $sth->bindParam(':document', $document, PDO::PARAM_STR); |
|
| 817 | |||
| 818 | 6 | $var = []; |
|
| 819 | 6 | View Code Duplication | if (is_array($photo)) { |
| 820 | 6 | foreach ($photo as $elm) { |
|
| 821 | $var[] = json_decode($elm, true); |
||
| 822 | } |
||
| 823 | |||
| 824 | 6 | $photo = json_encode($var); |
|
| 825 | } else { |
||
| 826 | $photo = ''; |
||
| 827 | } |
||
| 828 | |||
| 829 | 6 | $sth->bindParam(':photo', $photo, PDO::PARAM_STR); |
|
| 830 | 6 | $sth->bindParam(':sticker', $sticker, PDO::PARAM_STR); |
|
| 831 | 6 | $sth->bindParam(':video', $video, PDO::PARAM_STR); |
|
| 832 | 6 | $sth->bindParam(':voice', $voice, PDO::PARAM_STR); |
|
| 833 | 6 | $sth->bindParam(':caption', $caption, PDO::PARAM_STR); |
|
| 834 | 6 | $sth->bindParam(':contact', $contact, PDO::PARAM_STR); |
|
| 835 | 6 | $sth->bindParam(':location', $location, PDO::PARAM_STR); |
|
| 836 | 6 | $sth->bindParam(':venue', $venue, PDO::PARAM_STR); |
|
| 837 | 6 | $sth->bindParam(':new_chat_member', $new_chat_member, PDO::PARAM_INT); |
|
| 838 | 6 | $sth->bindParam(':left_chat_member', $left_chat_member, PDO::PARAM_INT); |
|
| 839 | 6 | $sth->bindParam(':new_chat_title', $new_chat_title, PDO::PARAM_STR); |
|
| 840 | |||
| 841 | //Array of PhotoSize |
||
| 842 | 6 | $var = []; |
|
| 843 | 6 | View Code Duplication | if (is_array($new_chat_photo)) { |
| 844 | 6 | foreach ($new_chat_photo as $elm) { |
|
| 845 | $var[] = json_decode($elm, true); |
||
| 846 | } |
||
| 847 | |||
| 848 | 6 | $new_chat_photo = json_encode($var); |
|
| 849 | } else { |
||
| 850 | $new_chat_photo = ''; |
||
| 851 | } |
||
| 852 | |||
| 853 | 6 | $sth->bindParam(':new_chat_photo', $new_chat_photo, PDO::PARAM_STR); |
|
| 854 | 6 | $sth->bindParam(':delete_chat_photo', $delete_chat_photo, PDO::PARAM_STR); |
|
| 855 | 6 | $sth->bindParam(':group_chat_created', $group_chat_created, PDO::PARAM_STR); |
|
| 856 | 6 | $sth->bindParam(':supergroup_chat_created', $supergroup_chat_created, PDO::PARAM_INT); |
|
| 857 | 6 | $sth->bindParam(':channel_chat_created', $channel_chat_created, PDO::PARAM_INT); |
|
| 858 | 6 | $sth->bindParam(':migrate_from_chat_id', $migrate_from_chat_id, PDO::PARAM_INT); |
|
| 859 | 6 | $sth->bindParam(':migrate_to_chat_id', $migrate_to_chat_id, PDO::PARAM_INT); |
|
| 860 | 6 | $sth->bindParam(':pinned_message', $pinned_message, PDO::PARAM_INT); |
|
| 861 | |||
| 862 | 6 | return $sth->execute(); |
|
| 863 | } catch (PDOException $e) { |
||
| 864 | throw new TelegramException($e->getMessage()); |
||
| 865 | } |
||
| 866 | } |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Insert Edited Message request in db |
||
| 870 | * |
||
| 871 | * @param \Longman\TelegramBot\Entities\Message $edited_message |
||
| 872 | * |
||
| 873 | * @return bool If the insert was successful |
||
| 874 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 875 | */ |
||
| 876 | public static function insertEditedMessageRequest(Message $edited_message) |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Select Group and/or single Chats |
||
| 940 | * |
||
| 941 | * @param bool $select_groups |
||
| 942 | * @param bool $select_super_groups |
||
| 943 | * @param bool $select_users |
||
| 944 | * @param string $date_from |
||
| 945 | * @param string $date_to |
||
| 946 | * @param int $chat_id |
||
| 947 | * @param string $text |
||
| 948 | * |
||
| 949 | * @return array|bool (Selected chats or false if invalid arguments) |
||
| 950 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 951 | */ |
||
| 952 | public static function selectChats( |
||
| 1055 | } |
||
| 1056 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..