Completed
Pull Request — master (#79)
by
unknown
01:44
created

WebSocketsServiceProvider::publishMigrations()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 12
Ratio 75 %

Importance

Changes 0
Metric Value
dl 12
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use Illuminate\Support\Facades\Gate;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Support\ServiceProvider;
8
use BeyondCode\LaravelWebSockets\Server\Router;
9
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
10
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
11
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
12
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
13
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
14
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
15
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
16
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
17
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics;
18
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
19
20
class WebSocketsServiceProvider extends ServiceProvider
21
{
22
    public function boot()
23
    {
24
        $this->publishes([
25
            __DIR__ . '/../config/websockets.php' => base_path('config/websockets.php'),
26
        ], 'config');
27
28
        $this->publishMigrations();
29
30
        $this->registerRoutes()->registerDashboardGate();
31
32
        $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'websockets');
33
34
        $this->commands([
35
            Console\StartWebSocketServer::class,
36
            Console\CleanStatistics::class,
37
        ]);
38
    }
39
40
    public function publishMigrations()
41
    {
42 View Code Duplication
        if ( ! class_exists('CreateWebSocketsStatisticsEntries')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            $this->publishes([
44
                __DIR__ . '/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/' . date('Y_m_d_His',
45
                        time()) . '_create_websockets_statistics_entries_table.php'),
46
            ], 'migrations');
47
        }
48
49 View Code Duplication
        if ( ! class_exists('CreateWebSocketsApps')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            $this->publishes([
51
                __DIR__ . '/../database/migrations/create_websockets_apps_table.php.stub' => database_path('migrations/' . date('Y_m_d_His',
52
                        time()) . '_create_websockets_apps_table.php'),
53
            ], 'migrations');
54
        }
55
    }
56
57
    public function register()
58
    {
59
        $this->mergeConfigFrom(__DIR__ . '/../config/websockets.php', 'websockets');
60
61
        $this->app->singleton('websockets.router', function () {
62
            return new Router();
63
        });
64
65
        $this->app->singleton(ChannelManager::class, function () {
66
            return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager')) ? app(config('websockets.channel_manager')) : new ArrayChannelManager();
67
        });
68
69
        $this->app->singleton(AppProvider::class, function () {
70
            return app(config('websockets.app_provider'));
71
        });
72
    }
73
74
    protected function registerRoutes()
75
    {
76
        Route::prefix(config('websockets.path'))->group(function () {
77
            Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () {
78
                Route::get('/', ShowDashboard::class);
79
                Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']);
80
                Route::post('auth', AuthenticateDashboard::class);
81
                Route::post('event', SendMessage::class);
82
            });
83
84
            Route::middleware(AuthorizeStatistics::class)->group(function () {
85
                Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
86
            });
87
        });
88
89
        return $this;
90
    }
91
92
    protected function registerDashboardGate()
93
    {
94
        Gate::define('viewWebSocketsDashboard', function ($user = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

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

Loading history...
95
            return app()->environment('local');
96
        });
97
98
        return $this;
99
    }
100
}
101