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

ExceptionContextIntegration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 27
ccs 6
cts 13
cp 0.4615
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A setupOnce() 0 25 6
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