Completed
Push — master ( 4314d2...3ee107 )
by Artem
03:45
created

ServiceProvider::loadMigrations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Slides\Connector\Auth;
4
5
use Illuminate\Support\Facades\Auth;
6
7
/**
8
 * Class ServiceProvider
9
 *
10
 * @package Slides\Connector\Auth
11
 */
12
class ServiceProvider extends \Illuminate\Support\ServiceProvider
13
{
14
    /**
15
     * Indicates if loading of the provider is deferred.
16
     *
17
     * @var bool
18
     */
19
    protected $defer = false;
20
21
    /**
22
     * Perform post-registration booting of services.
23
     *
24
     * @return void
25
     */
26
    public function boot()
27
    {
28
        $this->loadPublishes();
29
        $this->loadConsoleCommands();
30
        $this->loadGuards();
31
    }
32
33
    /**
34
     * Register any package services.
35
     *
36
     * @return void
37
     */
38
    public function register()
39
    {
40
        $this->mergeConfigFrom(
41
            __DIR__ . '/../config/connector.php', 'connector'
42
        );
43
44
        $this->registerFacades();
45
        $this->registerGuards();
46
    }
47
48
    /**
49
     * Load configs
50
     *
51
     * @return void
52
     */
53
    protected function loadPublishes()
54
    {
55
        $this->publishes([__DIR__ . '/../config/connector.php' => config_path('connector.php')], 'config');
56
        $this->publishes([__DIR__ . '/../database/migrations/' => database_path('migrations')], 'migrations');
57
    }
58
59
    /**
60
     * Load console commands
61
     *
62
     * @return void
63
     */
64
    protected function loadConsoleCommands()
65
    {
66
        if(!$this->app->runningInConsole()) {
67
            return;
68
        }
69
70
        $this->commands([
71
            \Slides\Connector\Auth\Commands\MakeAuthHandlers::class,
72
            \Slides\Connector\Auth\Commands\SyncUsers::class
73
        ]);
74
    }
75
76
    /**
77
     * Load default and fallback authentication guards.
78
     *
79
     * @return void
80
     */
81
    protected function loadGuards()
82
    {
83
        // Skip loading guards if an application running in the console
84
        if($this->app->runningInConsole()) {
85
            return;
86
        }
87
88
        $this->app['authService']->setGuard(
89
            $this->app['auth']->guard('authService')
90
        );
91
92
        if(!$this->enabled()) {
93
            $this->app['authService']->setFallbackGuard(
94
                $this->app['auth']->guard('fallback')
95
            );
96
        }
97
    }
98
99
    /**
100
     * Register package facades
101
     */
102
    protected function registerFacades()
103
    {
104
        $this->app->singleton(Client::class, function($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

104
        $this->app->singleton(Client::class, function(/** @scrutinizer ignore-unused */ $app) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
            return new Client();
106
        });
107
108
        $this->app->singleton(AuthService::class, function($app) {
109
            return new AuthService($app[Client::class]);
110
        });
111
112
        $this->app->bind('authService', function($app) {
113
            return $app[AuthService::class];
114
        });
115
    }
116
117
    /**
118
     * Register the guard
119
     *
120
     * @return void
121
     */
122
    protected function registerGuards()
123
    {
124
        $this->app['auth']->extend('authServiceToken', function(\Illuminate\Foundation\Application $app) {
125
            return $app->make(TokenGuard::class, [
126
                'provider' => $app['auth']->createUserProvider($app['config']['auth.guards.authService.provider']),
127
                'request' => $app['request'],
128
                'authService' => $app['authService'],
129
                'client' => $app[Client::class]
130
            ]);
131
        });
132
133
        // Register the fallback driver if service is disabled
134
        if(!$this->enabled()) {
135
            $this->app['auth']->shouldUse('fallback');
136
        }
137
    }
138
139
    /**
140
     * Checks whether service is enabled
141
     *
142
     * @return bool
143
     */
144
    private function enabled(): bool
145
    {
146
        return config('connector.auth.enabled') === true;
147
    }
148
}