Completed
Push — master ( 98713e...0c7d4a )
by Russell
01:56
created

RavenClient::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class: RavenClient.
5
 *
6
 * @author  Russell Michell 2017 <[email protected]>
7
 * @package phptek/sentry
8
 */
9
10
namespace phptek\Sentry\Adaptor;
11
12
use phptek\Sentry\Adaptor\SentryClientAdaptor;
13
use phptek\Sentry\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
     * @var Raven_Client
34
     */
35
    protected $client;
36
    
37
    /**
38
     * A mapping of log-level values between Zend_Log => Raven_Client
39
     * 
40
     * @var array
41
     */
42
    protected $logLevels = [
43
        'NOTICE'    => \Raven_Client::INFO,
44
        'WARN'      => \Raven_Client::WARNING,
45
        'ERR'       => \Raven_Client::ERROR,
46
        'EMERG'     => \Raven_Client::FATAL
47
    ];
48
    
49
    /**
50
     * @throws SentryLogWriterException
51
     * @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...
52
     */
53
    public function __construct()
54
    {        
55
        if (!$dsn = $this->getOpts('dsn')) {
56
            $msg = sprintf("%s requires a DSN string to be set in config.", __CLASS__);
57
            throw new SentryLogWriterException($msg);
58
        }
59
        
60
        $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<phptek\Sentry\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...
61
        
62
        // Installs all available PHP error handlers when set
63
        if ($this->config()->install === true) {
64
            $this->client->install();
65
        }
66
    }
67
68
    /**
69
     * Used in unit tests.
70
     *
71
     * @return Raven_Client
72
     */
73
    public function getSDK()
74
    {
75
        return $this->client;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function setData($field, $data)
82
    {
83
        switch($field) {
84
        case 'env':
85
            $this->client->setEnvironment($data);
86
            break;
87
        case 'tags':
88
            $this->client->tags_context($data);
89
            break;
90
        case 'user':
91
            $this->client->user_context($data);
92
            break;
93
        case 'extra':
94
            $this->client->extra_context($data);
95
            break;
96
        default:
97
            $msg = sprintf('Unknown field %s passed to %s.', $field, __FUNCTION__);
98
            throw new SentryLogWriterException($msg);
99
        }
100
    }
101
    
102
    /**
103
     * Simple accessor for data set to / on the client.
104
     * 
105
     * @return array
106
     */
107
    public function getData()
108
    {
109
        return [
110
            'env'   => $this->client->getEnvironment(),
111
            'tags'  => $this->client->context->tags,
112
            'user'  => $this->client->context->user,
113
            'extra' => $this->client->context->extra,
114
        ];
115
    }
116
    
117
    /**
118
     * @inheritdoc
119
     */
120
    public function getLevel($level)
121
    {
122
        return isset($this->client->logLevels[$level]) ?
123
            $this->client->logLevels[$level] : 
124
            $this->client->logLevels[self::$default_error_level];
125
    }
126
    
127
    /**
128
     * @inheritdoc
129
     */
130
    public function send($message, $extras = [], $data, $trace)
131
    {
132
        // Raven_Client::captureMessage() returns an ID to identify each message
133
        $eventId = $this->client->captureMessage($message, $extras, $data, $trace);
134
        
135
        return $eventId ?: false;
136
    }
137
138
}
139