Completed
Push — master ( 59aca9...7c7917 )
by Evgenii
11s
created

WebSocketServiceProvider::offerPublishing()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Laratrade\GDAX\WebSocket;
4
5
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
6
use Illuminate\Support\ServiceProvider;
7
use Laratrade\GDAX\WebSocket\Console\ProcessCommand;
8
use Laratrade\GDAX\WebSocket\Contracts\Subscriber as SubscriberContract;
9
use Psr\Log\LoggerInterface as LoggerContract;
10
use Ratchet\Client\Connector as RatchetConnector;
11
use React\EventLoop\Factory;
12
use React\EventLoop\LoopInterface as LoopContract;
13
use React\Socket\Connector as ReactConnector;
14
15
class WebSocketServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Register the application services.
19
     *
20
     * @return void
21
     */
22 4
    public function register()
23
    {
24 4
        $this->configure()
25 4
             ->offerPublishing()
26 4
             ->registerServices()
27 4
             ->registerCommands();
28 4
    }
29
30
    /**
31
     * Setup the configuration.
32
     *
33
     * @return $this
34
     */
35 4
    protected function configure(): self
36
    {
37 4
        $this->mergeConfigFrom(
38 4
            __DIR__ . '/../config/gdax-websocket.php',
39 4
            'gdax-websocket'
40
        );
41
42 4
        return $this;
43
    }
44
45
    /**
46
     * Setup the resource publishing groups.
47
     *
48
     * @return $this
49
     */
50 4
    protected function offerPublishing(): self
51
    {
52 4
        if ($this->app->runningInConsole()) {
53 4
            $this->publishes([
54 4
                __DIR__ . '/../config/gdax-websocket.php' => config_path('gdax-websocket.php'),
55 4
            ], 'gdax-websocket');
56
        }
57
58 4
        return $this;
59
    }
60
61
    /**
62
     * Register the services.
63
     *
64
     * @return $this
65
     */
66 4
    protected function registerServices(): self
67
    {
68
        // React loop
69
        $this->app->bind(LoopContract::class, function () {
70 4
            return Factory::create();
71 4
        });
72
73
        // Ratchet connector
74
        $this->app->singleton(RatchetConnector::class, function ($app) {
75
            /** @var LoopContract $loop */
76 4
            $loop = $app->make(LoopContract::class);
77
78 4
            $connector = new RatchetConnector($loop, new ReactConnector($loop, [
79 4
                'dns'     => config('gdax-websocket.dns'),
80 4
                'timeout' => config('gdax-websocket.timeout'),
81
            ]));
82
83
            register_shutdown_function(function () use ($loop) {
84
                $loop->run();
85 4
            });
86
87 4
            return $connector;
88 4
        });
89
90
        // Websocket subscriber
91
        $this->app->bind(SubscriberContract::class, function ($app) {
92 4
            return new Subscriber(
93 4
                $app->make(LoggerContract::class),
94 4
                $app->make(DispatcherContract::class),
95 4
                config('gdax-websocket.events')
96
            );
97 4
        });
98
99 4
        return $this;
100
    }
101
102
    /**
103
     * Register the commands.
104
     *
105
     * @return $this
106
     */
107 4
    protected function registerCommands(): self
108
    {
109 4
        if ($this->app->runningInConsole()) {
110 4
            $this->commands([
111 4
                ProcessCommand::class,
112
            ]);
113
        }
114
115 4
        return $this;
116
    }
117
}
118