Issues (3)

src/Middleware/SentryContext.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\LaravelSentry\Middleware;
6
7
use Closure;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use Illuminate\Contracts\Auth\Factory;
10
use Sentry\State\Scope;
11
use Zing\LaravelSentry\Support\SentryIntegration;
12
use function Sentry\configureScope;
13
14
class SentryContext
15
{
16
    /**
17
     * The authentication factory instance.
18
     *
19
     * @var \Illuminate\Contracts\Auth\Factory
20
     */
21
    protected $auth;
22
23
    /**
24
     * Create a new middleware instance.
25
     *
26
     * @param \Illuminate\Contracts\Auth\Factory $auth
27
     *
28
     * @return void
29
     */
30
    public function __construct(Factory $auth)
31
    {
32
        $this->auth = $auth;
33
    }
34
35
    /**
36
     * Handle an incoming request.
37
     *
38
     * @param \Illuminate\Http\Request $request
39
     * @param \Closure $next
40
     *
41
     * @return mixed
42
     */
43
    public function handle($request, Closure $next)
44
    {
45
        if ($this->auth->guard()->guest()) {
46
            return $next($request);
47
        }
48
49
        if (! $this->sentryIsBound()) {
50
            return $next($request);
51
        }
52
53
        configureScope(
54
            function (Scope $scope): void {
55
                $scope->setUser($this->resolveUserContext($this->auth->getDefaultDriver(), $this->auth->guard()->user()));
0 ignored issues
show
It seems like $this->auth->guard()->user() can also be of type null; however, parameter $user of Zing\LaravelSentry\Middl...t::resolveUserContext() does only seem to accept Illuminate\Contracts\Auth\Authenticatable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                $scope->setUser($this->resolveUserContext($this->auth->getDefaultDriver(), /** @scrutinizer ignore-type */ $this->auth->guard()->user()));
Loading history...
56
            }
57
        );
58
59
        return $next($request);
60
    }
61
62
    protected function sentryIsBound(): bool
63
    {
64
        return SentryIntegration::bound();
65
    }
66
67
    /**
68
     * @param string $guard
69
     * @param \Illuminate\Contracts\Auth\Authenticatable $user
70
     *
71
     * @return array
72
     */
73
    protected function resolveUserContext($guard, Authenticatable $user): array
0 ignored issues
show
The parameter $guard is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
    protected function resolveUserContext(/** @scrutinizer ignore-unused */ $guard, Authenticatable $user): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        return [
76
            'id' => $user->getAuthIdentifier(),
77
        ];
78
    }
79
}
80