Passed
Push — master ( b6588e...364135 )
by Vladislav
10:17 queued 08:02
created

BybitAPI::websockets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Carpenstar\ByBitAPI;
4
5
use Carpenstar\ByBitAPI\Core\Auth\Credentials;
6
use Carpenstar\ByBitAPI\Core\Builders\RestBuilder;
7
use Carpenstar\ByBitAPI\Core\Exceptions\SDKException;
8
use Carpenstar\ByBitAPI\Core\Objects\AbstractParameters;
9
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface;
10
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface;
11
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
12
use Carpenstar\ByBitAPI\WebsocketsV5\SocketAPI;
0 ignored issues
show
Bug introduced by
The type Carpenstar\ByBitAPI\WebsocketsV5\SocketAPI was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class BybitAPI
15
{
16
    /**
17
     * Модуль для подключения к сокет-соединениям
18
     *
19
     * @return SocketAPI
20
     * @throws SDKException
21
     */
22
    public function websockets(): SocketAPI
23
    {
24
        return new SocketAPI($this->credentials);
25
    }
26
27
28
29
30
    // TODO отделить в модуль Credentials
31
32
    protected Credentials $credentials;
33
34
    /**
35
     * @param string $host
36
     * @param string $apiKey
37
     * @param string $secret
38
     * @return $this
39
     */
40
    public function setCredentials(string $host, string $apiKey = '', string $secret = ''): self
41
    {
42
        $this->credentials = (new Credentials())
43
            ->setHost($host)
44
            ->setApiKey($apiKey)
45
            ->setSecret($secret);
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $host
52
     * @return $this
53
     */
54
    public function setHost(string $host): self
55
    {
56
        $this->credentials->setHost($host);
57
        return $this;
58
    }
59
60
    /**
61
     * @param string $apiKey
62
     * @return $this
63
     */
64
    public function setApiKey(string $apiKey): self
65
    {
66
        $this->setApiKey($apiKey);
67
        return $this;
68
    }
69
70
    /**
71
     * @param string $secret
72
     * @return $this
73
     */
74
    public function setSecret(string $secret): self
75
    {
76
        $this->credentials->setSecret($secret);
77
        return $this;
78
    }
79
80
81
82
    // TODO publicEndpoint, privateEndpoint, endpoint отделить в модуль rest
83
    /**
84
     * @param string $endpointClassName
85
     * @param IParametersInterface|null $parameters
86
     * @return IResponseInterface
87
     * @throws SDKException
88
     */
89
    public function publicEndpoint(string $endpointClassName, ?IParametersInterface $parameters = null): IEndpointInterface
90
    {
91
        if (empty($this->credentials->getHost())) {
92
            throw new SDKException("Host must be specified");
93
        }
94
95
        return $this->endpoint($endpointClassName, $parameters);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->endpoint($...ClassName, $parameters) returns the type Carpenstar\ByBitAPI\Core...aces\IEndpointInterface which is incompatible with the documented return type Carpenstar\ByBitAPI\Core...aces\IResponseInterface.
Loading history...
96
    }
97
98
    /**
99
     * @param string $endpointClassName
100
     * @param IParametersInterface|null $parameters
101
     * @return IResponseInterface
102
     * @throws SDKException
103
     */
104
    public function privateEndpoint(string $endpointClassName, ?IParametersInterface $parameters = null): IEndpointInterface
105
    {
106
        if (empty($this->credentials->getHost())) {
107
            throw new SDKException("Host must be specified");
108
        }
109
        if (empty($this->credentials->getApiKey())) {
110
            throw new SDKException("Api key must be specified");
111
        }
112
        if (empty($this->credentials->getSecret())) {
113
            throw new SDKException("Client secret must be specified");
114
        }
115
116
        return $this->endpoint($endpointClassName, $parameters);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->endpoint($...ClassName, $parameters) returns the type Carpenstar\ByBitAPI\Core...aces\IEndpointInterface which is incompatible with the documented return type Carpenstar\ByBitAPI\Core...aces\IResponseInterface.
Loading history...
117
    }
118
119
    /**
120
     * @param string $endpointClassName
121
     * @param AbstractParameters $params
122
     * @return IEndpointInterface
123
     * @throws \Exception
124
     */
125
    private function endpoint(string $endpointClassName, AbstractParameters $parameters = null): IEndpointInterface
126
    {
127
        return RestBuilder::make($endpointClassName, $this->credentials, $parameters);
128
    }
129
}
130