Completed
Push — master ( 326999...39a0bd )
by Fabian
02:17
created

KrakenApi::getTradeBalance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author  Fabian Hanisch
4
 * @since   16.07.2017 02:56
5
 * @version 1.0
6
 */
7
8
namespace HanischIt\KrakenApi;
9
10
use HanischIt\KrakenApi\External\HttpClient;
11
use HanischIt\KrakenApi\Model\AccountBalance\AccountBalanceRequest;
12
use HanischIt\KrakenApi\Model\AccountBalance\AccountBalanceResponse;
13
use HanischIt\KrakenApi\Model\AddOrder\AddOrderRequest;
14
use HanischIt\KrakenApi\Model\AddOrder\AddOrderResponse;
15
use HanischIt\KrakenApi\Model\Assets\AssetsRequest;
16
use HanischIt\KrakenApi\Model\Assets\AssetsResponse;
17
use HanischIt\KrakenApi\Model\CancelOpenOrder\CancelOpenOrderRequest;
18
use HanischIt\KrakenApi\Model\CancelOpenOrder\CancelOpenOrderResponse;
19
use HanischIt\KrakenApi\Model\ClosedOrders\ClosedOrdersRequest;
20
use HanischIt\KrakenApi\Model\ClosedOrders\ClosedOrdersResponse;
21
use HanischIt\KrakenApi\Model\GetTicker\TickerRequest;
22
use HanischIt\KrakenApi\Model\GetTicker\TickerResponse;
23
use HanischIt\KrakenApi\Model\Header;
24
use HanischIt\KrakenApi\Model\LedgersInfo\LedgersInfoRequest;
25
use HanischIt\KrakenApi\Model\LedgersInfo\LedgersInfoResponse;
26
use HanischIt\KrakenApi\Model\OHLCData\OHLCDataRequest;
27
use HanischIt\KrakenApi\Model\OHLCData\OHLCDataResponse;
28
use HanischIt\KrakenApi\Model\OpenOrders\OpenOrdersRequest;
29
use HanischIt\KrakenApi\Model\OpenOrders\OpenOrdersResponse;
30
use HanischIt\KrakenApi\Model\OpenPositions\OpenPositionsRequest;
31
use HanischIt\KrakenApi\Model\OpenPositions\OpenPositionsResponse;
32
use HanischIt\KrakenApi\Model\OrderBook\OrderBookRequest;
33
use HanischIt\KrakenApi\Model\OrderBook\OrderBookResponse;
34
use HanischIt\KrakenApi\Model\OrdersInfo\OrdersInfoRequest;
35
use HanischIt\KrakenApi\Model\OrdersInfo\OrdersInfoResponse;
36
use HanischIt\KrakenApi\Model\QueryLedgers\QueryLedgersRequest;
37
use HanischIt\KrakenApi\Model\QueryLedgers\QueryLedgersResponse;
38
use HanischIt\KrakenApi\Model\RecentTrades\RecentTradesRequest;
39
use HanischIt\KrakenApi\Model\RecentTrades\RecentTradesResponse;
40
use HanischIt\KrakenApi\Model\RequestInterface;
41
use HanischIt\KrakenApi\Model\RequestOptions;
42
use HanischIt\KrakenApi\Model\ResponseInterface;
43
use HanischIt\KrakenApi\Model\ServerTime\ServerTimeRequest;
44
use HanischIt\KrakenApi\Model\ServerTime\ServerTimeResponse;
45
use HanischIt\KrakenApi\Model\SpreadData\SpreadDataRequest;
46
use HanischIt\KrakenApi\Model\SpreadData\SpreadDataResponse;
47
use HanischIt\KrakenApi\Model\TradableAssetPairs\TradableAssetPairsRequest;
48
use HanischIt\KrakenApi\Model\TradableAssetPairs\TradableAssetPairsResponse;
49
use HanischIt\KrakenApi\Model\TradeBalance\TradeBalanceRequest;
50
use HanischIt\KrakenApi\Model\TradeBalance\TradeBalanceResponse;
51
use HanischIt\KrakenApi\Model\Trades\TradesRequest;
52
use HanischIt\KrakenApi\Model\Trades\TradesResponse;
53
use HanischIt\KrakenApi\Model\TradesHistory\TradesHistoryRequest;
54
use HanischIt\KrakenApi\Model\TradesHistory\TradesHistoryResponse;
55
use HanischIt\KrakenApi\Model\TradeVolume\TradeVolumeRequest;
56
use HanischIt\KrakenApi\Model\TradeVolume\TradeVolumeResponse;
57
use HanischIt\KrakenApi\Service\RequestService\GetRequest;
58
use HanischIt\KrakenApi\Service\RequestService\Nonce;
59
use HanischIt\KrakenApi\Service\RequestService\PostRequest;
60
use HanischIt\KrakenApi\Service\RequestService\Request;
61
use HanischIt\KrakenApi\Service\RequestService\RequestHeader;
62
63
/**
64
 * Class KrakenApi
65
 *
66
 * @package HanischIt\KrakenApi
67
 */
