ServiceProvider::loadConsoleCommands()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 4
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace Slides\Connector\Auth;
4
5
use Slides\Connector\Auth\Clients\Mandrill\Mailer;
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
        $this->loadRoutes();
32
    }
33
34
    /**
35
     * Register any package services.
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        $this->mergeConfigFrom(
42
            __DIR__ . '/../config/connector.php', 'connector'
43
        );
44
45
        $this->registerFacades();
46
        $this->registerGuards();
47
    }
48
49
    /**
50
     * Load configs
51
     *
52
     * @return void
53
     */
54
    protected function loadPublishes()
55
    {
56
        $this->publishes([__DIR__ . '/../config/connector.php' => config_path('connector.php')], 'config');
57
        $this->publishes([__DIR__ . '/../database/migrations/' => database_path('migrations')], 'migrations');
58
    }
59
60
    /**
61
     * Load console commands
62
     *
63
     * @return void
64
     */
65
    protected function loadConsoleCommands()
66
    {
67
        if(!$this->app->runningInConsole()) {
68
            return;
69
        }
70
71
        $this->commands([
72
            \Slides\Connector\Auth\Commands\MakeAuthHandlers::class,
73
            \Slides\Connector\Auth\Commands\SyncUsers::class,
74
            \Slides\Connector\Auth\Commands\SyncExport::class,
75
            \Slides\Connector\Auth\Commands\SyncImport::class,
76
            \Slides\Connector\Auth\Commands\ManageUsers::class,
77
            \Slides\Connector\Auth\Clients\Mandrill\Commands\Send::class
78
        ]);
79
    }
80
81
    /**
82
     * Load default and fallback authentication guards.
83
     *
84
     * @return void
85
     */
86
    protected function loadGuards()
87
    {
88
        // Skip loading guards if an application running in the console
89
        if($this->app->runningInConsole()) {
90
            return;
91
        }
92
93
        $this->app['authService']->setGuard(
94
            $this->app['auth']->guard('authService')
95
        );
96
97
        if(!$this->enabled()) {
98
            $this->app['authService']->setFallbackGuard(
99
                $this->app['auth']->guard('fallback')
100
            );
101
        }
102
    }
103
104
    /**
105
     * Load routes.
106
     *
107
     * @return void
108
     */
109
    protected function loadRoutes()
110
    {
111
        $this->loadRoutesFrom(__DIR__ . '/Http/routes.php');
112
113
        \Illuminate\Support\Facades\Route::getRoutes()->refreshNameLookups();
114
    }
115
116
    /**
117
     * Register package facades
118
     */
119
    protected function registerFacades()
120
    {
121
        $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

121
        $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...
122
            return new Client();
123
        });
124
125
        $this->app->singleton(AuthService::class);
126
127
        $this->app->alias(AuthService::class,'authService');
128
129
        $this->app->singleton(Mailer::class);
130
131
        $this->app->alias(Mailer::class, 'mandrill');
132
    }
133
134
    /**
135
     * Register the guard
136
     *
137
     * @return void
138
     */
139
    protected function registerGuards()
140
    {
141
        $this->app['auth']->extend('authServiceToken', function(\Illuminate\Foundation\Application $app) {
142
            return $app->make(TokenGuard::class, [
143
                'provider' => $app['auth']->createUserProvider($app['config']['auth.guards.authService.provider']),
144
                'request' => $app['request'],
145
                'authService' => $app['authService'],
146
                'client' => $app[Client::class]
147
            ]);
148
        });
149
150
        // Register the fallback driver if service is disabled
151
        if(!$this->app->runningInConsole() && !$this->enabled()) {
152
            $this->app['auth']->shouldUse('fallback');
153
        }
154
    }
155
156
    /**
157
     * Checks whether service is enabled
158
     *
159
     * @return bool
160
     */
161
    private function enabled(): bool
162
    {
163
        return config('connector.auth.enabled') === true;
164
    }
165
}