Completed
Push — master ( 4d64c5...0a88d7 )
by Sergi Tur
03:50
created

UsersServiceProvider::defineApiRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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