Completed
Push — master ( 42edb2...d38aea )
by Russell
08:56 queued 10s
created

SentryHandler::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class: SentryHandler.
5
 *
6
 * @author  Russell Michell 2017-2019 <[email protected]>
7
 * @package phptek/sentry
8
 */
9
10
namespace PhpTek\Sentry\Handler;
11
12
use Monolog\Handler\AbstractProcessingHandler;
13
use Monolog\Logger;
14
use Sentry\Severity;
15
use SilverStripe\Core\Injector\Injectable;
16
use PhpTek\Sentry\Log\SentryLogger;
17
use PhpTek\Sentry\Adaptor\SentryAdaptor;
18
19
/**
20
 * Monolog handler to send messages to a Sentry (https://github.com/getsentry/sentry) server
21
 * using sentry-php (https://github.com/getsentry/sentry-php).
22
 */
23
class SentryHandler extends AbstractProcessingHandler
24
{
25
    use Injectable;
26
27
    /**
28
     * @param  int     $level
29
     * @param  boolean $bubble
30
     * @param  array   $extras
31
     * @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...
32
     */
33
    public function __construct(int $level = Logger::DEBUG, bool $bubble = true, array $extras = [])
34
    {
35
        // Returns an instance of {@link SentryLogger}
36
        $logger = SentryLogger::factory($extras);
37
        $this->client = $logger->getAdaptor();
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
        parent::__construct($level, $bubble);
40
    }
41
42
    /**
43
     * write() forms the entry point into the physical sending of the error. The
44
     * sending itself is done by the current adaptor's `send()` method.
45
     *
46
     * @param  array $record An array of error-context metadata with the following
47
     *                       available keys:
48
     *
49
     *                       - message
50
     *                       - context
51
     *                       - level
52
     *                       - level_name
53
     *                       - channel
54
     *                       - datetime
55
     *                       - extra
56
     *                       - formatted
57
     *
58
     * @return void
59
     */
60
    protected function write(array $record) : void
61
    {
62
        $record = array_merge($record, [
63
            'timestamp'  => $record['datetime']->getTimestamp(),
64
            'stacktrace' => SentryLogger::backtrace($record),
65
        ]);
66
67
        if (
68
                isset($record['context']['exception']) &&
69
                $record['context']['exception'] instanceof \Throwable
70
            ) {
71
            $this->client->getSDK()->captureException($record['context']['exception']);
72
        } else {
73
            $this->client->getSDK()->captureMessage($record['formatted'], new Severity(strtolower($record['level_name'])));
74
        }
75
    }
76
77
    /**
78
     * @return SentryAdaptor
79
     */
80
    public function getClient() : SentryAdaptor
81
    {
82
        return $this->client;
83
    }
84
}
85