Passed
Pull Request — master (#19)
by
unknown
03:06
created

ExceptionContextIntegration::setupOnce()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
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 10
    public function setupOnce(): void
16
    {
17 1
        Scope::addGlobalEventProcessor(
18 1
            static function (Event $event, ?EventHint $hint = null): Event {
19 10
                $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
20
21
                if (
22 10
                    !$self instanceof self ||
23 2
                    $hint === null ||
24 2
                    $hint->exception === null ||
25 1
                    !method_exists($hint->exception, 'context') ||
26 10
                    !is_array($hint->exception->context())
27
                ) {
28 9
                    return $event;
29
                }
30 1
                $event->setExtra(['exception_context' => $hint->exception->context()]);
31
32 1
                return $event;
33
            }
34
        );
35
    }
36
}
37