1 | <?php |
||
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) && $event = $this->config->get(sprintf('gdax-websocket.events.%s', $payload->type))) { |
|
89 | 2 | $this->dispatcher->dispatch(new $event($payload)); |
|
90 | } |
||
91 | 4 | } |
|
92 | |||
93 | /** |
||
94 | * Handle the disconnect event. |
||
95 | * |
||
96 | * @param int|null $code |
||
97 | * @param string|null $reason |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | 2 | public function onDisconnect(int $code = null, string $reason = null): void |
|
105 | |||
106 | /** |
||
107 | * Handle the error event. |
||
108 | * |
||
109 | * @param Exception $exception |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | 2 | public function onError(Exception $exception): void |
|
117 | |||
118 | /** |
||
119 | * Register the listeners for the subscriber. |
||
120 | * |
||
121 | * @param PromiseContract $connection |
||
122 | */ |
||
123 | 2 | public function subscribe(PromiseContract $connection): void |
|
127 | } |
||
128 |