Completed
Push — master ( 413e7c...13a6e1 )
by Sergi Tur
09:46
created

registerGoogleServiceProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\Users\Providers;
4
5
use Acacha\Stateful\Providers\StatefulServiceProvider;
6
use Illuminate\Foundation\AliasLoader;
7
use PulkitJalan\Google\Facades\Google;
8
use PulkitJalan\Google\GoogleServiceProvider;
9
use Acacha\Users\Models\UserInvitation;
10
use Acacha\Users\Observers\UserInvitationObserver;
11
use Acacha\Users\Observers\UserObserver;
12
use AcachaUsers;
13
use App\User;
14
use Broadcast;
15
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
16
use Laravel\Passport\Passport;
17
use Laravel\Passport\PassportServiceProvider;
18
use Spatie\Permission\PermissionServiceProvider;
19
20
/**
21
 * Class UsersManagementServiceProvider.
22
 *
23
 * @package Acacha\Users\Providers
24
 */
25
class UsersManagementServiceProvider extends EventServiceProvider
26
{
27
28
    /**
29
     * The event listener mappings for the application.
30
     *
31
     * @var array
32
     */
33
    protected $listen = [
34
        \Acacha\Users\Events\UserHasBeenMigrated::class => [
35
            \Acacha\Users\Listeners\PersistUserMigrationInDatabase::class,
36
        ],
37
    ];
38
39
    /**
40
     * Register the application services.
41
     */
42
    public function register() {
43
        if (!defined('ACACHA_USERS_PATH')) {
44
            define('ACACHA_USERS_PATH', realpath(__DIR__.'/../../'));
45
        }
46
        $this->app->bind('AcachaUsers', function () {
47
            return new \Acacha\Users\AcachaUsers();
48
        });
49
50
        if (config('acacha_users.register_spatie_permission_service_provider', true)) {
51
            $this->registerSpatiePermissionServiceProvider();
52
        }
53
54
        if (config('acacha_users.register_laravel_passport_service_provider', true)) {
55
            $this->registerLaravelPassportServiceProvider();
56
            Passport::routes();
57
        }
58
59
        if (config('acacha_users.register_acacha_stateful_service_provider', true)) {
60
            $this->registerAcachaStatefulServiceProvider();
61
        }
62
63
        if (config('acacha_users.register_google_service_provider', true)) {
64
            $this->registerGoogleServiceProvider();
65
        }
66
    }
67
68
    /**
69
     * Register Spatie permissions service Provider.
70
     */
71
    protected function registerSpatiePermissionServiceProvider()
72
    {
73
        $this->app->register(PermissionServiceProvider::class);
74
75
        //TODO: publish spatie config file
76
        //Add Trait to App/User
77
    }
78
79
    /**
80
     * Register Laravel passport service Provider.
81
     */
82
    protected function registerLaravelPassportServiceProvider()
83
    {
84
        $this->app->register(PassportServiceProvider::class);
85
86
        //TODO: execute php artisan passport:install
87
        //Add Trait to App/User HasApiTokens
88
    }
89
90
    /**
91
     * Register Acacha Stateful service Provider.
92
     */
93
    protected function registerAcachaStatefulServiceProvider()
94
    {
95
        $this->app->register(StatefulServiceProvider::class);
96
    }
97
98
    /**
99
     * Register Google Service Provider.
100
     */
101
    protected function registerGoogleServiceProvider()
102
    {
103
        $this->app->register(GoogleServiceProvider::class);
104
105
        $this->app->booting(function() {
106
            $loader = AliasLoader::getInstance();
107
            $loader->alias('Google', Google::class);
108
        });
109
110
        app()->extend(\PulkitJalan\Google\Client::class, function ($command, $app) {
111
            $config = $app['config']['google'];
112
            return new \PulkitJalan\Google\Client($config, config('users.google_apps_admin_user_email'));
113
        });
114
    }
115
116
    /**
117
     * Bootstrap the application services.
118
     */
119
    public function boot() {
120
        //Parent will be responsible of registering events using $listen property
121
        parent::boot();
122
123
        $this->defineRoutes();
124
125
        //Publish
126
        $this->publishLanguages();
127
        $this->publishViews();
128
        $this->publishConfigAuth();
129
        $this->publishFactories();
130
131
        $this->loadMigrations();
132
        $this->publishSeeds();
133
134
        $this->defineObservers();
135
        $this->configureBroadcastChannels();
136
137
    }
138
139
    /**
140
     * Configure broadcast channels.
141
     *
142
     */
143
    protected function configureBroadcastChannels()
144
    {
145
        Broadcast::channel('acacha-users', function ($user) {
146
            return $user->can('subscribe-to-users-broadcast-channel');
147
        });
148
    }
149
150
151
    /**
152
     * Define the AdminLTETemplate routes.
153
     */
154
    protected function defineRoutes()
155
    {
156
        if (!$this->app->routesAreCached()) {
157
            $router = app('router');
158
            $router->group(['namespace' => 'Acacha\Users\Http\Controllers'], function () {
159
                require __DIR__.'/../Http/routes.php';
160
            });
161
        }
162
    }
163
164
    /**
165
     * Publish package language to Laravel project.
166
     */
167
    private function publishLanguages()
168
    {
169
        $this->loadTranslationsFrom(ACACHA_USERS_PATH.'/resources/lang/', 'acacha_users_lang');
170
171
        $this->publishes(AcachaUsers::languages(), 'acacha_users_lang');
172
    }
173
174
    /**
175
     * Publish package views to Laravel project.
176
     */
177
    private function publishViews()
178
    {
179
        $this->loadViewsFrom(ACACHA_USERS_PATH.'/resources/views/', 'acacha_users');
180
181
        $this->publishes(AcachaUsers::views(), 'acacha_users_views');
182
    }
183
184
    /**
185
     * Publish config auth.
186
     */
187
    private function publishConfigAuth() {
188
        $this->publishes(AcachaUsers::configAuth(), 'acacha_users_config');
189
    }
190
191
    /**
192
     * Load package migrations.
193
     */
194
    public function loadMigrations()
195
    {
196
        $this->loadMigrationsFrom(ACACHA_USERS_PATH .'/database/migrations');
197
    }
198
199
    /**
200
     * Publish seeds.
201
     */
202
    private function publishSeeds() {
203
        $this->publishes(AcachaUsers::seeds(), 'acacha_users_seeds');
204
    }
205
206
    /**
207
     * Publish factories.
208
     */
209
    private function publishFactories() {
210
        $this->publishes(AcachaUsers::factories(), 'acacha_users_factories');
211
    }
212
213
    /**
214
     * Define observers.
215
     */
216
    public function defineObservers()
217
    {
218
        UserInvitation::observe(UserInvitationObserver::class);
219
        User::observe(UserObserver::class);
220
    }
221
222
}