| Conditions | 22 |
| Paths | 112 |
| Total Lines | 71 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 12 | public static function getInstance($channel, $context) { |
||
| 13 | |||
| 14 | if(Registry::hasLogger($channel)) { |
||
| 15 | return Registry::getInstance($channel); |
||
| 16 | } |
||
| 17 | |||
| 18 | if($channel == 'telegram-messenger') { |
||
| 19 | |||
| 20 | $isEnabled = false; |
||
| 21 | if(isset($_ENV['APP_LOG_TELEGRAM'])) { |
||
| 22 | $APP_LOG_TELEGRAM = trim(strtolower($_ENV['APP_LOG_TELEGRAM'])); |
||
| 23 | if($APP_LOG_TELEGRAM === 'on' || $APP_LOG_TELEGRAM === 'true' || $APP_LOG_TELEGRAM === '1') { |
||
| 24 | $isEnabled = true; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | if(!isset($_ENV['APP_LOG_TELEGRAM_KEY']) || empty($_ENV['APP_LOG_TELEGRAM_KEY'])) { |
||
| 29 | $isEnabled = false; |
||
| 30 | } |
||
| 31 | if(!isset($_ENV['APP_LOG_TELEGRAM_CHANNEL']) || empty($_ENV['APP_LOG_TELEGRAM_CHANNEL'])) { |
||
| 32 | $isEnabled = false; |
||
| 33 | } |
||
| 34 | |||
| 35 | if(!$isEnabled) { |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | |||
| 39 | $sender = new TelegramBotHandler($_ENV['APP_LOG_TELEGRAM_KEY'], $_ENV['APP_LOG_TELEGRAM_CHANNEL']); |
||
| 40 | |||
| 41 | if(isset($context['parse_mode']) && !empty($context['parse_mode'])) { |
||
| 42 | $sender->setParseMode($context['parse_mode']); |
||
| 43 | unset($context['parse_mode']); |
||
| 44 | } |
||
| 45 | if(isset($context['disable_notification']) && $context['disable_notification'] === true) { |
||
| 46 | $sender->disableNotification(true); |
||
| 47 | unset($context['disable_notification']); |
||
| 48 | } |
||
| 49 | if(isset($context['disable_preview']) && $context['disable_preview'] === true) { |
||
| 50 | $sender->disableWebPagePreview(true); |
||
| 51 | unset($context['disable_preview']); |
||
| 52 | } |
||
| 53 | |||
| 54 | $sender->setFormatter(new LineFormatter("[%datetime%] %level_name%: %message%")); |
||
| 55 | |||
| 56 | $telegramLogger = new Logger('Telegram'); |
||
| 57 | $telegramLogger->pushHandler($sender); |
||
| 58 | Registry::addLogger($telegramLogger, $channel, true); |
||
| 59 | |||
| 60 | return $telegramLogger; |
||
| 61 | |||
| 62 | } else { |
||
| 63 | |||
| 64 | $logPath = $_SERVER['DOCUMENT_ROOT'] . ($_ENV['APP_LOG_FOLDER'] ?: '/log/'); |
||
| 65 | $logPath .= $channel . '/' . date('Y-m-d') . '.log'; |
||
| 66 | $logDir = pathinfo($logPath, PATHINFO_DIRNAME); |
||
| 67 | if(!file_exists($logDir)) { |
||
|
|
|||
| 68 | $mode = 0775; |
||
| 69 | if(defined('BX_DIR_PERMISSIONS') && BX_DIR_PERMISSIONS) { |
||
| 70 | $mode = BX_DIR_PERMISSIONS; |
||
| 71 | } |
||
| 72 | mkdir($logDir, $mode, true); |
||
| 73 | } |
||
| 74 | |||
| 75 | $handler = new StreamHandler($logPath); |
||
| 76 | $handler->setFormatter(new ArrayFormatter()); |
||
| 77 | |||
| 78 | $logger = new Logger($channel); |
||
| 79 | $logger->pushHandler($handler); |
||
| 80 | Registry::addLogger($logger, $channel, true); |
||
| 81 | |||
| 82 | return $logger; |
||
| 83 | } |
||
| 86 |