Completed
Push — master ( 83b55d...472d84 )
by Justin
03:47 queued 36s
created

Rollbar::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php namespace Rollbar;
2
3
use Rollbar\Payload\Level;
4
use Rollbar\Handlers\FatalHandler;
5
use Rollbar\Handlers\ErrorHandler;
6
use Rollbar\Handlers\ExceptionHandler;
7
8
class Rollbar
9
{
10
    /**
11
     * @var RollbarLogger
12
     */
13
    private static $logger = null;
14
    private static $fatalHandler = null;
15
    private static $errorHandler = null;
16
    private static $exceptionHandler = null;
17
18
    public static function init(
19
        $configOrLogger,
20
        $handleException = true,
21
        $handleError = true,
22
        $handleFatal = true
23
    ) {
24
        $setupHandlers = is_null(self::$logger);
25
26
        self::setLogger($configOrLogger);
27
28
        if ($setupHandlers) {
29
            if ($handleException) {
30
                self::setupExceptionHandling();
31
            }
32
            if ($handleError) {
33
                self::setupErrorHandling();
34
            }
35
            if ($handleFatal) {
36
                self::setupFatalHandling();
37
            }
38
            self::setupBatchHandling();
39
        }
40
    }
41
42
    private static function setLogger($configOrLogger)
43
    {
44
        if ($configOrLogger instanceof RollbarLogger) {
45
            $logger = $configOrLogger;
46
        }
47
48
        // Replacing the logger rather than configuring the existing logger breaks BC
49
        if (self::$logger && !isset($logger)) {
50
            self::$logger->configure($configOrLogger);
51
            return;
52
        }
53
54
        self::$logger = isset($logger) ? $logger : new RollbarLogger($configOrLogger);
55
    }
56
    
57
    public static function enable()
58
    {
59
        return self::logger()->enable();
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::logger()->enable() targeting Rollbar\RollbarLogger::enable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
    }
61
    
62
    public static function disable()
63
    {
64
        return self::logger()->disable();
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::logger()->disable() targeting Rollbar\RollbarLogger::disable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
65
    }
66
    
67
    public static function enabled()
68
    {
69
        return self::logger()->enabled();
70
    }
71
    
72
    public static function disabled()
73
    {
74
        return self::logger()->disabled();
75
    }
76
77
    public static function logger()
78
    {
79
        return self::$logger;
80
    }
81
82
    public static function scope($config)
83
    {
84
        if (is_null(self::$logger)) {
85
            return new RollbarLogger($config);
86
        }
87
        return self::$logger->scope($config);
88
    }
89
90
    public static function log($level, $toLog, $extra = array(), $isUncaught = false)
91
    {
92
        if (is_null(self::$logger)) {
93
            return self::getNotInitializedResponse();
94
        }
95
        return self::$logger->log($level, $toLog, (array)$extra, $isUncaught);
96
    }
97
    
98
    public static function debug($toLog, $extra = array())
99
    {
100
        return self::log(Level::DEBUG, $toLog, $extra);
101
    }
102
    
103
    public static function info($toLog, $extra = array())
104
    {
105
        return self::log(Level::INFO, $toLog, $extra);
106
    }
107
    
108
    public static function notice($toLog, $extra = array())
109
    {
110
        return self::log(Level::NOTICE, $toLog, $extra);
111
    }
112
    
113
    public static function warning($toLog, $extra = array())
114
    {
115
        return self::log(Level::WARNING, $toLog, $extra);
116
    }
117
    
118
    public static function error($toLog, $extra = array())
119
    {
120
        return self::log(Level::ERROR, $toLog, $extra);
121
    }
122
    
123
    public static function critical($toLog, $extra = array())
124
    {
125
        return self::log(Level::CRITICAL, $toLog, $extra);
126
    }
127
    
128
    public static function alert($toLog, $extra = array())
129
    {
130
        return self::log(Level::ALERT, $toLog, $extra);
131
    }
132
    
133
    public static function emergency($toLog, $extra = array())
134
    {
135
        return self::log(Level::EMERGENCY, $toLog, $extra);
136
    }
137
138
    public static function setupExceptionHandling()
139
    {
140
        self::$exceptionHandler = new ExceptionHandler(self::$logger);
141
        self::$exceptionHandler->register();
142
    }
143
    
144
    public static function setupErrorHandling()
145
    {
146
        self::$errorHandler = new ErrorHandler(self::$logger);
147
        self::$errorHandler->register();
148
    }
149
150
    public static function setupFatalHandling()
151
    {
152
        self::$fatalHandler = new FatalHandler(self::$logger);
153
        self::$fatalHandler->register();
154
    }
155
156
    private static function getNotInitializedResponse()
157
    {
158
        return new Response(0, "Rollbar Not Initialized");
159
    }
160
    
161
    public static function setupBatchHandling()
162
    {
163
        register_shutdown_function('Rollbar\Rollbar::flushAndWait');
164
    }
165
166
    public static function flush()
167
    {
168
        if (is_null(self::$logger)) {
169
            return;
170
        }
171
        self::$logger->flush();
172
    }
173
174
    public static function flushAndWait()
175
    {
176
        if (is_null(self::$logger)) {
177
            return;
178
        }
179
        self::$logger->flushAndWait();
180
    }
181
    
182
    public static function addCustom($key, $value)
183
    {
184
        self::$logger->addCustom($key, $value);
185
    }
186
    
187
    public static function removeCustom($key)
188
    {
189
        self::$logger->removeCustom($key);
190
    }
191
    
192
    public static function getCustom()
193
    {
194
        self::$logger->getCustom();
195
    }
196
    
197
    public static function configure($config)
198
    {
199
        self::$logger->configure($config);
200
    }
201
    
202
    /**
203
     * Destroys the currently stored $logger allowing for a fresh configuration.
204
     * This is especially used in testing scenarios.
205
     */
206
    public static function destroy()
207
    {
208
        self::$logger = null;
209
    }
210
    
211
    // @codingStandardsIgnoreStart
212
    
213
    /**
214
     * Below methods are deprecated and still available only for backwards
215
     * compatibility. If you're still using them in your application, please
216
     * transition to using the ::log method as soon as possible.
217
     */
218
    
219
    /**
220
     * @param \Exception $exc Exception to be logged
221
     * @param array $extra_data Additional data to be logged with the exception
222
     * @param array $payload_data This is deprecated as of v1.0.0 and remains for
223
     * backwards compatibility. The content fo this array will be merged with
224
     * $extra_data.
225
     *
226
     * @return string uuid
227
     *
228
     * @deprecated 1.0.0 This method has been replaced by ::log
229
     */
230
    public static function report_exception($exc, $extra_data = null, $payload_data = null)
231
    {
232
        
233
        if ($payload_data) {
234
            $extra_data = array_merge($extra_data, $payload_data);
0 ignored issues
show
Bug introduced by
It seems like $extra_data can also be of type null; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
            $extra_data = array_merge(/** @scrutinizer ignore-type */ $extra_data, $payload_data);
Loading history...
235
        }
236
        return self::log(Level::ERROR, $exc, $extra_data)->getUuid();
237
    }
238
239
    /**
240
     * @param string $message Message to be logged
241
     * @param string $level One of the values in \Rollbar\Payload\Level::$values
242
     * @param array $extra_data Additional data to be logged with the exception
243
     * @param array $payload_data This is deprecated as of v1.0.0 and remains for
244
     * backwards compatibility. The content fo this array will be merged with
245
     * $extra_data.
246
     *
247
     * @return string uuid
248
     *
249
     * @deprecated 1.0.0 This method has been replaced by ::log
250
     */
251
    public static function report_message($message, $level = null, $extra_data = null, $payload_data = null)
252
    {
253
        
254
        $level = $level ? $level : Level::ERROR;
255
        if ($payload_data) {
256
            $extra_data = array_merge($extra_data, $payload_data);
0 ignored issues
show
Bug introduced by
It seems like $extra_data can also be of type null; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
            $extra_data = array_merge(/** @scrutinizer ignore-type */ $extra_data, $payload_data);
Loading history...
257
        }
258
        return self::log($level, $message, $extra_data)->getUuid();
259
    }
260
261
262
    /**
263
     * Catch any fatal errors that are causing the shutdown
264
     *
265
     * @deprecated 1.0.0 This method has been replaced by ::fatalHandler
266
     */
267
    public static function report_fatal_error()
268
    {
269
        self::$fatalHandler->handle();
270
    }
271
272
273
    /**
274
     * This function must return false so that the default php error handler runs
275
     *
276
     * @deprecated 1.0.0 This method has been replaced by ::log
277
     */
278
    public static function report_php_error($errno, $errstr, $errfile, $errline)
279
    {
280
        self::$errorHandler->handle($errno, $errstr, $errfile, $errline);
281
        return false;
282
    }
283
284
    // @codingStandardsIgnoreEnd
285
}
286