Completed
Push — 6.x ( 90a72d...95cac0 )
by Samuel
08:14
created

MultiauthServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
c 4
b 0
f 0
1
<?php
2
3
namespace SMartins\PassportMultiauth\Providers;
4
5
use Illuminate\Auth\AuthServiceProvider;
6
use Illuminate\Support\Facades\Event;
7
use Laravel\Passport\Events\AccessTokenCreated;
8
use SMartins\PassportMultiauth\Auth\AuthManager;
9
use SMartins\PassportMultiauth\Http\Middleware\AddCustomProvider;
10
use SMartins\PassportMultiauth\PassportMultiauth;
11
use SMartins\PassportMultiauth\ProviderRepository;
12
13
class MultiauthServiceProvider extends AuthServiceProvider
14
{
15
    /**
16
     * Bootstrap any application services.
17
     *
18
     * @param ProviderRepository $providers
19
     * @return void
20
     */
21 40
    public function boot(ProviderRepository $providers)
22
    {
23 40
        if ($this->app->runningInConsole()) {
24 40
            $this->registerMigrations();
25
        }
26
27 40
        $this->createAccessTokenProvider($providers);
28
29
        // Register the middleware as singleton to use the same middleware
30
        // instance when the handle and terminate methods are called.
31 40
        $this->app->singleton(AddCustomProvider::class);
32 40
    }
33
34
    /**
35
     * Register migrations to work on `php artisan migrate` command.
36
     *
37
     * @return void
38
     */
39 40
    protected function registerMigrations()
40
    {
41 40
        $migrationsPath = __DIR__.'/../../database/migrations';
42
43 40
        if (PassportMultiauth::$runsMigrations) {
44 40
            $this->loadMigrationsFrom($migrationsPath);
45
        }
46
47 40
        $this->publishes(
48 40
            [$migrationsPath => database_path('migrations')],
49 40
            'migrations'
50
        );
51 40
    }
52
53
    /**
54
     * Register the authenticator services.
55
     *
56
     * @return void
57
     */
58 40
    protected function registerAuthenticator()
59
    {
60
        $this->app->singleton('auth', function ($app) {
61
            // Once the authentication service has actually been requested by the developer
62
            // we will set a variable in the application indicating such. This helps us
63
            // know that we need to set any queued cookies in the after event later.
64 16
            $app['auth.loaded'] = true;
65
66 16
            return new AuthManager($app);
67 40
        });
68
69
        $this->app->singleton('auth.driver', function ($app) {
70
            return $app['auth']->guard();
71 40
        });
72 40
    }
73
74
    /**
75
     * Create access token provider when access token is created.
76
     *
77
     * @param ProviderRepository $repository
78
     * @return void
79
     */
80 40
    protected function createAccessTokenProvider(ProviderRepository $repository)
81
    {
82
        Event::listen(AccessTokenCreated::class, function ($event) use ($repository) {
83 10
            $provider = config('auth.guards.api.provider');
84
85 10
            $repository->create($event->tokenId, $provider);
86 40
        });
87 40
    }
88
}
89