Completed
Push — master ( 40fcfc...22704e )
by Michał
04:16
created

Raven::write()   F

Complexity

Conditions 19
Paths 6144

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 42
nc 6144
nop 1
dl 0
loc 57
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
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\Handler;
6
7
use Monolog\Handler\RavenHandler;
8
9
class Raven extends RavenHandler
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function handleBatch(array $records): void
15
    {
16
        $level = $this->level;
17
18
        // filter records based on their level
19
        $records = array_filter(
20
            $records,
21
            function ($record) use ($level) {
22
                return $record['level'] >= $level;
23
            }
24
        );
25
26
        if (!$records) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $records of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
            return;
28
        }
29
30
        // the record with the highest severity is the "main" one
31
        $record = array_reduce(
32
            $records,
33
            function ($highest, $record) {
34
                if ($record['level'] > $highest['level']) {
35
                    return $record;
36
                }
37
38
                return $highest;
39
            }
40
        );
41
42
        // the other ones are added as a context item
43
        $logs = [];
44
        foreach ($records as $r) {
45
            $log = $this->processRecord($r);
46
47
            $crumb = [
48
                'category' => $log['channel'],
49
                'message' => $log['message'],
50
                'level' => strtolower($log['level_name']),
51
                'timestamp' => (float)$log['datetime']->format('U.u'),
52
            ];
53
54
            if (array_key_exists('context', $log)) {
55
                if ($log['channel'] === 'request' && array_key_exists('route_parameters', $log['context'])) {
56
                    $crumb['data']['route'] = $log['context']['route_parameters']['_route'];
57
                    $crumb['data']['controller'] = $log['context']['route_parameters']['_controller'];
58
                    $crumb['data']['uri'] = $log['context']['request_uri'];
59
                }
60
61
                if ($log['channel'] === 'security' && array_key_exists('user', $log['context'])) {
62
                    $crumb['data']['user'] = $log['context']['user']['username'];
63
                }
64
            }
65
66
            $this->ravenClient->breadcrumbs->record($crumb);
67
        }
68
69
        if ($logs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $logs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
$logs is an empty array, thus is always false.
Loading history...
70
            $record['context']['logs'] = (string)$this->getBatchFormatter()->formatBatch($logs);
71
        }
72
73
        $this->handle($record);
74
    }
75
    
76
    /**
77
     * Fix until https://github.com/Seldaek/monolog/pull/1180 will be merged
78
     * {@inheritdoc}
79
     */
80
    protected function write(array $record)
81
    {
82
        $previousUserContext = false;
83
        $options = array();
84
        $options['level'] = $this->logLevels[$record['level']];
0 ignored issues
show
Bug introduced by
The property logLevels is declared private in Monolog\Handler\RavenHandler and cannot be accessed from this context.
Loading history...
85
        $options['tags'] = array();
86
        if (!empty($record['extra']['tags'])) {
87
            $options['tags'] = array_merge($options['tags'], $record['extra']['tags']);
88
            unset($record['extra']['tags']);
89
        }
90
        if (!empty($record['context']['tags'])) {
91
            $options['tags'] = array_merge($options['tags'], $record['context']['tags']);
92
            unset($record['context']['tags']);
93
        }
94
        if (!empty($record['context']['fingerprint'])) {
95
            $options['fingerprint'] = $record['context']['fingerprint'];
96
            unset($record['context']['fingerprint']);
97
        }
98
        if (!empty($record['context']['logger'])) {
99
            $options['logger'] = $record['context']['logger'];
100
            unset($record['context']['logger']);
101
        } else {
102
            $options['logger'] = $record['channel'];
103
        }
104
        foreach ($this->getExtraParameters() as $key) {
105
            foreach (array('extra', 'context') as $source) {
106
                if (!empty($record[$source][$key])) {
107
                    $options[$key] = $record[$source][$key];
108
                    unset($record[$source][$key]);
109
                }
110
            }
111
        }
112
        if (!empty($record['context'])) {
113
            $options['extra']['context'] = $record['context'];
114
            if (!empty($record['context']['user'])) {
115
                $previousUserContext = $this->ravenClient->context->user;
116
                $this->ravenClient->user_context($record['context']['user']);
117
                unset($options['extra']['context']['user']);
118
            }
119
        }
120
        if (!empty($record['contexts'])) {
121
            $options['contexts'] = $record['contexts'];
122
        }
123
        if (!empty($record['extra'])) {
124
            $options['extra']['extra'] = $record['extra'];
125
        }
126
        if (!empty($this->release) && !isset($options['release'])) {
0 ignored issues
show
Bug introduced by
The property release is declared private in Monolog\Handler\RavenHandler and cannot be accessed from this context.
Loading history...
127
            $options['release'] = $this->release;
128
        }
129
        if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) {
130
            $options['message'] = $record['formatted'];
131
            $this->ravenClient->captureException($record['context']['exception'], $options);
132
        } else {
133
            $this->ravenClient->captureMessage($record['formatted'], array(), $options);
134
        }
135
        if ($previousUserContext !== false) {
136
            $this->ravenClient->user_context($previousUserContext);
137
        }
138
    }
139
}
140