WebSocketsServiceProvider::registerRouter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
6
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
7
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
8
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
9
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
10
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
11
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
12
use BeyondCode\LaravelWebSockets\Queue\AsyncRedisConnector;
13
use BeyondCode\LaravelWebSockets\Server\Router;
14
use Illuminate\Support\Facades\Gate;
15
use Illuminate\Support\Facades\Queue;
16
use Illuminate\Support\Facades\Route;
17
use Illuminate\Support\ServiceProvider;
18
19
class WebSocketsServiceProvider extends ServiceProvider
20
{
21
    /**
22
     * Boot the service provider.
23
     *
24
     * @return void
25
     */
26
    public function boot()
27
    {
28
        $this->publishes([
29
            __DIR__.'/../config/websockets.php' => config_path('websockets.php'),
30
        ], 'config');
31
32
        $this->mergeConfigFrom(
33
            __DIR__.'/../config/websockets.php', 'websockets'
34
        );
35
36
        $this->publishes([
37
            __DIR__.'/../database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php' => database_path('migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php'),
38
            __DIR__.'/../database/migrations/0000_00_00_000000_rename_statistics_counters.php' => database_path('migrations/0000_00_00_000000_rename_statistics_counters.php'),
39
        ], 'migrations');
40
41
        $this->registerAsyncRedisQueueDriver();
42
43
        $this->registerRouter();
44
45
        $this->registerManagers();
46
47
        $this->registerStatistics();
48
49
        $this->registerDashboard();
50
51
        $this->registerCommands();
52
    }
53
54
    /**
55
     * Register the service provider.
56
     *
57
     * @return void
58
     */
59
    public function register()
60
    {
61
        //
62
    }
63
64
    /**
65
     * Register the async, non-blocking Redis queue driver.
66
     *
67
     * @return void
68
     */
69
    protected function registerAsyncRedisQueueDriver()
70
    {
71
        Queue::extend('async-redis', function () {
72
            return new AsyncRedisConnector($this->app['redis']);
73
        });
74
    }
75
76
    /**
77
     * Register the statistics-related contracts.
78
     *
79
     * @return void
80
     */
81
    protected function registerStatistics()
82
    {
83
        $this->app->singleton(StatisticsStore::class, function ($app) {
84
            $config = $app['config']['websockets'];
85
            $class = $config['statistics']['store'];
86
87
            return new $class;
88
        });
89
90
        $this->app->singleton(StatisticsCollector::class, function ($app) {
91
            $config = $app['config']['websockets'];
92
            $replicationMode = $config['replication']['mode'] ?? 'local';
93
94
            $class = $config['replication']['modes'][$replicationMode]['collector'];
95
96
            return new $class;
97
        });
98
    }
99
100
    /**
101
     * Regsiter the dashboard components.
102
     *
103
     * @return void
104
     */
105
    protected function registerDashboard()
106
    {
107
        $this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
108
109
        $this->registerDashboardRoutes();
110
        $this->registerDashboardGate();
111
    }
112
113
    /**
114
     * Register the package commands.
115
     *
116
     * @return void
117
     */
118
    protected function registerCommands()
119
    {
120
        $this->commands([
121
            Console\Commands\StartServer::class,
122
            Console\Commands\RestartServer::class,
123
            Console\Commands\CleanStatistics::class,
124
            Console\Commands\FlushCollectedStatistics::class,
125
        ]);
126
    }
127
128
    /**
129
     * Register the routing.
130
     *
131
     * @return void
132
     */
133
    protected function registerRouter()
134
    {
135
        $this->app->singleton('websockets.router', function () {
136
            return new Router;
137
        });
138
    }
139
140
    /**
141
     * Register the managers for the app.
142
     *
143
     * @return void
144
     */
145
    protected function registerManagers()
146
    {
147
        $this->app->singleton(Contracts\AppManager::class, function ($app) {
148
            $config = $app['config']['websockets'];
149
150
            return $this->app->make($config['managers']['app']);
151
        });
152
    }
153
154
    /**
155
     * Register the dashboard routes.
156
     *
157
     * @return void
158
     */
159
    protected function registerDashboardRoutes()
160
    {
161
        Route::group([
162
            'domain' => config('websockets.dashboard.domain'),
163
            'prefix' => config('websockets.dashboard.path'),
164
            'as' => 'laravel-websockets.',
165
            'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]),
166
        ], function () {
167
            Route::get('/', ShowDashboard::class)->name('dashboard');
168
            Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics');
169
            Route::post('/auth', AuthenticateDashboard::class)->name('auth');
170
            Route::post('/event', SendMessage::class)->name('event');
171
        });
172
    }
173
174
    /**
175
     * Register the dashboard gate.
176
     *
177
     * @return void
178
     */
179
    protected function registerDashboardGate()
180
    {
181
        Gate::define('viewWebSocketsDashboard', function ($user = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

181
        Gate::define('viewWebSocketsDashboard', function (/** @scrutinizer ignore-unused */ $user = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
            return $this->app->environment('local');
183
        });
184
    }
185
}
186