Completed
Push — master ( 7aba0b...5bba2a )
by Russell
08:42
created

RavenClient::getLevel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * Class: RavenClient.
5
 *
6
 * @author  Russell Michell 2017 <[email protected]>
7
 * @package phptek/sentry
8
 */
9
10
namespace SilverStripeSentry\Adaptor;
11
12
use SilverStripeSentry\Adaptor\SentryClientAdaptor;
13
use SilverStripeSentry\Exception\SentryLogWriterException;
14
15
/**
16
 * The RavenClient class simply acts as a bridge between the Raven PHP SDK and
17
 * the SentryLogWriter class itself. Any {@link SentryClientAdaptor} subclass
18
 * should be able to be swapped-out and used at any point.
19
 */
20
21
class RavenClient extends SentryClientAdaptor
22
{
23
    
24
    /**
25
     * It's an ERROR unless proven otherwise!
26
     * 
27
     * @var    string
28
     * @config
29
     */
30
    private static $default_error_level = 'ERROR';
31
    
32
    /**
33
     *
34
     * @var Raven_Client
35
     */
36
    protected $client;
37
    
38
    /**
39
     * A mapping of log-level values between Zend_Log => Raven_Client
40
     * 
41
     * @var array
42
     */
43
    protected $logLevels = [
44
        'NOTICE'    => \Raven_Client::INFO,
45
        'WARN'      => \Raven_Client::WARNING,
46
        'ERR'       => \Raven_Client::ERROR,
47
        'EMERG'     => \Raven_Client::FATAL
48
    ];
49
    
50
    /**
51
     * @throws SentryLogWriterException
52
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
53
     */
54
    public function __construct()
55
    {        
56
        if (!$dsn = $this->getOpts('dsn')) {
57
            $msg = sprintf("%s requires a DSN string to be set in config.", __CLASS__);
58
            throw new SentryLogWriterException($msg);
59
        }
60
        
61
        $this->client = new \Raven_Client($dsn);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Raven_Client($dsn) of type object<Raven_Client> is incompatible with the declared type object<SilverStripeSentry\Adaptor\Raven_Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
        
63
        // Installs all available PHP error handlers when set
64
        if ($this->config()->install === true) {
65
            $this->client->install();
66
        }
67
    }
68
69
    /**
70
     * Used in unit tests.
71
     *
72
     * @return Raven_Client
73
     */
74
    public function getSDK()
75
    {
76
        return $this->client;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function setData($field, $data)
83
    {
84
        switch($field) {
85
            case 'env':
86
                $this->client->setEnvironment($data);
87
                break;
88
            case 'tags':
89
                $this->client->tags_context($data);
90
                break;
91
            case 'user':
92
                $this->client->user_context($data);
93
                break;
94
            case 'extra':
95
                $this->client->extra_context($data);
96
                break;
97
            default:
98
                $msg = sprintf('Unknown field %s passed to %s.', $field, __FUNCTION__);
99
                throw new SentryLogWriterException($msg);
100
        }
101
    }
102
    
103
    /**
104
     * @inheritdoc
105
     */
106
    public function getLevel($level)
107
    {
108
        return isset($this->client->logLevels[$level]) ?
109
            $this->client->logLevels[$level] : 
110
            $this->client->logLevels[self::$default_error_level];
111
    }
112
    
113
    /**
114
     * @inheritdoc
115
     */
116
    public function send($message, $extras = [], $data, $trace)
117
    {
118
        // Raven_Client::captureMessage() returns an ID to identify each message
119
        $eventId = $this->client->captureMessage($message, $extras, $data, $trace);
120
        
121
        return $eventId ?: false;
122
    }
123
124
}
125