Completed
Push — master ( 3a2225...4cb758 )
by Mikhail
01:50
created

BittrexClient::countAPI()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
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\Market;
11
use R3bers\BittrexApi\Api\PublicApi;
12
use R3bers\BittrexApi\Exception\InvalidCredentialException;
13
use R3bers\BittrexApi\Middleware\Authentication;
14
15
class BittrexClient
16
{
17
    /**
18
     * Main URL to Bittrex Exchange
19
     */
20
    private const BASE_URI = 'https://api.bittrex.com';
21
22
    /** @var Client */
23
    private $publicClient;
24
25
    /** @var Client */
26
    private $privateClient;
27
28
    /** @var string */
29
    private $key = '';
30
31
    /** @var string */
32
    private $secret = '';
33
34
    /** @var string */
35
    private $currentMinuteCount = 0;
36
    /**
37
     * @var int
38
     */
39
    private $lastAPITime;
40
    /**
41
     * @var int
42
     */
43
    private $prevMinuteCount = 0;
44
45
    /**
46
     * BittrexClient constructor.
47
     */
48 9
    public function __construct()
49
    {
50 9
        $this->lastAPITime = time();
51 9
    }
52
53
    /**
54
     * @return int
55
     */
56 1
    public function getCurrentMinuteCount(): int
57
    {
58 1
        return $this->currentMinuteCount;
59
    }
60
61
    /**
62
     * @param string $key
63
     * @param string $secret
64
     */
65 3
    public function setCredential(string $key, string $secret): void
66
    {
67 3
        $this->key = $key;
68 3
        $this->secret = $secret;
69 3
    }
70
71
    /**
72
     * @return PublicApi
73
     */
74 3
    public function public(): PublicApi
75
    {
76 3
        $this->countAPI();
77 3
        return new PublicApi($this->getPublicClient());
78
79
    }
80
81
    /**
82
     * Counting in minute API
83
     */
84 7
    private function countAPI(): void
85
    {
86 7
        $currentTime = time();
87 7
        if (intdiv($this->lastAPITime, 60) < intdiv($currentTime, 60)) {
88 1
            $this->prevMinuteCount = $this->currentMinuteCount;
0 ignored issues
show
Documentation Bug introduced by
The property $prevMinuteCount was declared of type integer, but $this->currentMinuteCount is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
89 1
            $this->currentMinuteCount = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $currentMinuteCount was declared of type string, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
90
        } else {
91 7
            $this->currentMinuteCount++;
92 7
            $this->lastAPITime = $currentTime;
93
        }
94 7
    }
95
96
    /**
97
     * @return Client
98
     */
99 3
    private function getPublicClient(): Client
100
    {
101 3
        return $this->publicClient ?: $this->createPublicClient();
102
    }
103
104
    /**
105
     * @return Client
106
     */
107 3
    private function createPublicClient(): Client
108
    {
109 3
        return new Client([
110
            'headers' => [
111 3
                'User-Agent' => 'r3bers/bittrex-api/1.3',
112
                'Accept' => 'application/json',
113
                'Content-Type' => 'application/json'
114
            ],
115 3
            'base_uri' => self::BASE_URI
116
        ]);
117
    }
118
119
    /**
120
     * @return Market
121
     * @throws InvalidCredentialException
122
     */
123 2
    public function market(): Market
124
    {
125 2
        $this->countAPI();
126 2
        return new Market($this->getPrivateClient());
127
    }
128
129
    /**
130
     * @return Client
131
     * @throws InvalidCredentialException
132
     */
133 4
    private function getPrivateClient(): Client
134
    {
135 4
        return $this->privateClient ?: $this->createPrivateClient();
136
    }
137
138
    /**
139
     * @return Client
140
     * @throws InvalidCredentialException
141
     */
142 4
    private function createPrivateClient(): Client
143
    {
144 4
        if (empty($this->key) || empty($this->secret)) {
145 2
            throw new InvalidCredentialException('Key and secret must be set for authenticated API');
146
        }
147 2
        $stack = HandlerStack::create();
148 2
        $stack->push(new Authentication($this->getKey(), $this->getSecret()));
149
150 2
        return new Client([
151
            'headers' => [
152 2
                'User-Agent' => 'r3bers/bittrex-api/1.3',
153
                'Accept' => 'application/json',
154
                'Content-Type' => 'application/json'
155
            ],
156 2
            'handler' => $stack,
157 2
            'base_uri' => self::BASE_URI
158
        ]);
159
    }
160
161
    /**
162
     * @return string
163
     */
164 4
    public function getKey(): string
165
    {
166 4
        return $this->key;
167
    }
168
169
    /**
170
     * @return string
171
     */
172 4
    public function getSecret(): string
173
    {
174 4
        return $this->secret;
175
    }
176
177
    /**
178
     * @return Account
179
     * @throws InvalidCredentialException
180
     */
181 2
    public function account(): Account
182
    {
183 2
        $this->countAPI();
184 2
        return new Account($this->getPrivateClient());
185
    }
186
187
    /**
188
     * @return int
189
     */
190 1
    public function getPrevMinuteCount(): int
191
    {
192 1
        return $this->prevMinuteCount;
193
    }
194
}