ExceptionHandlerLog::write()   C
last analyzed

Complexity

Conditions 11
Paths 232

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 26
c 5
b 0
f 0
dl 0
loc 37
rs 6.0833
cc 11
nc 232
nop 2

How to fix   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 App\Log;
5
use Bitrix\Main\Diag\Debug;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Diag\Debug was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Bitrix\Main\Diag\ExceptionHandler;
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Diag\ExceptionHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Monolog\Logger;
8
use Psr\Log\LogLevel;
9
10
class ExceptionHandlerLog extends \Bitrix\Main\Diag\ExceptionHandlerLog {
0 ignored issues
show
Bug introduced by
The type Bitrix\Main\Diag\ExceptionHandlerLog was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
    /**
13
     * @var Logger
14
     */
15
	protected $logger;
16
	/**
17
     * @var callable
18
     */
19
	protected $context;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function initialize(array $options)
25
    {
26
        if (isset($options['context']))
27
        {
28
            $this->context = $options['context'];
29
        }
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function write($exception, $logType)
36
    {
37
        try {
38
            $log = new Log();
39
            if(is_callable($this->context)) {
40
                try {
41
                    $this->context = call_user_func($this->context, $exception);
42
                } catch(\Exception $e) {
43
                    self::logInnerException(new \Exception('Can not call ' . (string) $this->context));
44
                }
45
            }
46
            if(!is_array($this->context)) {
47
                $this->context = (!empty($this->context) ? [$this->context] : []);
48
            }
49
            if($logType == \Bitrix\Main\Diag\ExceptionHandlerLog::LOW_PRIORITY_ERROR) {
50
                $log->error($exception, $this->context);
51
            } else {
52
                $telegramBlocked = false;
53
                if($_ENV['APP_LOG_NO_TELEGRAM']) {
54
                    $blockStrings = explode('|', $_ENV['APP_LOG_NO_TELEGRAM']);
55
                    foreach($blockStrings as $blockString) {
56
                        if(stripos($exception, $blockString) !== false) {
57
                            $telegramBlocked = true;
58
                            break;
59
                        }
60
                    }
61
                }
62
                if($telegramBlocked) {
63
                    $log->error($exception, $this->context);
64
                } else {
65
                    $this->context['source'] = self::logTypeToString($logType);
66
                    $log->telegram(LogLevel::CRITICAL, $exception, $this->context);
67
                }
68
            }
69
70
        } catch(\Exception $e) {
71
            self::logInnerException($e);
72
        }
73
    }
74
75
    /**
76
     * @param \Exception $exception
77
     */
78
    protected static function logInnerException(\Exception $exception)
79
    {
80
        Debug::writeToFile((string) $exception, "", "inner_error.log");
81
    }
82
}