Passed
Pull Request — master (#19)
by
unknown
02:53
created

ExceptionContextIntegration::setupOnce()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 11.6215

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 1
nop 0
dl 0
loc 25
ccs 6
cts 13
cp 0.4615
crap 11.6215
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Sentry\Integration;
6
7
use Sentry\Event;
8
use Sentry\EventHint;
9
use Sentry\Integration\IntegrationInterface;
10
use Sentry\SentrySdk;
11
use Sentry\State\Scope;
12
13
class ExceptionContextIntegration implements IntegrationInterface
14
{
15 1
    public function setupOnce(): void
16
    {
17 1
        Scope::addGlobalEventProcessor(static function (Event $event, ?EventHint $hint = null): Event {
18 1
            $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
19
20 1
            if (!$self instanceof self) {
21
                return $event;
22
            }
23
24 1
            if ($hint === null || $hint->exception === null) {
25 1
                return $event;
26
            }
27
28
            if (!method_exists($hint->exception, 'context')) {
29
                return $event;
30
            }
31
32
            /** @psalm-suppress  MixedAssignment  $context */
33
            $context = $hint->exception->context();
34
35
            if (is_array($context)) {
36
                $event->setExtra(['exception_context' => $context]);
37
            }
38
39
            return $event;
40
        });
41
    }
42
}
43