Filter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 2
A __construct() 0 5 1
1
<?php
2
3
namespace Spinen\Halo\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Routing\UrlGenerator;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Redirector;
9
use Spinen\Halo\Api\Client as Halo;
10
11
/**
12
 * Class Filter
13
 */
14
class Filter
15
{
16
    /**
17
     * Create a new Halo filter middleware instance.
18
     */
19 6
    public function __construct(
20
        protected Halo $halo,
21
        protected Redirector $redirector,
22
        protected UrlGenerator $url_generator
23
    ) {
24 6
    }
25
26
    /**
27
     * Handle an incoming request.
28
     */
29 5
    public function handle(Request $request, Closure $next)
30
    {
31 5
        if (! $request->user()->halo_token) {
32
            // Set intended route, so that after linking account, user is put where they were going
33 4
            $this->redirector->setIntendedUrl($request->path());
34
35
            // Keys will be null if not enabled
36 4
            $pkce = $this->halo->generateProofKey();
37
38 4
            $request->session()->flash('halo_code_verifier', $pkce['verifier'] ?? null);
39
40 4
            return $this->redirector->to(
41 4
                $this->halo->oauthUri(
42 4
                    challenge: $pkce['challenge'] ?? null,
43 4
                    uri: (string) $this->url_generator->route('halo.sso.redirect_uri'),
44 4
                )
45 4
            );
46
        }
47
48 1
        return $next($request);
49
    }
50
}
51