Test Failed
Pull Request — 7.x (#141)
by
unknown
04:29
created

MultiauthServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 73
ccs 24
cts 27
cp 0.8889
rs 10
c 8
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 2
A registerMigrations() 0 11 2
A registerAuthenticator() 0 13 1
A createAccessTokenProvider() 0 6 1
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 18
    public function boot(ProviderRepository $providers)
22
    {
23 18
        if ($this->app->runningInConsole()) {
24 18
            $this->registerMigrations();
25
        }
26
27 18
        $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 18
        $this->app->singleton(AddCustomProvider::class);
32 18
    }
33
34
    /**
35
     * Register migrations to work on `php artisan migrate` command.
36
     *
37
     * @return void
38
     */
39 18
    protected function registerMigrations()
40
    {
41 18
        $migrationsPath = __DIR__.'/../../database/migrations';
42
43 18
        if (PassportMultiauth::$runsMigrations) {
44 18
            $this->loadMigrationsFrom($migrationsPath);
45
        }
46
47 18
        $this->publishes(
48 18
            [$migrationsPath => database_path('migrations')],
49 18
            'migrations'
50
        );
51 18
    }
52
53
    /**
54
     * Register the authenticator services.
55
     *
56
     * @return void
57
     */
58 18
    protected function registerAuthenticator()
59
    {
60 18
        $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
            $app['auth.loaded'] = true;
65
66
            return new AuthManager($app);
67 18
        });
68
69 18
        $this->app->singleton('auth.driver', function ($app) {
70
            return $app['auth']->guard();
71 18
        });
72 18
    }
73
74
    /**
75
     * Create access token provider when access token is created.
76
     *
77
     * @param ProviderRepository $repository
78
     * @return void
79
     */
80 18
    protected function createAccessTokenProvider(ProviderRepository $repository)
81
    {
82 18
        Event::listen(AccessTokenCreated::class, function ($event) use ($repository) {
83 1
            $provider = config('auth.guards.api.provider');
84
85 1
            $repository->create($event->tokenId, $provider);
86 18
        });
87 18
    }
88
}
89