Test Failed
Pull Request — master (#12)
by wujunze
03:09
created

SeaslogConfig::log()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 22
c 1
b 0
f 1
nc 32
nop 3
dl 0
loc 29
rs 7.3166

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
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger;
5
6
use Exception;
7
use Seaslog;
0 ignored issues
show
Bug introduced by
The type Seaslog 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
9
/**
10
 * Class SeaslogConfig
11
 * @package Seasx\SeasLogger
12
 */
13
class SeaslogConfig extends AbstractConfig
14
{
15
    /**
16
     * SeaslogConfig constructor.
17
     * @param array $target
18
     * @param array $configs
19
     */
20
    public function __construct(array $target, array $configs = [])
21
    {
22
        $this->template = explode($this->split, ini_get('seaslog.default_template'));
23
        ini_set('seaslog.recall_depth', (string)$this->recall_depth);
24
        parent::__construct($target, $configs);
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function getBuffer(): array
31
    {
32
        $buffer = Seaslog::getBuffer();
33
        return $buffer !== false ? $buffer : [];
34
    }
35
36
    public function getDatetimeFormat(): string
37
    {
38
        return SeasLog::getDatetimeFormat();
39
    }
40
41
    public function setDatetimeFormat(string $format): bool
42
    {
43
        return SeasLog::setDatetimeFormat($format);
44
    }
45
46
47
    /**
48
     * @param string $level
49
     * @param string $message
50
     * @param array $context
51
     * @throws Exception
52
     */
53
    public function log(string $level, string $message, array $context = []): void
54
    {
55
        $template = $this->getUserTemplate();
56
        $module = ArrayHelper::remove($context, 'module');
57
        if ($module !== null) {
58
            Seaslog::setLogger($this->appName . '_' . $module);
59
        }
60
        isset($template['%Q']) && Seaslog::setRequestID($template['%Q']);
61
        foreach (array_filter([
62
            SEASLOG_REQUEST_VARIABLE_DOMAIN_PORT => isset($template['%D']) ? $template['%D'] : null,
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG...ST_VARIABLE_DOMAIN_PORT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
            SEASLOG_REQUEST_VARIABLE_REQUEST_URI => isset($template['%R']) ? $template['%R'] : null,
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG...ST_VARIABLE_REQUEST_URI was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
            SEASLOG_REQUEST_VARIABLE_REQUEST_METHOD => isset($template['%m']) ? $template['%m'] : null,
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG...VARIABLE_REQUEST_METHOD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
            SEASLOG_REQUEST_VARIABLE_CLIENT_IP => isset($template['%I']) ? $template['%I'] : null
0 ignored issues
show
Bug introduced by
The constant Seasx\SeasLogger\SEASLOG...UEST_VARIABLE_CLIENT_IP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
        ]) as $key => $value) {
67
            Seaslog::setRequestVariable($key, $value);
68
        }
69
        if (!empty($template = ArrayHelper::remove($context, 'template', []) ?? ArrayHelper::remove($template, '%A',
70
                []))) {
71
            switch ($this->customerType) {
72
                case AbstractConfig::TYPE_JSON:
73
                    $template = json_encode($template, JSON_UNESCAPED_UNICODE);
74
                    break;
75
                case AbstractConfig::TYPE_FIELD:
76
                    $template = implode($this->split, $template);
77
            }
78
            $message = $template . $this->split . $message;
79
        }
80
        Seaslog::$level($message);
81
        $this->flush();
82
    }
83
84
    /**
85
     * @param bool $flush
86
     * @throws Exception
87
     */
88
    public function flush(bool $flush = false): void
89
    {
90
        if (method_exists('Seaslog', 'getBufferCount')) {
91
            if (($flush || ($total = Seaslog::getBufferCount()) >= $this->bufferSize) && ($buffer = Seaslog::getBuffer()) !== false) {
0 ignored issues
show
Unused Code introduced by
The assignment to $total is dead and can be removed.
Loading history...
92
                Seaslog::flushBuffer(0);
93
                foreach ($this->targetList as $index => $target) {
94
                    rgo(function () use ($target, $buffer, $flush) {
0 ignored issues
show
Unused Code introduced by
The import $flush is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
95
                        $target->export($buffer);
96
                    });
97
                }
98
                unset($buffer);
99
            }
100
        } elseif ($flush) {
101
            Seaslog::flushBuffer();
102
        }
103
    }
104
}