Completed
Push — master ( 0c603e...505b06 )
by Sergi Tur
02:19
created

LaravelSocialServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Acacha\LaravelSocial\Providers;
4
5
use Acacha\LaravelSocial\Facades\LaravelSocial;
6
use Acacha\LaravelSocial\Repositories\EloquentSocialUserRepository;
7
use Acacha\LaravelSocial\Repositories\SocialUserRepository;
8
use Acacha\LaravelSocial\Services\ConfigureSocialServicesManager;
9
use Acacha\LaravelSocial\Services\LaravelSocialiteService;
10
use Acacha\LaravelSocial\Services\OAuthApp;
11
use Acacha\LaravelSocial\SocialProviders\SocialProviderManager;
12
use Illuminate\Console\DetectsApplicationNamespace;
13
use Illuminate\Support\ServiceProvider;
14
use Laravel\Socialite\SocialiteServiceProvider;
15
16
/**
17
 * Class LaravelSocialServiceProvider.
18
 */
19
class LaravelSocialServiceProvider extends ServiceProvider
20
{
21
    use DetectsApplicationNamespace;
22
23
    /**
24
     * Enabled social providers.
25
     *
26
     * @var array
27
     */
28
    public $enabled = ['Github','Facebook','Google','Twitter'];
29
30
    /**
31
     * Register the application services.
32
     */
33
    public function register()
34
    {
35
        if (!defined('LARAVELSOCIAL_PATH')) {
36
            define('LARAVELSOCIAL_PATH', realpath(__DIR__.'/../../'));
37
        }
38
39
        if ($this->app->runningInConsole()) {
40
            $this->commands([\Acacha\LaravelSocial\Console\Commands\Social::class]);
41
        }
42
43
        $this->app->bind('LaravelSocial', function () {
44
            return new \Acacha\LaravelSocial\LaravelSocial();
45
        });
46
47
        $this->registerSocialiteProvider();
48
49
        $this->registerSocialProviderManager();
50
51
        $this->registerConfigureSocialServicesManager();
52
53
        $this->registerOAuthApp();
54
55
        $this->registerSocialProviders();
56
57
        $this->registerLaravelSocialiteService();
58
59
        $this->registerSocialUsersRepository();
60
    }
61
62
    /**
63
     * Bootstrap the application services.
64
     */
65
    public function boot()
66
    {
67
        $this->defineRoutes();
68
        $this->loadMigrations();
69
    }
70
71
    /**
72
     * Define the AdminLTETemplate routes.
73
     */
74
    protected function defineRoutes()
75
    {
76
        if (!$this->app->routesAreCached()) {
77
            $router = app('router');
78
79
            $router->group(['namespace' => 'Acacha\LaravelSocial\Http\Controllers'], function () {
80
                require __DIR__.'/../Http/routes.php';
81
            });
82
        }
83
    }
84
85
    /**
86
     * Register social provider manager.
87
     */
88
    private function registerSocialProviderManager()
89
    {
90
        $this->app->singleton(\Acacha\LaravelSocial\Contracts\Factory::class, function ($app) {
91
            return new SocialProviderManager($app);
92
        });
93
    }
94
95
    /**
96
     * Register socialite service provider.
97
     */
98
    private function registerSocialiteProvider()
99
    {
100
        $this->app->register(SocialiteServiceProvider::class);
101
    }
102
103
    /**
104
     * Register configure social services manager.
105
     */
106
    private function registerConfigureSocialServicesManager()
107
    {
108
        $this->app->singleton(\Acacha\LaravelSocial\Contracts\ConfigureSocialServicesFactory::class,
109
            function ($app) {
110
                return new ConfigureSocialServicesManager($app);
111
            });
112
    }
113
114
    /**
115
     * Register OAuth App.
116
     */
117
    private function registerOAuthApp()
118
    {
119
        $oauth = new OAuthApp();
120
        $this->app->instance(\Acacha\LaravelSocial\Services\OAuthApp::class, $oauth);
121
    }
122
123
    /**
124
     * Register Laravel Socialite service.
125
     */
126
    private function registerLaravelSocialiteService()
127
    {
128
        $this->app->bind(\Acacha\LaravelSocial\Services\LaravelSocialiteService::class, function ($app) {
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...
129
            return new LaravelSocialiteService(
130
                new \Acacha\Filesystem\Compiler\StubFileCompiler(),
131
                new \Acacha\Filesystem\Filesystem()
132
            );
133
        });
134
    }
135
136
    /**
137
     * Register social providers.
138
     */
139
    private function registerSocialProviders()
140
    {
141
        foreach ($this->enabled as $provider) {
142
            $providerClass =  $provider . 'SocialProvider';
143
            $this->app->bind($providerClass, function ($app) use ($providerClass) {
144
                $providerClassWithNamespace = 'Acacha\LaravelSocial\SocialProviders\\' . $providerClass;
145
                return new $providerClassWithNamespace($app->make('Laravel\Socialite\Contracts\Factory'));
146
            });
147
        }
148
    }
149
150
    /**
151
     * Load migrations.
152
     */
153
    private function loadMigrations()
154
    {
155
        $this->loadMigrationsFrom( LARAVELSOCIAL_PATH .'/database/migrations');
156
    }
157
158
    /**
159
     * Register social users repository.
160
     */
161
    private function registerSocialUsersRepository()
162
    {
163
        $this->app->bind(
164
            SocialUserRepository::class,
165
            EloquentSocialUserRepository::class
166
        );
167
    }
168
}
169