JwtServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.4285
1
<?php
2
3
namespace Framgia\Jwt;
4
5
use Illuminate\Auth\AuthManager;
6
use Framgia\Jwt\Storage\CacheStorage;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\ServiceProvider;
9
use Framgia\Jwt\Signers\Factory as SignerFactory;
10
use Illuminate\Contracts\Cache\Repository as Cache;
11
use Framgia\Jwt\Contracts\Signer as SignerContract;
12
use Framgia\Jwt\Contracts\Storage as StorageContract;
13
14
class JwtServiceProvider extends ServiceProvider
15
{
16
    public function boot()
17
    {
18
        $this->publishes([
19
            __DIR__ . '/../config/jwt.php' => config_path('api.php')
20
        ]);
21
22
        $this->mergeConfigFrom(__DIR__ . '/../config/jwt.php', 'jwt');
23
24
        $manager = $this->app[AuthManager::class];
25
26
        $manager->extend('jwt', function ($app, $name, $config) use ($manager) {
27
            $guard = new Guard($manager->createUserProvider($config['provider']), $app[Request::class], $app[Blacklist::class], $app[SignerContract::class]);
28
29
            $app->refresh(Request::class, $guard, 'setRequest');
30
31
            return $guard;
32
        });
33
    }
34
35
    /**
36
     * Register services
37
     *
38
     * @return void
39
     */
40
    public function register()
41
    {
42
        $this->app->singleton(StorageManager::class, function ($app) {
43
            return new StorageManager($app);
44
        });
45
46
        $this->app->singleton(Blacklist::class, function ($app) {
47
            return new Blacklist($app[StorageContract::class]);
48
        });
49
50
        $this->app->singleton(SignerFactory::class, function ($app) {
51
            return new SignerFactory($app);
52
        });
53
54
        $this->app->bind(SignerContract::class, function ($app) {
55
            return $app[SignerFactory::class]->driver();
56
        });
57
58
        $this->app->bind(StorageContract::class, function ($app) {
59
            return $app[StorageManager::class]->driver();
60
        });
61
    }
62
}
63