Passed
Push — master ( e709b2...a3d3dc )
by Zing
03:43
created

SentryContext   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 3
A resolveUserContext() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\LaravelSentry\Middleware;
6
7
use Closure;
8
use Illuminate\Container\Container;
9
use Illuminate\Contracts\Auth\Authenticatable;
10
use Illuminate\Contracts\Auth\Factory;
11
use function Sentry\configureScope;
12
use Sentry\State\Scope;
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()->check() && Container::getInstance()->bound('sentry')) {
46
            configureScope(
47
                function (Scope $scope): void {
48
                    $scope->setUser($this->resolveUserContext($this->auth->getDefaultDriver(), $this->auth->guard()->user()));
0 ignored issues
show
Bug introduced by
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

48
                    $scope->setUser($this->resolveUserContext($this->auth->getDefaultDriver(), /** @scrutinizer ignore-type */ $this->auth->guard()->user()));
Loading history...
49
                }
50
            );
51
        }
52
53
        return $next($request);
54
    }
55
56
    protected function resolveUserContext($guard, Authenticatable $user): array
0 ignored issues
show
Unused Code introduced by
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

56
    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...
57
    {
58
        return [
59
            'id' => $user->getAuthIdentifier(),
60
            'email' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
61
        ];
62
    }
63
}
64