Completed
Push — master ( 5932ed...faf75e )
by Joseph
06:23
created

addNullUserProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace STS\SocialiteAuth;
4
5
use Illuminate\Auth\SessionGuard;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Config;
10
11
class SocialiteAuthServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap the application services.
15
     * @param \Illuminate\Http\Request $request
16
     */
17
    public function boot(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
18
    {
19
        $this->loadRoutesFrom(__DIR__ .'/../routes/web.php');
20
        $this->publishAssetsIfConsole();
21
22
        $this->registerSocialiteGuardMixin();
23
        $this->registerSocialiteAuthDriver();
24
        $this->addGuardToConfig();
25
        $this->configureRedirect();
26
27
        if(config('socialite-auth.match') == false) {
28
            config(['socialite-auth.provider' => 'null']);
29
        };
30
    }
31
32
    /**
33
     * Register the application services.
34
     */
35
    public function register()
36
    {
37
        // Automatically apply the package configuration
38
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'socialite-auth');
39
40
        // Register the main class to use with the facade
41
        $this->app->singleton('socialite-auth', function () {
42
            return new SocialiteAuth(config('socialite-auth'));
43
        });
44
    }
45
46
    protected function publishAssetsIfConsole()
47
    {
48
        if ($this->app->runningInConsole()) {
49
            $this->publishes([
50
                __DIR__.'/../config/config.php' => config_path('socialite-auth.php'),
51
            ], 'config');
52
        }
53
    }
54
55
    protected function registerSocialiteGuardMixin()
56
    {
57
        SessionGuard::mixin(new GuardHelpers());
58
    }
59
60
    protected function registerSocialiteAuthDriver()
61
    {
62
        Auth::extend('socialite', function ($app, $name, array $config) {
63
            return Auth::createSessionDriver($name, $config);
64
        });
65
    }
66
67
    protected function addGuardToConfig()
68
    {
69
        if (!Config::has('auth.guards.socialite')) {
70
            Config::set('auth.guards.socialite', [
71
                'driver' => 'socialite',
72
                'provider' => config('socialite-auth.provider')
73
            ]);
74
        }
75
    }
76
77
    protected function addNullUserProvider()
78
    {
79
        Auth::provider('null', function($app, $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

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

Loading history...
80
            return new NullUserProvider();
81
        });
82
    }
83
84
    /**
85
     * If a redirect URL hasn't been configured, we deduce from the current request
86
     */
87
    protected function configureRedirect()
88
    {
89
        $providerName = config('socialite-auth.driver');
90
91
        if(!config("services.$providerName.redirect")) {
92
            config(["services.$providerName.redirect" => $this->app['request']->root() . "/socialite-auth/callback"]);
93
        }
94
    }
95
}
96