Completed
Push — master ( 981faf...8174df )
by Sergi Tur
07:45
created

UsersManagementServiceProvider::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 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
            $this->defineWebRoutes($router);
159
            $this->defineWebRoutes($router);
160
        }
161
    }
162
163
    /**
164
     * Define web routes.
165
     *
166
     * @param $router
167
     */
168
    protected function defineWebRoutes($router)
169
    {
170
        $router->group(['namespace' => 'Acacha\Users\Http\Controllers'], function () {
171
            require ACACHA_USERS_PATH.'/routes/web.php';
172
        });
173
    }
174
175
    /**
176
     * Define api routes.
177
     *
178
     * @param $router
179
     */
180
    protected function defineApiRoutes($router)
181
    {
182
        $router->group(['namespace' => 'Acacha\Users\Http\Controllers'], function () {
183
            require ACACHA_USERS_PATH.'/routes/api.php';
184
        });
185
    }
186
187
    /**
188
     * Publish package language to Laravel project.
189
     */
190
    private function publishLanguages()
191
    {
192
        $this->loadTranslationsFrom(ACACHA_USERS_PATH.'/resources/lang/', 'acacha_users_lang');
193
194
        $this->publishes(AcachaUsers::languages(), 'acacha_users_lang');
195
    }
196
197
    /**
198
     * Publish package views to Laravel project.
199
     */
200
    private function publishViews()
201
    {
202
        $this->loadViewsFrom(ACACHA_USERS_PATH.'/resources/views/', 'acacha_users');
203
204
        $this->publishes(AcachaUsers::views(), 'acacha_users');
205
    }
206
207
    /**
208
     * Publish config auth.
209
     */
210
    private function publishConfigAuth() {
211
        $this->publishes(AcachaUsers::configAuth(), 'acacha_users_config');
212
    }
213
214
    /**
215
     * Load package migrations.
216
     */
217
    public function loadMigrations()
218
    {
219
        $this->loadMigrationsFrom(ACACHA_USERS_PATH .'/database/migrations');
220
    }
221
222
    /**
223
     * Publish seeds.
224
     */
225
    private function publishSeeds() {
226
        $this->publishes(AcachaUsers::seeds(), 'acacha_users_seeds');
227
    }
228
229
    /**
230
     * Publish factories.
231
     */
232
    private function publishFactories() {
233
        $this->publishes(AcachaUsers::factories(), 'acacha_users_factories');
234
    }
235
236
    /**
237
     * Define observers.
238
     */
239
    public function defineObservers()
240
    {
241
        UserInvitation::observe(UserInvitationObserver::class);
242
        User::observe(UserObserver::class);
243
    }
244
245
}