SCBroadcastServiceProvider::makeSocketCluster()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 5
nop 1
1
<?php
2
3
namespace LaravelSocketCluster;
4
5
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6
use Illuminate\Broadcasting\BroadcastManager;
7
use WebSocket\Client as WebSocketClient;
8
9
class SCBroadcastServiceProvider extends BaseServiceProvider
10
{
11
    /**
12
     * Register bindings in the container.
13
     *
14
     * @return void
15
     */
16
    public function register()
17
    {
18
        //
19
    }
20
21
    /**
22
     * Register new BroadcastManager in boot
23
     *
24
     * @return void
25
     */
26
    public function boot()
27
    {
28
        $self = $this;
29
30
        $this->app
31
            ->make(BroadcastManager::class)
32
            ->extend('socketcluster', function ($app, $config) use ($self) {
33
                return new SCBroadcaster($self->makeSocketCluster($config));
34
            });
35
    }
36
37
    /**
38
     * Register service SocketCluster
39
     *
40
     * @return void
41
     */
42
    protected function makeSocketCluster($config)
43
    {
44
        if (empty($config['uri'])) {
45
            $scheme = $config['secure']==true ? 'wss' : 'ws';
46
            $host   = trim($config['host'], "/");
47
            $port   = !empty($config['port']) ? ":".$config['port'] : '';
48
            $path   = trim($config['path'], "/");
49
            $config['uri'] = sprintf("%s://%s%s/%s/", $scheme, $host, $port, $path);
50
        }
51
        
52
        return new SocketCluster(new WebSocketClient($config['uri']));
53
    }
54
}