Completed
Push — master ( 58043b...413e7c )
by Sergi Tur
06:39
created

configureBroadcastChannels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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