Completed
Push — master ( 948a52...1d3ad0 )
by Mikhail
01:37
created

BittrexClient::batch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
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 $publicClient;
31
32
    /** @var Client */
33
    private $privateClient;
34
35
    /** @var string */
36
    private $key = '';
37
38
    /** @var string */
39
    private $secret = '';
40
41
    /**
42
     * @return PublicApi
43 1
     */
44
    public function public(): PublicApi
45 1
    {
46
        return new PublicApi($this->getPublicClient());
47
    }
48
49
    /**
50
     * @return Client
51 1
     */
52
    private function getPublicClient(): Client
53 1
    {
54
        return $this->publicClient ?: $this->createPublicClient();
55
    }
56
57
    /**
58
     * @return Client
59 1
     */
60
    private function createPublicClient(): Client
61 1
    {
62 1
        $this->publicClient = new Client([
63 1
            'headers' => self::CLIENT_HEADER,
64
            'base_uri' => self::BASE_URI
65
        ]);
66
67
        return $this->publicClient;
68
    }
69
70
    /**
71
     * @param string $key
72 4
     * @param string $secret
73
     * @throws InvalidCredentialException
74
     */
75 4
    public function setCredential(string $key, string $secret): void
76 1
    {
77
78 3
        if (!($this->isValidMd5($key) and $this->isValidMd5($secret)))
79 3
            throw new InvalidCredentialException('API Key and Secret have bad format');
80 3
81
        $this->key = $key;
82
        $this->secret = $secret;
83
    }
84
85
    /**
86 4
     * @param string $md5
87
     * @return bool
88 4
     */
89
    public function isValidMd5(string $md5 = ''): bool
90
    {
91
        return (preg_match('/^[a-f0-9]{32}$/', $md5) === 1);
92
    }
93
94
    /**
95 2
     * @return Batch
96
     * @throws InvalidCredentialException
97 2
     */
98
    public function batch(): Batch
99
    {
100
        return new Batch($this->getPrivateClient());
101
    }
102
103
    /**
104 4
     * @return Client
105
     * @throws InvalidCredentialException
106 4
     */
107
    private function getPrivateClient(): Client
108
    {
109
        return $this->privateClient ?: $this->createPrivateClient();
110
    }
111
112
    /**
113 4
     * @return Client
114
     * @throws InvalidCredentialException
115 4
     */
116 2
    private function createPrivateClient(): Client
117
    {
118 2
        if (!$this->haveValidCredentials())
119 2
            throw new InvalidCredentialException('Key and secret must be set before call Private API');
120
121 2
        $stack = HandlerStack::create();
122 2
        $stack->push(new Authentication($this->key, $this->secret));
123 2
124 2
        $this->privateClient = new Client([
125
            'headers' => self::CLIENT_HEADER,
126
            'handler' => $stack,
127
            'base_uri' => self::BASE_URI
128
        ]);
129
130
        return $this->privateClient;
131 5
    }
132
133 5
    /**
134
     * @return bool
135
     */
136
    public function haveValidCredentials(): bool
137
    {
138
        return (!empty($this->key) and !empty($this->secret) and $this->isValidMd5($this->key) and $this->isValidMd5($this->secret));
139
    }
140 2
141
    /**
142 2
     * @return Market
143
     * @throws InvalidCredentialException
144 1
     */
145
    public function market(): Market
146
    {
147
        return new Market($this->getPrivateClient());
148
    }
149
150
    /**
151
     * @return Account
152
     * @throws InvalidCredentialException
153
     */
154
    public function account(): Account
155
    {
156
        return new Account($this->getPrivateClient());
157
    }
158
}