Passed
Push — master ( 1e4b3a...3c3497 )
by Ioannes
01:26
created

LoggerFactory::getInstance()   D

Complexity

Conditions 22
Paths 112

Size

Total Lines 71
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 71
rs 4.0666
cc 22
nc 112
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
namespace App\Monolog;
3
4
use Monolog\Formatter\LineFormatter;
5
use Monolog\Handler\StreamHandler;
6
use Monolog\Handler\TelegramBotHandler;
7
use Monolog\Logger;
8
use Monolog\Registry;
9
10
class LoggerFactory {
11
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)) {
0 ignored issues
show
Bug introduced by
It seems like $logDir can also be of type array; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            if(!file_exists(/** @scrutinizer ignore-type */ $logDir)) {
Loading history...
68
                $mode = 0775;
69
                if(defined('BX_DIR_PERMISSIONS') && BX_DIR_PERMISSIONS) {
0 ignored issues
show
Bug introduced by
The constant App\Monolog\BX_DIR_PERMISSIONS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
                    $mode = BX_DIR_PERMISSIONS;
71
                }
72
                mkdir($logDir, $mode, true);
0 ignored issues
show
Bug introduced by
It seems like $logDir can also be of type array; however, parameter $directory of mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                mkdir(/** @scrutinizer ignore-type */ $logDir, $mode, true);
Loading history...
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
        }
84
    }
85
}
86