Completed
Pull Request — master (#447)
by Alexandru
01:22
created

WebSocketsServiceProvider::configurePubSub()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use BeyondCode\LaravelWebSockets\Apps\AppManager;
6
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
7
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
8
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
9
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
10
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
11
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
12
use BeyondCode\LaravelWebSockets\Server\Router;
13
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
14
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
15
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
16
use Illuminate\Broadcasting\BroadcastManager;
17
use Illuminate\Support\Facades\Gate;
18
use Illuminate\Support\Facades\Route;
19
use Illuminate\Support\ServiceProvider;
20
use Psr\Log\LoggerInterface;
21
use Pusher\Pusher;
22
23
class WebSocketsServiceProvider extends ServiceProvider
24
{
25
    /**
26
     * Boot the service provider.
27
     *
28
     * @return void
29
     */
30
    public function boot()
31
    {
32
        $this->publishes([
33
            __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
34
        ], 'config');
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
        ], 'migrations');
39
40
        $this->registerDashboardRoutes()
41
            ->registerDashboardGate();
42
43
        $this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
44
45
        $this->commands([
46
            Console\StartWebSocketServer::class,
47
            Console\CleanStatistics::class,
48
            Console\RestartWebSocketServer::class,
49
        ]);
50
51
        $this->configurePubSub();
52
    }
53
54
    /**
55
     * Register the service provider.
56
     *
57
     * @return void
58
     */
59
    public function register()
60
    {
61
        $this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
62
63
        $this->app->singleton('websockets.router', function () {
64
            return new Router();
65
        });
66
67
        $this->app->singleton(ChannelManager::class, function () {
68
            $channelManager = config('websockets.managers.channel', ArrayChannelManager::class);
69
70
            return new $channelManager;
71
        });
72
73
        $this->app->singleton(AppManager::class, function () {
74
            return $this->app->make(config('websockets.managers.app'));
75
        });
76
77
        $this->app->singleton(StatisticsDriver::class, function () {
78
            $driver = config('websockets.statistics.driver');
79
80
            return $this->app->make(
81
                config('websockets.statistics')[$driver]['driver']
82
                ??
83
                \BeyondCode\LaravelWebSockets\Statistics\Drivers\DatabaseDriver::class
84
            );
85
        });
86
    }
87
88
    /**
89
     * Configure the PubSub replication.
90
     *
91
     * @return void
92
     */
93
    protected function configurePubSub()
94
    {
95
        $this->app->make(BroadcastManager::class)->extend('websockets', function ($app, array $config) {
96
            $pusher = new Pusher(
97
                $config['key'], $config['secret'],
98
                $config['app_id'], $config['options'] ?? []
99
            );
100
101
            if ($config['log'] ?? false) {
102
                $pusher->setLogger($this->app->make(LoggerInterface::class));
103
            }
104
105
            return new RedisPusherBroadcaster(
106
                $pusher,
107
                $config['app_id'],
108
                $this->app->make('redis')
109
            );
110
        });
111
    }
112
113
    /**
114
     * Register the dashboard routes.
115
     *
116
     * @return void
117
     */
118
    protected function registerDashboardRoutes()
119
    {
120
        Route::group([
121
            'prefix' => config('websockets.dashboard.path'),
122
            'as' => 'laravel-websockets.',
123
            'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]),
124
        ], function () {
125
            Route::get('/', ShowDashboard::class)->name('dashboard');
126
            Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics');
127
            Route::post('/auth', AuthenticateDashboard::class)->name('auth');
128
            Route::post('/event', SendMessage::class)->name('event');
129
        });
130
131
        return $this;
132
    }
133
134
    /**
135
     * Register the dashboard gate.
136
     *
137
     * @return void
138
     */
139
    protected function registerDashboardGate()
140
    {
141
        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...
142
            return $this->app->environment('local');
143
        });
144
145
        return $this;
146
    }
147
}
148