BittrexClient   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 39
c 2
b 0
f 0
dl 0
loc 155
ccs 40
cts 40
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrivateClient() 0 3 1
A public() 0 3 1
A market() 0 3 1
A createPublicClient() 0 8 1
A createPrivateClient() 0 15 2
A account() 0 3 1
A haveValidCredentials() 0 3 4
A isValidMd5() 0 3 1
A setCredential() 0 8 3
A getPublicClient() 0 3 1
A batch() 0 4 1
A createBatch() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace R3bers\BittrexApi;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\HandlerStack;
9
use R3bers\BittrexApi\Api\Account;
10
use R3bers\BittrexApi\Api\Batch;
11
use R3bers\BittrexApi\Api\Market;
12
use R3bers\BittrexApi\Api\PublicApi;
13
use R3bers\BittrexApi\Exception\InvalidCredentialException;
14
use R3bers\BittrexApi\Middleware\Authentication;
15
16
class BittrexClient
17
{
18
    /**
19
     * Main URL to Bittrex Exchange
20
     */
21
    private const BASE_URI = 'https://api.bittrex.com';
22
23
    private const CLIENT_HEADER = [
24
        'User-Agent' => 'r3bers/bittrex-api/1.3.1',
25
        'Accept' => 'application/json',
26
        'Content-Type' => 'application/json'
27
    ];
28
29
    /** @var Client */
30
    private Client $publicClient;
31
32
    /** @var Client */
33
    private Client $privateClient;
34
35
    /** @var string */
36
    private string $key = '';
37
38
    /** @var string */
39
    private string $secret = '';
40
    /**
41
     * @var Batch
42
     */
43
    private Batch $batchClient;
44
45
    /**
46
     * @return PublicApi
47
     */
48 1
    public function public(): PublicApi
49
    {
50 1
        return new PublicApi($this->getPublicClient());
51
    }
52
53
    /**
54
     * @return Client
55
     */
56 1
    private function getPublicClient(): Client
57
    {
58 1
        return $this->publicClient ?? $this->createPublicClient();
59
    }
60
61
    /**
62
     * @return Client
63
     */
64 1
    private function createPublicClient(): Client
65
    {
66 1
        $this->publicClient = new Client([
67 1
            'headers' => self::CLIENT_HEADER,
68 1
            'base_uri' => self::BASE_URI
69
        ]);
70
71 1
        return $this->publicClient;
72
    }
73
74
    /**
75
     * @param string $key
76
     * @param string $secret
77
     * @throws InvalidCredentialException
78
     */
79 5
    public function setCredential(string $key, string $secret): void
80
    {
81
82 5
        if (!($this->isValidMd5($key) and $this->isValidMd5($secret)))
83 1
            throw new InvalidCredentialException('API Key and Secret have bad format');
84
85 4
        $this->key = $key;
86 4
        $this->secret = $secret;
87 4
    }
88
89
    /**
90
     * @param string $md5
91
     * @return bool
92
     */
93 5
    public function isValidMd5(string $md5 = ''): bool
94
    {
95 5
        return (preg_match('/^[a-f0-9]{32}$/', $md5) === 1);
96
    }
97
98
    /**
99
     * @return Batch
100
     * @throws InvalidCredentialException
101
     */
102 2
    public function batch(): Batch
103
    {
104
105 2
        return $this->batchClient ?? $this->createBatch();
106
    }
107
108
    /**
109
     * @throws InvalidCredentialException
110
     */
111 2
    private function createBatch(): Batch
112
    {
113 2
        $this->batchClient = new Batch($this->getPrivateClient());
114 1
        return $this->batchClient;
115
    }
116
117
    /**
118
     * @return Client
119
     * @throws InvalidCredentialException
120
     */
121 6
    private function getPrivateClient(): Client
122
    {
123 6
        return $this->privateClient ?? $this->createPrivateClient();
124
    }
125
126
    /**
127
     * @return Client
128
     * @throws InvalidCredentialException
129
     */
130 6
    private function createPrivateClient(): Client
131
    {
132 6
        if (!$this->haveValidCredentials())
133 3
            throw new InvalidCredentialException('Key and secret must be set before call Private API');
134
135 3
        $stack = HandlerStack::create();
136 3
        $stack->push(new Authentication($this->key, $this->secret));
137
138 3
        $this->privateClient = new Client([
139 3
            'headers' => self::CLIENT_HEADER,
140 3
            'handler' => $stack,
141 3
            'base_uri' => self::BASE_URI
142
        ]);
143
144 3
        return $this->privateClient;
145
    }
146
147
    /**
148
     * @return bool
149
     */
150 7
    public function haveValidCredentials(): bool
151
    {
152 7
        return (!empty($this->key) and !empty($this->secret) and $this->isValidMd5($this->key) and $this->isValidMd5($this->secret));
153
    }
154
155
    /**
156
     * @return Market
157
     * @throws InvalidCredentialException
158
     */
159 2
    public function market(): Market
160
    {
161 2
        return new Market($this->getPrivateClient());
162
    }
163
164
    /**
165
     * @return Account
166
     * @throws InvalidCredentialException
167
     */
168 2
    public function account(): Account
169
    {
170 2
        return new Account($this->getPrivateClient());
171
    }
172
}