BitfinexClient   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 86
ccs 28
cts 28
cp 1
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A authConnect() 0 7 1
A getOnReject() 0 3 1
A connect() 0 7 1
A __construct() 0 6 2
A getPublicChannelsUrl() 0 3 1
A getAuthenticatedChannelsUrl() 0 3 1
A createMessage() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Codenixsv\BitfinexWs;
6
7
use Codenixsv\BitfinexWs\Request\Request;
8
use Exception;
9
use Ratchet\RFC6455\Messaging\Message;
10
11
/**
12
 * Class BitfinexClient
13
 * @package Codenixsv\BitfinexWs
14
 */
15
class BitfinexClient
16
{
17
    private const PUBLIC_CHANNELS_URL = 'wss://api-pub.bitfinex.com/ws/2';
18
    private const AUTHENTICATED_CHANNELS_URL = 'wss://api.bitfinex.com/ws/2';
19
20
    /** @var BaseWsClient */
21
    private $client;
22
23
    /** @var callable */
24
    private $onReject;
25
26
    /**
27
     * BitfinexClient constructor.
28
     * @param BaseWsClient|null $client
29
     */
30 5
    public function __construct(?BaseWsClient $client = null)
31
    {
32 5
        $this->client = $client ?: new WsClient();
33
34
        $this->onReject = static function (Exception $exception) {
35 1
            echo $exception->getMessage();
36 1
        };
37 5
    }
38
39
    /**
40
     * @param Request[] $requests
41
     * @param array $events
42
     */
43 2
    public function connect(array $requests, array $events)
44
    {
45 2
        $this->client->connect(
46 2
            $this->getPublicChannelsUrl(),
47 2
            $this->createMessage($requests),
48
            $events,
49 2
            $this->onReject
50
        );
51 2
    }
52
53
    /**
54
     * @param array $requests
55
     * @param array $events
56
     */
57 1
    public function authConnect(array $requests, array $events)
58
    {
59 1
        $this->client->connect(
60 1
            $this->getAuthenticatedChannelsUrl(),
61 1
            $this->createMessage($requests),
62
            $events,
63 1
            $this->onReject
64
        );
65 1
    }
66
67
    /**
68
     * @return string
69
     */
70 2
    public function getPublicChannelsUrl(): string
71
    {
72 2
        return self::PUBLIC_CHANNELS_URL;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    public function getAuthenticatedChannelsUrl(): string
79
    {
80 1
        return self::AUTHENTICATED_CHANNELS_URL;
81
    }
82
83 2
    public function getOnReject(): callable
84
    {
85 2
        return $this->onReject;
86
    }
87
88
    /**
89
     * @param Request[] $requests
90
     * @return Message
91
     */
92 3
    private function createMessage(array $requests): Message
93
    {
94 3
        $message = new Message();
95
96 3
        foreach ($requests as $request) {
97 1
            $message->addFrame($request->getFrame());
98
        }
99
100 3
        return $message;
101
    }
102
}
103