Passed
Pull Request — 2.0 (#45)
by Samuel
10:16
created

MultiauthServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 54
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMigrations() 0 9 1
A createAccessTokenProvider() 0 6 1
A boot() 0 16 2
1
<?php
2
3
namespace SMartins\PassportMultiauth\Providers;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\ServiceProvider;
7
use Laravel\Passport\Events\AccessTokenCreated;
8
use SMartins\PassportMultiauth\ProviderRepository;
9
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
10
11
class MultiauthServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap any application services.
15
     *
16
     * @return void
17
     */
18 34
    public function boot(ProviderRepository $providers)
19
    {
20 34
        if ($this->app->runningInConsole()) {
21 34
            $this->registerMigrations();
22
        }
23
24 34
        $this->createAccessTokenProvider($providers);
25
26
        // Register the facade ServerRequest returning an instance of DiactorosFactory.
27
        $this->app->singleton('ServerRequest', function () {
28
            return new DiactorosFactory;
29 34
        });
30
31
        // Register the middleware as singleton to use the same middleware
32
        // instance when the handle and terminate methods are called.
33 34
        $this->app->singleton(\SMartins\PassportMultiauth\Http\Middleware\AddCustomProvider::class);
34 34
    }
35
36
    /**
37
     * Register migrations to work on `php artisan migrate` command.
38
     *
39
     * @return void
40
     */
41 34
    protected function registerMigrations()
42
    {
43 34
        $migrationsPath = __DIR__.'/../../database/migrations';
44
45 34
        $this->loadMigrationsFrom($migrationsPath);
46
47 34
        $this->publishes(
48 34
            [$migrationsPath => database_path('migrations')],
49 34
            'migrations'
50
        );
51 34
    }
52
53
    /**
54
     * Create access token provider when access token is created.
55
     *
56
     * @param ProviderRepository $repository
57
     * @return void
58
     */
59 34
    protected function createAccessTokenProvider(ProviderRepository $repository)
60
    {
61
        Event::listen(AccessTokenCreated::class, function ($event) use ($repository) {
62 4
            $provider = config('auth.guards.api.provider');
63
64 4
            $repository->create($event->tokenId, $provider);
65 34
        });
66 34
    }
67
}
68