Completed
Pull Request — master (#411)
by
unknown
01:45 queued 28s
created

WebSocketsServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 7
dl 0
loc 71
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
6
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
7
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
8
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
9
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
10
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
11
use BeyondCode\LaravelWebSockets\Server\Router;
12
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
13
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics;
14
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
15
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
16
use Illuminate\Support\Facades\Gate;
17
use Illuminate\Support\Facades\Route;
18
use Illuminate\Support\ServiceProvider;
19
use Illuminate\Support\Facades\Schema;
20
21
class WebSocketsServiceProvider extends ServiceProvider
22
{
23
    public function boot()
24
    {
25
        $this->publishes([
26
            __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
27
        ], 'config');
28
29
        if (!Schema::hasTable('websockets_statistics_entries')) 
30
            $this->publishes([
31
                __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_statistics_entries_table.php'),
32
            ], 'migrations');
33
        }
34
35
        $this
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting T_FUNCTION or T_CONST
Loading history...
36
            ->registerRoutes()
37
            ->registerDashboardGate();
38
39
        $this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
40
41
        $this->commands([
42
            Console\StartWebSocketServer::class,
43
            Console\CleanStatistics::class,
44
        ]);
45
    }
46
47
    public function register()
48
    {
49
        $this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
50
51
        $this->app->singleton('websockets.router', function () {
52
            return new Router();
53
        });
54
55
        $this->app->singleton(ChannelManager::class, function () {
56
            return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager'))
57
                ? app(config('websockets.channel_manager')) : new ArrayChannelManager();
58
        });
59
60
        $this->app->singleton(AppProvider::class, function () {
61
            return app(config('websockets.app_provider'));
62
        });
63
    }
64
65
    protected function registerRoutes()
66
    {
67
        Route::prefix(config('websockets.path'))->group(function () {
68
            Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () {
69
                Route::get('/', ShowDashboard::class);
70
                Route::get('/api/{appId}/statistics', [DashboardApiController::class,  'getStatistics']);
71
                Route::post('auth', AuthenticateDashboard::class);
72
                Route::post('event', SendMessage::class);
73
            });
74
75
            Route::middleware(AuthorizeStatistics::class)->group(function () {
76
                Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
77
            });
78
        });
79
80
        return $this;
81
    }
82
83
    protected function registerDashboardGate()
84
    {
85
        Gate::define('viewWebSocketsDashboard', function ($user = null) {
86
            return app()->environment('local');
87
        });
88
89
        return $this;
90
    }
91
}
92