Passed
Push — master ( acce6f...212374 )
by Charles
03:08
created

PsrTarget::export()   F

Complexity

Conditions 19
Paths 1441

Size

Total Lines 69
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 42
nc 1441
nop 0
dl 0
loc 69
rs 0.3499
c 0
b 0
f 0

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
3
namespace yrc\components\log;
4
5
use Psr\Log\LoggerAwareInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerAwareInterface 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 Psr\Log\LoggerAwareTrait;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerAwareTrait 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 Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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...
8
use Psr\Log\LogLevel;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LogLevel 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...
9
use yii\base\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type yii\base\InvalidConfigException 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...
10
use yii\helpers\VarDumper;
0 ignored issues
show
Bug introduced by
The type yii\helpers\VarDumper 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
use yii\log\Logger;
0 ignored issues
show
Bug introduced by
The type yii\log\Logger 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...
12
use yii\log\Target;
0 ignored issues
show
Bug introduced by
The type yii\log\Target 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...
13
14
final class PsrTarget extends Target
15
{
16
    use LoggerAwareTrait;
17
18
    private $_psrLevels = [
19
        Logger::LEVEL_ERROR => LogLevel::ERROR,
20
        Logger::LEVEL_WARNING => LogLevel::WARNING,
21
        Logger::LEVEL_INFO => LogLevel::INFO,
22
        Logger::LEVEL_TRACE => LogLevel::DEBUG,
23
        Logger::LEVEL_PROFILE => LogLevel::DEBUG,
24
        Logger::LEVEL_PROFILE_BEGIN => LogLevel::DEBUG,
25
        Logger::LEVEL_PROFILE_END => LogLevel::DEBUG,
26
    ];
27
28
    /**
29
     * @return LoggerInterface
30
     * @throws InvalidConfigException
31
     */
32
    public function getLogger()
33
    {
34
        if ($this->logger === null) {
35
            throw new InvalidConfigException('Logger should be configured with Psr\Log\LoggerInterface.');
36
        }
37
        return $this->logger;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function export()
44
    {
45
        foreach ($this->messages as $message) {
46
            $context = [];
47
            if (isset($message[4])) {
48
                $context['trace'] = $message[4];
49
            }
50
51
            if (isset($message[5])) {
52
                $context['memory'] = $message[5];
53
            }
54
55
            if (isset($message[2])) {
56
                $context['category'] = $message[2];
57
            }
58
59
            $text = $message[0];
60
61
            if (!is_string($text)) {
62
                // exceptions may not be serializable if in the call stack somewhere is a Closure
63
                if ($text instanceof \Throwable || $text instanceof \Exception) {
64
                    $text = (string)$text;
65
                } elseif (\is_array($text)) {
66
                    $ctx = $text;
67
                    if (isset($ctx['message'])) {
68
                        $text = $ctx['message'];
69
                        unset($ctx['message']);
70
71
                        if (isset($ctx['exception'])) {
72
                            $e = $ctx['exception'];
73
                            unset($ctx['exception']);
74
                            $context['exception'] = [
75
                                'message' => $e->getMessage(),
76
                                'line' => $e->getLine(),
77
                                'file' => $e->getFile(),
78
                                'code' => $e->getCode(),
79
                                'trace' => $e->getTrace()
80
                            ];
81
                        }
82
                        foreach ($ctx as $k => $v) {
83
                            $context[$k] = $v;
84
                        }
85
                    } else {
86
                        $text = VarDumper::export($text);
87
                    }
88
                } else {
89
                    $text = VarDumper::export($text);
90
                }
91
            }
92
            
93
            // If the user_id is not passed, dynamically set it from the user identity object
94
            if (!isset($context['user_id'])) {
95
                $context['user_id'] = Yii::$app->has('user') && Yii::$app->user->id != null ? Yii::$app->user->id : 'system';
0 ignored issues
show
Bug introduced by
The type yrc\components\log\Yii 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...
96
            }
97
98
            // If the user_id of the event isn't the system, pre-load the policy number
99
            if (!\in_array($context['user_id'], ['system', null])) {
100
                if (Yii::$app->has('user') && Yii::$app->user->id) {
101
                    $model = Yii::$app->user->identity;
0 ignored issues
show
Unused Code introduced by
The assignment to $model is dead and can be removed.
Loading history...
102
                } else {
103
                    $model = Yii::$app->user->identityClass::find()->where(['id' => $context['user_id']])->one();
104
                }
105
            }
106
            
107
            if (!isset($context['timestamp'])) {
108
                $context['timestamp'] = \microtime(true);
109
            }
110
111
            $this->getLogger()->log($this->_psrLevels[$message[1]], $text, $context);
112
        }
113
    }
114
}
115