JWTServiceProvider   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 14
c 4
b 0
f 0
dl 0
loc 27
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 25 2
1
<?php
2
3
namespace Sprocketbox\JWT;
4
5
use Illuminate\Auth\AuthManager;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
8
use RuntimeException;
9
use Sprocketbox\JWT\Commands\KeyGenerateCommand;
10
11
class JWTServiceProvider extends BaseServiceProvider
12
{
13
    public function register(): void
14
    {
15
        // Register the JWT driver with Laravel
16
        $auth = $this->app->make(AuthManager::class);
17
        $auth->extend('jwt', function (Application $app, string $name, array $config) use ($auth) {
18
            $provider = $auth->createUserProvider($config['provider'] ?? null);
19
20
            if ($provider === null) {
21
                throw new RuntimeException('No user provider available');
22
            }
23
24
            $guard = new JWTGuard($provider, $name, $config);
25
            $guard
26
                // Set the request instance on the guard
27
                ->setRequest($app->refresh('request', $guard, 'setRequest'))
28
                // Set the event dispatcher on the guard
29
                ->setDispatcher($this->app['events'])
30
                // Set the cookie jar
31
                ->setCookieJar($this->app['cookie']);
32
33
            return $guard;
34
        });
35
36
        $this->commands([
37
            KeyGenerateCommand::class,
38
        ]);
39
    }
40
}