Completed
Push — master ( 96cd47...32ada6 )
by Russell
01:41
created

SentryRavenHandler::write()   F

Complexity

Conditions 19
Paths 6144

Size

Total Lines 74
Code Lines 44

Duplication

Lines 10
Ratio 13.51 %

Importance

Changes 0
Metric Value
dl 10
loc 74
rs 2.387
c 0
b 0
f 0
cc 19
eloc 44
nc 6144
nop 1

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
/*
4
 * This file is part of the Monolog package.
5
 *
6
 * (c) Jordi Boggiano <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 * 
11
 * 
12
 */
13
14
namespace PhpTek\Sentry\Monolog\Handler;
15
16
use Monolog\Handler\RavenHandler,
17
    Monolog\Logger,
18
    Raven_Client;
19
20
/**
21
 * Subclasses RavenHandler purely to overload its `write()` method.
22
 */
23
class SentryRavenHandler extends RavenHandler
24
{
25
    
26
    /**
27
     * Translates Monolog log levels to Raven log levels.
28
     * 
29
     * Reproduced here because it's a private static.
30
     */
31
    private $logLevels = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
        Logger::DEBUG     => Raven_Client::DEBUG,
33
        Logger::INFO      => Raven_Client::INFO,
34
        Logger::NOTICE    => Raven_Client::INFO,
35
        Logger::WARNING   => Raven_Client::WARNING,
36
        Logger::ERROR     => Raven_Client::ERROR,
37
        Logger::CRITICAL  => Raven_Client::FATAL,
38
        Logger::ALERT     => Raven_Client::FATAL,
39
        Logger::EMERGENCY => Raven_Client::FATAL,
40
    );
41
42
    /**
43
     * Overloads RavenHandler::write() to allow a stacktrace to be passed
44
     * into Sentry. Otherwise, this method is identical to the one included
45
     * in the Monolog package.
46
     * 
47
     * {@inheritdoc}
48
     */
49
    protected function write(array $record)
50
    {
51
        $previousUserContext = false;
52
        $options = [];
53
        $options['level'] = $this->logLevels[$record['level']];
54
        $options['tags'] = [];
55
        
56
        if (!empty($record['extra']['tags'])) {
57
            $options['tags'] = array_merge($options['tags'], $record['extra']['tags']);
58
            unset($record['extra']['tags']);
59
        }
60
        
61 View Code Duplication
        if (!empty($record['context']['tags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            $options['tags'] = array_merge($options['tags'], $record['context']['tags']);
63
            unset($record['context']['tags']);
64
        }
65
        
66
        if (!empty($record['context']['fingerprint'])) {
67
            $options['fingerprint'] = $record['context']['fingerprint'];
68
            unset($record['context']['fingerprint']);
69
        }
70
        
71 View Code Duplication
        if (!empty($record['context']['logger'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            $options['logger'] = $record['context']['logger'];
73
            unset($record['context']['logger']);
74
        } else {
75
            $options['logger'] = $record['channel'];
76
        }
77
        
78
        foreach ($this->getExtraParameters() as $key) {
79
            foreach (['extra', 'context'] as $source) {
80
                if (!empty($record[$source][$key])) {
81
                    $options[$key] = $record[$source][$key];
82
                    
83
                    unset($record[$source][$key]);
84
                }
85
            }
86
        }
87
        
88
        if (!empty($record['context'])) {
89
            $options['extra']['context'] = $record['context'];
90
            
91
            if (!empty($record['context']['user'])) {
92
                $previousUserContext = $this->ravenClient->context->user;
93
                $this->ravenClient->user_context($record['context']['user']);
94
                unset($options['extra']['context']['user']);
95
            }
96
        }
97
        
98
        if (!empty($record['extra'])) {
99
            $options['extra']['extra'] = $record['extra'];
100
        }
101
        
102
        // New for phptek/sentry
103
        $stack = false;
104
        if (!empty($record['stack'])) {
105
            $stack = (bool) $record['stack'];
106
        }
107
108
        if (!empty($this->release) && !isset($options['release'])) {
0 ignored issues
show
Bug introduced by
The property release cannot be accessed from this context as it is declared private in class Monolog\Handler\RavenHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
109
            $options['release'] = $this->release;
0 ignored issues
show
Bug introduced by
The property release cannot be accessed from this context as it is declared private in class Monolog\Handler\RavenHandler.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
110
        }
111
112
        if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) {
113
            $options['extra']['message'] = $record['formatted'];
114
            $this->ravenClient->captureException($record['context']['exception'], $options);
115
        } else {
116
            $this->ravenClient->captureMessage($record['formatted'], [], $options, $stack);
117
        }
118
119
        if ($previousUserContext !== false) {
120
            $this->ravenClient->user_context($previousUserContext);
121
        }
122
    }
123
}
124