WebSocketServiceProvider::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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