68
class KrakenApi
69
{
70
    /**
71
     * @var RequestOptions
72
     */
73
    private $requestOptions;
74
    /**
75
     * @var Header
76
     */
77
    private $header;
78
    /**
79
     * @var Request
80
     */
81
    private $request;
82
83
    /**
84
     * KrakenApi constructor.
85
     *
86
     * @param string $apiKey
87
     * @param string $apiSign
88
     * @param string $version
89
     * @param string $endpoint
90
     */
91 21
    public function __construct($apiKey, $apiSign, $version = '0', $endpoint = 'https://api.kraken.com/')
92
    {
93 21
        $httpClient = new HttpClient(['verify' => false]);
94 21
        $requestHeader = new RequestHeader();
95 21
        $nonce = new Nonce();
96
97 21
        $this->requestOptions = new RequestOptions($endpoint, $version);
98 21
        $postRequest = new PostRequest($httpClient, $requestHeader, $nonce);
99 21
        $getRequest = new GetRequest($httpClient, $requestHeader);
100 21
        $this->request = new Request($postRequest, $getRequest);
101 21
        $this->header = new Header($apiKey, $apiSign);
102 21
    }
103
104
    /**
105
     * @param Request $request
106
     */
107 21
    public function setRequest(Request $request)
108
    {
109 21
        $this->request = $request;
110 21
    }
111
112
    /**
113
     * @return ServerTimeResponse|ResponseInterface
114
     */
115 1
    public function getServerTime()
116
    {
117 1
        $serverTimeRequest = new ServerTimeRequest();
118
119 1
        return $this->doRequest($serverTimeRequest);
120
    }
121
122
    /**
123
     * @return AccountBalanceResponse|ResponseInterface
124
     */
125 1
    public function getAccountBalance()
126
    {
127 1
        $accountBalanceRequest = new AccountBalanceRequest();
128
129 1
        return $this->doRequest($accountBalanceRequest);
130
    }
131
132
    /**
133
     * @param string $pair
134
     * @param string $type
135
     * @param string $orderType
136
     * @param null|float $price
137
     * @param null|float $volume
138
     *
139
     * @return ResponseInterface|AddOrderResponse
140
     */
141 1
    public function addOrder($pair, $type, $orderType, $price = null, $volume = null)
142
    {
143 1
        $addOrderRequest = new AddOrderRequest($pair, $type, $orderType, $price, $volume);
144
145 1
        return $this->doRequest($addOrderRequest);
146
    }
147
148
    /**
149
     * @return ResponseInterface|AssetsResponse
150
     */
151 1
    public function getAssets()
152
    {
153 1
        $assetsRequest = new AssetsRequest();
154
155 1
        return $this->doRequest($assetsRequest);
156
    }
157
158
    /**
159
     * @param array $assetNames
160
     *
161
     * @return ResponseInterface|TickerResponse
162
     */
163 1
    public function getTicker(array $assetNames)
164
    {
165 1
        $tickerRequest = new TickerRequest($assetNames);
166
167 1
        return $this->doRequest($tickerRequest);
168
    }
169
170
    /**
171
     * @param string $assetPair
172
     * @param int|null $count
173
     *
174
     * @return ResponseInterface|OrderBookResponse
175
     */
176 1
    public function getOrderBook($assetPair, $count = null)
177
    {
178 1
        $orderBookRequest = new OrderBookRequest($assetPair, $count);
179
180 1
        return $this->doRequest($orderBookRequest);
181
    }
182
183
    /**
184
     * @param bool $trades
185
     * @param null $userref
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userref is correct as it would always require null to be passed?
Loading history...
186
     *
187
     * @return ResponseInterface|OpenOrdersResponse
188
     */
189 1
    public function getOpenOrders($trades = false, $userref = null)
190
    {
191 1
        $orderBookRequest = new OpenOrdersRequest($trades, $userref);
192
193 1
        return $this->doRequest($orderBookRequest);
194
    }
195
196
    /**
197
     * @param bool $trades
198
     * @param null $userref
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userref is correct as it would always require null to be passed?
Loading history...
199
     * @param null|string $start
200
     * @param null|string $end
201
     * @param null|int $ofs
202
     * @param null|string $closetime
203
     *
204
     * @return ClosedOrdersResponse|ResponseInterface
205
     */
206 1
    public function getClosedOrders(
207
        $trades = false,
208
        $userref = null,
209
        $start = null,
210
        $end = null,
211
        $ofs = null,
212
        $closetime = null
213
    ) {
214 1
        $orderBookRequest = new ClosedOrdersRequest($trades, $userref, $start, $end, $ofs, $closetime);
215
216 1
        return $this->doRequest($orderBookRequest);
217
    }
218
219
    /**
220
     * @param string $assetPair
221
     * @param null|string $since
222
     *
223
     * @return ResponseInterface|RecentTradesResponse
224
     */
225 1
    public function getRecentTrades($assetPair, $since = null)
226
    {
227 1
        $recentTradeRequest = new RecentTradesRequest($assetPair, $since);
228
229 1
        return $this->doRequest($recentTradeRequest);
230
    }
231
232
    /**
233
     * @param string $assetPair
234
     * @param string $since
235
     * @return ResponseInterface|SpreadDataResponse
236
     */
237 1
    public function getSpreadData($assetPair, $since = null)
238
    {
239 1
        $spreadDataRequest = new SpreadDataRequest($assetPair, $since);
240
241 1
        return $this->doRequest($spreadDataRequest);
242
    }
243
244
    /**
245
     * @param string $info
246
     * @param array|null $assetPairs
247
     * @return ResponseInterface|TradableAssetPairsResponse
248
     */
249 1
    public function getTradableAssetPairs($info, array $assetPairs = null)
250
    {
251 1
        if (null !== $assetPairs) {
252 1
            $assetPairs = implode(',', $assetPairs);
253 1
        }
254 1
        $tradableAssetPairs = new TradableAssetPairsRequest($info, $assetPairs);
255
256 1
        return $this->doRequest($tradableAssetPairs);
257
    }
258
259
    /**
260
     * @param string $assetPair
261
     * @param null|int $interval
262
     * @param null|int $since
263
     * @return ResponseInterface|OHLCDataResponse
264
     */
265 1
    public function getOHLCData($assetPair, $interval = null, $since = null)
266
    {
267 1
        $ohlcDataRequest = new OHLCDataRequest($assetPair, $interval, $since);
268
269 1
        return $this->doRequest($ohlcDataRequest);
270
    }
271
272
    /**
273
     * @param array|null $txids
274
     * @param bool $trades
275
     * @param null|string $userref
276
     * @return ResponseInterface|OrdersInfoResponse
277
     */
278 1
    public function getOrdersInfo(array $txids, $trades = false, $userref = null)
279
    {
280 1
        $ordersInfoRequest = new OrdersInfoRequest($txids, $trades, $userref);
281
282 1
        return $this->doRequest($ordersInfoRequest);
283
    }
284
285
    /**
286
     * @param string $type
287
     * @param bool $trades
288
     * @param null $start
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $start is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $ofs is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $end is correct as it would always require null to be passed?
Loading history...
289
     * @param null $end
290
     * @param null $ofs
291
     * @return ResponseInterface|TradesHistoryResponse
292
     */
293 1
    public function getTradesHistory($type = 'all', $trades = false, $start = null, $end = null, $ofs = null)
294
    {
295 1
        $tradesHistoryRequest = new TradesHistoryRequest($type, $trades, $start, $end, $ofs);
296
297 1
        return $this->doRequest($tradesHistoryRequest);
298
    }
299
300
    /**
301
     * @param string $txid
302
     * @param bool $trades
303
     * @return ResponseInterface|TradesResponse
304
     */
305 1
    public function getTrades($txid, $trades = false)
306
    {
307 1
        $tradesHistoryRequest = new TradesRequest($txid, $trades);
308
309 1
        return $this->doRequest($tradesHistoryRequest);
310
    }
311
312
    /**
313
     * @param string $txid
314
     * @return ResponseInterface|CancelOpenOrderResponse
315
     */
316 1
    public function cancelOrder($txid)
317
    {
318 1
        $cancelOrderRequest = new CancelOpenOrderRequest($txid);
319
320 1
        return $this->doRequest($cancelOrderRequest);
321
    }
322
323
    /**
324
     * @param string|null $aclass
325
     * @param string|null $asset
326
     * @return ResponseInterface|TradeBalanceResponse
327
     */
328 1
    public function getTradeBalance($aclass = null, $asset = null)
329
    {
330 1
        $tradeBalanceRequest = new TradeBalanceRequest($aclass, $asset);
331
332 1
        return $this->doRequest($tradeBalanceRequest);
333
    }
334
335
    /**
336
     * @param string $aclass
337
     * @param string $asset
338
     * @param string $type
339
     * @param null|string $start
340
     * @param null|string $end
341
     * @param null|string $ofs
342
     * @return ResponseInterface|LedgersInfoResponse
343
     */
344 1
    public function getLedgersInfo(
345
        $aclass = 'currency',
346
        $asset = 'all',
347
        $type = 'all',
348
        $start = null,
349
        $end = null,
350
        $ofs = null
351
    ) {
352 1
        $ledgersInfoRequest = new LedgersInfoRequest(
353 1
            $aclass,
354 1
            $asset,
355 1
            $type,
356 1
            $start,
357 1
            $end,
358 1
            $ofs);
359
360 1
        return $this->doRequest($ledgersInfoRequest);
361
    }
362
363
    /**
364
     * @param string $id
365
     * @return ResponseInterface|QueryLedgersResponse
366
     */
367 1
    public function getLedgers($id)
368
    {
369 1
        $ledgersInfoRequest = new QueryLedgersRequest($id);
370
371 1
        return $this->doRequest($ledgersInfoRequest);
372
373
    }
374
375
    /**
376
     * @param null|string $pair
377
     * @param null|bool $feeInfo
378
     * @return TradeVolumeResponse|ResponseInterface
379
     */
380 1
    public function getTradeVolume($pair = null, $feeInfo = null)
381
    {
382 1
        $tradeVolumeRequest = new TradeVolumeRequest($pair, $feeInfo);
383
384 1
        return $this->doRequest($tradeVolumeRequest);
385
    }
386
387
    /**
388
     * @param string $txid
389
     * @param bool $docalcs
390
     * @return ResponseInterface|OpenPositionsResponse
391
     */
392 1
    public function getOpenPositions($txid, $docalcs = false)
393
    {
394 1
        $openPositionsRequest = new OpenPositionsRequest($txid, $docalcs);
395
396 1
        return $this->doRequest($openPositionsRequest);
397
    }
398
399
    /**
400
     * @param RequestInterface $requestInterface
401
     *
402
     * @return ResponseInterface
403
     */
404 21
    private function doRequest(RequestInterface $requestInterface)
405
    {
406 21
        return $this->request->execute($requestInterface, $this->requestOptions, $this->header);
407
    }
408
}
409