Passed
Pull Request — master (#19)
by
unknown
12:21
created

Integration::setupOnce()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 0
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 10
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\Breadcrumb;
8
use Sentry\Event;
9
use Sentry\Integration\IntegrationInterface;
10
use Sentry\SentrySdk;
11
use Sentry\State\Scope;
12
use Sentry\Tracing\Span;
13
use Yiisoft\Router\CurrentRoute;
14
15
use function Sentry\addBreadcrumb;
16
use function Sentry\configureScope;
17
18
final class Integration implements IntegrationInterface
19
{
20
    private static ?string $transaction = null;
21
22
    /**
23
     * Adds a breadcrumb if the integration is enabled for Yii3.
24
     */
25 4
    public static function addBreadcrumb(Breadcrumb $breadcrumb): void
26
    {
27 4
        $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
28
29 4
        if (!$self instanceof self) {
30
            return;
31
        }
32
33 4
        addBreadcrumb($breadcrumb);
34
    }
35
36
    /**
37
     * Configures the scope if the integration is enabled for Yii3.
38
     */
39
    public static function configureScope(callable $callback): void
40
    {
41
        $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
42
43
        if (!$self instanceof self) {
44
            return;
45
        }
46
47
        configureScope($callback);
48
    }
49
50
    /**
51
     * Block until all async events are processed for the HTTP transport.
52
     *
53
     * @internal This is not part of the public API and is here temporarily until
54
     *  the underlying issue can be resolved, this method will be removed.
55
     */
56
    public static function flushEvents(): void
57
    {
58
        $client = SentrySdk::getCurrentHub()->getClient();
59
60
        if ($client !== null) {
61
            $client->flush();
62
        }
63
    }
64
65
    /**
66
     * Extract the readable name for a route.
67
     */
68 1
    public static function extractNameForRoute(?CurrentRoute $route): ?string
69
    {
70 1
        if (null === $route) {
71
            return null;
72
        }
73 1
        $routeName = null;
74
75 1
        if ($route->getName()) {
76
            $routeName = $route->getName();
77
        }
78
79 1
        if (empty($routeName) && $route->getUri()) {
80
            $routeName = $route->getUri()->getPath();
81
        }
82
83 1
        return $routeName;
84
    }
85
86
    /**
87
     * Retrieve the meta tags with tracing information to link this request to front-end requests.
88
     */
89
    public static function sentryTracingMeta(): string
90
    {
91
        $span = self::currentTracingSpan();
92
93
        if ($span === null) {
94
            return '';
95
        }
96
97
        return sprintf('<meta name="sentry-trace" content="%s"/>', $span->toTraceparent());
98
    }
99
100
    /**
101
     * Get the current active tracing span from the scope.
102
     *
103
     * @internal This is used internally as an easy way to retrieve the current active tracing span.
104
     */
105 4
    public static function currentTracingSpan(): ?Span
106
    {
107 4
        return SentrySdk::getCurrentHub()->getSpan();
108
    }
109
110 4
    public static function logLevelToBreadcrumbLevel(string $level): string
111
    {
112 4
        switch (strtolower($level)) {
113 4
        case 'debug':
114 4
            return Breadcrumb::LEVEL_DEBUG;
115 4
        case 'warning':
116
            return Breadcrumb::LEVEL_WARNING;
117 4
        case 'error':
118 1
            return Breadcrumb::LEVEL_ERROR;
119 3
        case 'critical':
120 3
        case 'alert':
121 3
        case 'emergency':
122
            return Breadcrumb::LEVEL_FATAL;
123 3
        case 'info':
124
        case 'notice':
125
        default:
126 3
            return Breadcrumb::LEVEL_INFO;
127
        }
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 10
    public function setupOnce(): void
134
    {
135 1
        Scope::addGlobalEventProcessor(
136 1
            function (Event $event): Event {
137 10
                $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
138
139 10
                if (!$self instanceof self) {
140
                    return $event;
141
                }
142
143 10
                if (empty($event->getTransaction())) {
144 9
                    $event->setTransaction($self->getTransaction());
145
                }
146
147 10
                return $event;
148
            }
149
        );
150
    }
151
152 9
    public static function getTransaction(): ?string
153
    {
154 9
        return self::$transaction;
155
    }
156
157
    public static function setTransaction(?string $transaction): void
158
    {
159
        self::$transaction = $transaction;
160
    }
161
}
162