| Conditions | 22 |
| Paths | 17 |
| Total Lines | 77 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 14 | public static function getInstance($channel, $context) { |
||
| 15 | |||
| 16 | $key = md5(json_encode([$channel, $context])); |
||
| 17 | if(Registry::hasLogger($key)) { |
||
| 18 | return Registry::getInstance($key); |
||
| 19 | } |
||
| 20 | |||
| 21 | if($channel == self::TELEGRAM_CHANNEL) { |
||
| 22 | |||
| 23 | if(isset($_ENV['APP_LOG_TELEGRAM'])) { |
||
| 24 | $APP_LOG_TELEGRAM = trim(strtolower($_ENV['APP_LOG_TELEGRAM'])); |
||
| 25 | if($APP_LOG_TELEGRAM !== 'on' && $APP_LOG_TELEGRAM !== 'true' && $APP_LOG_TELEGRAM !== '1') { |
||
| 26 | return null; |
||
| 27 | } |
||
| 28 | } else { |
||
| 29 | return null; |
||
| 30 | } |
||
| 31 | |||
| 32 | if(!isset($_ENV['APP_LOG_TELEGRAM_KEY']) || empty($_ENV['APP_LOG_TELEGRAM_KEY'])) { |
||
| 33 | return null; |
||
| 34 | } |
||
| 35 | if(!isset($_ENV['APP_LOG_TELEGRAM_CHATID']) || empty($_ENV['APP_LOG_TELEGRAM_CHATID'])) { |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | |||
| 39 | if(!class_exists('\Monolog\Handler\TelegramBotHandler')) { |
||
| 40 | return null; |
||
| 41 | } |
||
| 42 | |||
| 43 | $sender = new TelegramBotHandler($_ENV['APP_LOG_TELEGRAM_KEY'], $_ENV['APP_LOG_TELEGRAM_CHATID']); |
||
| 44 | |||
| 45 | if(isset($context['parse_mode']) && !empty($context['parse_mode'])) { |
||
| 46 | $sender->setParseMode($context['parse_mode']); |
||
| 47 | unset($context['parse_mode']); |
||
| 48 | } else { |
||
| 49 | $sender->setParseMode('HTML'); |
||
| 50 | } |
||
| 51 | if(isset($context['disable_notification']) && $context['disable_notification'] === true) { |
||
| 52 | $sender->disableNotification(true); |
||
| 53 | unset($context['disable_notification']); |
||
| 54 | } |
||
| 55 | if(isset($context['disable_preview']) && $context['disable_preview'] === false) { |
||
| 56 | $sender->disableWebPagePreview(false); |
||
| 57 | unset($context['disable_preview']); |
||
| 58 | } else { |
||
| 59 | $sender->disableWebPagePreview(true); |
||
| 60 | } |
||
| 61 | |||
| 62 | $sender->setFormatter(new LineFormatter("%level_name%! %message%")); |
||
| 63 | |||
| 64 | $telegramLogger = new Logger(self::TELEGRAM_CHANNEL); |
||
| 65 | $telegramLogger->pushHandler($sender); |
||
| 66 | Registry::addLogger($telegramLogger, $key, true); |
||
| 67 | |||
| 68 | return $telegramLogger; |
||
| 69 | |||
| 70 | } else { |
||
| 71 | |||
| 72 | $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/'); |
||
| 73 | $logPath .= $channel . '/' . date('Y-m-d') . '.log'; |
||
| 74 | $logDir = pathinfo($logPath, PATHINFO_DIRNAME); |
||
| 75 | if(!file_exists($logDir)) { |
||
|
|
|||
| 76 | $mode = 0775; |
||
| 77 | if(defined('BX_DIR_PERMISSIONS') && BX_DIR_PERMISSIONS) { |
||
| 78 | $mode = BX_DIR_PERMISSIONS; |
||
| 79 | } |
||
| 80 | mkdir($logDir, $mode, true); |
||
| 81 | } |
||
| 82 | |||
| 83 | $handler = new StreamHandler($logPath); |
||
| 84 | $handler->setFormatter(new ArrayFormatter()); |
||
| 85 | |||
| 86 | $logger = new Logger($channel); |
||
| 87 | $logger->pushHandler($handler); |
||
| 88 | Registry::addLogger($logger, $key, true); |
||
| 89 | |||
| 90 | return $logger; |
||
| 91 | } |
||
| 94 |