Passed
Push — master ( b3acba...0a9519 )
by Ioannes
01:30
created

ExceptionHandlerLog   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 22
c 3
b 0
f 0
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B write() 0 23 7
A logInnerException() 0 3 1
A initialize() 0 5 3
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']) && is_callable($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
                    $context = call_user_func($this->context, $exception);
0 ignored issues
show
Unused Code introduced by
The assignment to $context is dead and can be removed.
Loading history...
42
                } catch(\Exception $e) {
43
                    self::logInnerException(new \Exception('Can not call ' . $this->context));
0 ignored issues
show
Bug introduced by
Are you sure $this->context of type callable can be used in concatenation? ( Ignorable by Annotation )

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

43
                    self::logInnerException(new \Exception('Can not call ' . /** @scrutinizer ignore-type */ $this->context));
Loading history...
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
                $this->context['source'] = self::logTypeToString($logType);
53
                $log->telegram(LogLevel::CRITICAL, $exception, $this->context);
54
            }
55
56
        } catch(\Exception $e) {
57
            self::logInnerException($e);
58
        }
59
    }
60
61
    /**
62
     * @param \Exception $exception
63
     */
64
    protected static function logInnerException(\Exception $exception)
65
    {
66
        Debug::writeToFile((string) $exception, "", "inner_error.log");
67
    }
68
}