Completed
Push — master ( 3517c3...5a579f )
by Evgenii
10s
created

Subscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 118
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A onConnect() 0 13 1
A onMessage() 0 14 3
A onDisconnect() 0 4 1
A onError() 0 4 1
A subscribe() 0 4 1
1
<?php
2
3
namespace Laratrade\GDAX\WebSocket;
4
5
use Exception;
6
use Illuminate\Config\Repository as RepositoryContract;
7
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
8
use Laratrade\GDAX\WebSocket\Contracts\Subscriber as SubscriberContract;
9
use Psr\Log\LoggerInterface as LoggerContract;
10
use Ratchet\Client\WebSocket;
11
use Ratchet\RFC6455\Messaging\MessageInterface as MessageContract;
12
use React\Promise\PromiseInterface as PromiseContract;
13
14
class Subscriber implements SubscriberContract
15
{
16
    /**
17
     * The logger instance.
18
     *
19
     * @var LoggerContract
20
     */
21
    protected $logger;
22
23
    /**
24
     * The config repository instance.
25
     *
26
     * @var RepositoryContract
27
     */
28
    protected $config;
29
30
    /**
31
     * The dispatcher instance.
32
     *
33
     * @var DispatcherContract
34
     */
35
    protected $dispatcher;
36
37
    /**
38
     * Create a new subscriber instance.
39
     *
40
     * @param LoggerContract     $logger
41
     * @param RepositoryContract $config
42
     * @param DispatcherContract $dispatcher
43
     */
44 16
    public function __construct(
45
        LoggerContract $logger,
46
        RepositoryContract $config,
47
        DispatcherContract $dispatcher
48
    ) {
49 16
        $this->logger     = $logger;
50 16
        $this->config     = $config;
51 16
        $this->dispatcher = $dispatcher;
52 16
    }
53
54
    /**
55
     * Handle the connect event.
56
     *
57
     * @param WebSocket $webSocket
58
     *
59
     * @return void
60
     */
61 2
    public function onConnect(WebSocket $webSocket): void
62
    {
63 2
        $this->logger->info('websocket connect');
64
65 2
        $webSocket->on('message', [$this, 'onMessage']);
66 2
        $webSocket->on('close', [$this, 'onDisconnect']);
67
68 2
        $webSocket->send(json_encode([
69 2
            'type'        => 'subscribe',
70 2
            'product_ids' => $this->config->get('gdax-websocket.products'),
71 2
            'channels'    => $this->config->get('gdax-websocket.channels'),
72
        ]));
73 2
    }
74
75
    /**
76
     * Handle the message event.
77
     *
78
     * @param MessageContract $message
79
     *
80
     * @return void
81
     */
82 4
    public function onMessage(MessageContract $message): void
83
    {
84 4
        $payload = json_decode($message->getPayload());
85
86 4
        $this->logger->info('websocket message', compact('payload'));
87
88 4
        if (!isset($payload->type)) {
89
            return;
90
        }
91
92 4
        $event = $this->config->get(sprintf('gdax-websocket.events.%s', $payload->type));
93
94 4
        $event && $this->dispatcher->dispatch(new $event($payload));
95 4
    }
96
97
    /**
98
     * Handle the disconnect event.
99
     *
100
     * @param int|null    $code
101
     * @param string|null $reason
102
     *
103
     * @return void
104
     */
105 2
    public function onDisconnect(int $code = null, string $reason = null): void
106
    {
107 2
        $this->logger->warning('websocket disconnect', compact('code', 'reason'));
108 2
    }
109
110
    /**
111
     * Handle the error event.
112
     *
113
     * @param Exception $exception
114
     *
115
     * @return void
116
     */
117 2
    public function onError(Exception $exception): void
118
    {
119 2
        $this->logger->error('websocket error', compact('exception'));
120 2
    }
121
122
    /**
123
     * Register the listeners for the subscriber.
124
     *
125
     * @param PromiseContract $connection
126
     */
127 2
    public function subscribe(PromiseContract $connection): void
128
    {
129 2
        $connection->then([$this, 'onConnect'], [$this, 'onError']);
130 2
    }
131
}
132