Completed
Push — master ( 2e294b...087ce9 )
by Renato
06:42
created

SCServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace SocketCluster\Laravel;
4
5
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
6
use Illuminate\Broadcasting\BroadcastManager;
7
use WebSocket\Client as WebSocketClient;
8
use SocketCluster\SocketCluster;
9
10
class SCServiceProvider extends BaseServiceProvider
11
{
12
    /**
13
     * Indicates if loading of the provider is deferred.
14
     *
15
     * @var bool
16
     */
17
    protected $defer = false;
18
    
19
    /**
20
     * Register bindings in the container.
21
     *
22
     * @return void
23
     */
24
    public function register()
25
    {
26
        $this->registerSocketCluster();
27
    }
28
29
    /**
30
     * Register SocketCluster
31
     *
32
     * @return void
33
     */
34
    protected function registerSocketCluster()
35
    {
36
        $this->app->singleton('SocketCluster', function ($app) {
37
            $config = $app['config']['broadcasting']['connections']['socketcluster'];
38
39
            if (empty($config['uri'])) {
40
                $scheme = $config['secure']==true ? 'wss' : 'ws';
41
                $host   = trim($config['host'], "/");
42
                $port   = !empty($config['port']) ? ":".$config['port'] : '';
43
                $path   = trim($config['path'], "/");
44
                $path   = !empty($path) ? $path . "/" : '';
45
                $config['uri'] = sprintf("%s://%s%s/%s", $scheme, $host, $port, $path);
46
            }
47
            
48
            $websocket  = new WebSocketClient($config['uri']);
49
            return new SocketCluster($websocket);
50
        });
51
52
        $this->app->alias('SocketCluster', SocketCluster::class);
53
    }
54
55
    /**
56
     * Register new BroadcastManager in boot
57
     *
58
     * @return void
59
     */
60
    public function boot()
61
    {
62
        $this->app
63
            ->make(BroadcastManager::class)
64
            ->extend('socketcluster', function ($app) {
65
                return new SCBroadcaster($app['SocketCluster']);
66
            });
67
    }
68
69
    /**
70
     * Get the services provided by the provider.
71
     *
72
     * @return array
73
     */
74
    public function provides()
75
    {
76
        return ['SocketCluster'];
77
    }
78
}
79