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

Integration::logLevelToBreadcrumbLevel()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9.648

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 9
nop 1
dl 0
loc 17
ccs 12
cts 15
cp 0.8
crap 9.648
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Sentry;
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
     *
69
     */
70 1
    public static function extractNameForRoute(?CurrentRoute $route): ?string
71
    {
72 1
        if (null === $route) {
73
            return null;
74
        }
75 1
        $routeName = null;
76
77 1
        if ($route->getName()) {
78
            $routeName = $route->getName();
79
        }
80
81 1
        if (empty($routeName) && $route->getUri()) {
82
            $routeName = $route->getUri()->getPath();
83
        }
84
85 1
        return $routeName;
86
    }
87
88
    /**
89
     * Retrieve the meta tags with tracing information to link this request to front-end requests.
90
     */
91
    public static function sentryTracingMeta(): string
92
    {
93
        $span = self::currentTracingSpan();
94
95
        if ($span === null) {
96
            return '';
97
        }
98
99
        return sprintf('<meta name="sentry-trace" content="%s"/>', $span->toTraceparent());
100
    }
101
102
    /**
103
     * Get the current active tracing span from the scope.
104
     *
105
     *
106
     * @internal This is used internally as an easy way to retrieve the current active tracing span.
107
     */
108 4
    public static function currentTracingSpan(): ?Span
109
    {
110 4
        return SentrySdk::getCurrentHub()->getSpan();
111
    }
112
113 4
    public static function logLevelToBreadcrumbLevel(string $level): string
114
    {
115 4
        switch (strtolower($level)) {
116 4
            case 'debug':
117 4
                return Breadcrumb::LEVEL_DEBUG;
118 4
            case 'warning':
119
                return Breadcrumb::LEVEL_WARNING;
120 4
            case 'error':
121 1
                return Breadcrumb::LEVEL_ERROR;
122 3
            case 'critical':
123 3
            case 'alert':
124 3
            case 'emergency':
125
                return Breadcrumb::LEVEL_FATAL;
126 3
            case 'info':
127
            case 'notice':
128
            default:
129 3
                return Breadcrumb::LEVEL_INFO;
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 9
    public function setupOnce(): void
137
    {
138
        Scope::addGlobalEventProcessor(function (Event $event): Event {
139 9
            $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
140
141 9
            if (!$self instanceof self) {
142
                return $event;
143
            }
144
145 9
            if (empty($event->getTransaction())) {
146 8
                $event->setTransaction($self->getTransaction());
147
            }
148
149 9
            return $event;
150
        });
151
    }
152
153 8
    public static function getTransaction(): ?string
154
    {
155 8
        return self::$transaction;
156
    }
157
158
    public static function setTransaction(?string $transaction): void
159
    {
160
        self::$transaction = $transaction;
161
    }
162
}
163