Passed
Push — master ( fbecfe...63322c )
by Vladislav
04:09 queued 01:55
created

BybitAPI::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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
     * Параметры подключения к ByBit
18
     *
19
     * @var Credentials
20
     */
21
    protected Credentials $credentials;
22
23
    /**
24
     * Конструктор приложения.
25
     *
26
     * @param string|null $host
27
     */
28
    public function __construct(string $host = null)
29
    {
30
        $this->credentials = new Credentials();
31
        $this->credentials->setHost($host);
0 ignored issues
show
Bug introduced by
It seems like $host can also be of type null; however, parameter $host of Carpenstar\ByBitAPI\Core...\Credentials::setHost() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $this->credentials->setHost(/** @scrutinizer ignore-type */ $host);
Loading history...
32
    }
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
     *
83
     * @return SocketAPI
84
     * @throws SDKException
85
     */
86
    public function websockets(): SocketAPI
87
    {
88
        return new SocketAPI($this->credentials);
89
    }
90
91
92
    // TODO publicEndpoint, privateEndpoint, endpoint отделить в модуль rest
93
    /**
94
     * @param string $endpointClassName
95
     * @param IParametersInterface|null $parameters
96
     * @return IResponseInterface
97
     * @throws SDKException
98
     */
99
    public function publicEndpoint(string $endpointClassName, ?IParametersInterface $parameters = null): IEndpointInterface
100
    {
101
        if (empty($this->credentials->getHost())) {
102
            throw new SDKException("Host must be specified");
103
        }
104
105
        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...
106
    }
107
108
    /**
109
     * @param string $endpointClassName
110
     * @param IParametersInterface|null $parameters
111
     * @return IResponseInterface
112
     * @throws SDKException
113
     */
114
    public function privateEndpoint(string $endpointClassName, ?IParametersInterface $parameters = null): IEndpointInterface
115
    {
116
        if (empty($this->credentials->getHost())) {
117
            throw new SDKException("Host must be specified");
118
        }
119
        if (empty($this->credentials->getApiKey())) {
120
            throw new SDKException("Api key must be specified");
121
        }
122
        if (empty($this->credentials->getSecret())) {
123
            throw new SDKException("Client secret must be specified");
124
        }
125
126
        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...
127
    }
128
129
    /**
130
     * @param string $endpointClassName
131
     * @param AbstractParameters $params
132
     * @return IEndpointInterface
133
     * @throws \Exception
134
     */
135
    private function endpoint(string $endpointClassName, AbstractParameters $parameters = null): IEndpointInterface
136
    {
137
        return RestBuilder::make($endpointClassName, $this->credentials, $parameters);
138
    }
139
}
140