Completed
Push — master ( ee3472...674111 )
by Fabian
03:13
created

KrakenApi   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 276
ccs 69
cts 69
cp 1
rs 10
c 0
b 0
f 0
wmc 21

20 Methods

Rating   Name   Duplication   Size   Complexity  
A addOrder() 0 5 1
A getAccountBalance() 0 5 1
A getTradesHistory() 0 5 1
A getTradableAssetPairs() 0 8 2
A getTrades() 0 5 1
A getTradeBalance() 0 5 1
A getClosedOrders() 0 12 1
A setRequest() 0 3 1
A getTicker() 0 5 1
A cancelOrder() 0 5 1
A getServerTime() 0 5 1
A doRequest() 0 3 1
A getOpenOrders() 0 5 1
A getAssets() 0 5 1
A getRecentTrades() 0 5 1
A getOrderBook() 0 5 1
A getSpreadData() 0 5 1
A __construct() 0 11 1
A getOrdersInfo() 0 5 1
A getOHLCData() 0 5 1
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\OHLCData\OHLCDataRequest;
25
use HanischIt\KrakenApi\Model\OHLCData\OHLCDataResponse;
26
use HanischIt\KrakenApi\Model\OpenOrders\OpenOrdersRequest;
27
use HanischIt\KrakenApi\Model\OpenOrders\OpenOrdersResponse;
28
use HanischIt\KrakenApi\Model\OrderBook\OrderBookRequest;
29
use HanischIt\KrakenApi\Model\OrderBook\OrderBookResponse;
30
use HanischIt\KrakenApi\Model\OrdersInfo\OrdersInfoRequest;
31
use HanischIt\KrakenApi\Model\OrdersInfo\OrdersInfoResponse;
32
use HanischIt\KrakenApi\Model\RecentTrades\RecentTradesRequest;
33
use HanischIt\KrakenApi\Model\RecentTrades\RecentTradesResponse;
34
use HanischIt\KrakenApi\Model\RequestInterface;
35
use HanischIt\KrakenApi\Model\RequestOptions;
36
use HanischIt\KrakenApi\Model\ResponseInterface;
37
use HanischIt\KrakenApi\Model\ServerTime\ServerTimeRequest;
38
use HanischIt\KrakenApi\Model\ServerTime\ServerTimeResponse;
39
use HanischIt\KrakenApi\Model\SpreadData\SpreadDataRequest;
40
use HanischIt\KrakenApi\Model\SpreadData\SpreadDataResponse;
41
use HanischIt\KrakenApi\Model\TradableAssetPairs\TradableAssetPairsRequest;
42
use HanischIt\KrakenApi\Model\TradableAssetPairs\TradableAssetPairsResponse;
43
use HanischIt\KrakenApi\Model\TradeBalance\TradeBalanceRequest;
44
use HanischIt\KrakenApi\Model\TradeBalance\TradeBalanceResponse;
45
use HanischIt\KrakenApi\Model\Trades\TradesRequest;
46
use HanischIt\KrakenApi\Model\Trades\TradesResponse;
47
use HanischIt\KrakenApi\Model\TradesHistory\TradesHistoryRequest;
48
use HanischIt\KrakenApi\Model\TradesHistory\TradesHistoryResponse;
49
use HanischIt\KrakenApi\Service\RequestService\GetRequest;
50
use HanischIt\KrakenApi\Service\RequestService\Nonce;
51
use HanischIt\KrakenApi\Service\RequestService\PostRequest;
52
use HanischIt\KrakenApi\Service\RequestService\Request;
53
use HanischIt\KrakenApi\Service\RequestService\RequestHeader;
54
55
/**
56
 * Class KrakenApi
57
 *
58
 * @package HanischIt\KrakenApi
59
 */
60
class KrakenApi
61
{
62
    /**
63
     * @var RequestOptions
64
     */
65
    private $requestOptions;
66
    /**
67
     * @var Header
68
     */
69
    private $header;
70
    /**
71
     * @var Request
72
     */
73
    private $request;
74
75
    /**
76
     * KrakenApi constructor.
77
     *
78
     * @param string $apiKey
79
     * @param string $apiSign
80
     * @param string $version
81
     * @param string $endpoint
82
     */
83 17
    public function __construct($apiKey, $apiSign, $version = '0', $endpoint = 'https://api.kraken.com/')
84
    {
85 17
        $httpClient = new HttpClient(['verify' => false]);
86 17
        $requestHeader = new RequestHeader();
87 17
        $nonce = new Nonce();
88
89 17
        $this->requestOptions = new RequestOptions($endpoint, $version);
90 17
        $postRequest = new PostRequest($httpClient, $requestHeader, $nonce);
91 17
        $getRequest = new GetRequest($httpClient, $requestHeader);
92 17
        $this->request = new Request($postRequest, $getRequest);
93 17
        $this->header = new Header($apiKey, $apiSign);
94 17
    }
95
96
    /**
97
     * @param Request $request
98
     */
99 17
    public function setRequest(Request $request)
100
    {
101 17
        $this->request = $request;
102 17
    }
103
104
    /**
105
     * @return ServerTimeResponse|ResponseInterface
106
     */
107 1
    public function getServerTime()
108
    {
109 1
        $serverTimeRequest = new ServerTimeRequest();
110
111 1
        return $this->doRequest($serverTimeRequest);
112
    }
113
114
    /**
115
     * @return AccountBalanceResponse|ResponseInterface
116
     */
117 1
    public function getAccountBalance()
118
    {
119 1
        $accountBalanceRequest = new AccountBalanceRequest();
120
121 1
        return $this->doRequest($accountBalanceRequest);
122
    }
123
124
    /**
125
     * @param string $pair
126
     * @param string $type
127
     * @param string $orderType
128
     * @param null|float $price
129
     * @param null|float $volume
130
     *
131
     * @return ResponseInterface|AddOrderResponse
132
     */
133 1
    public function addOrder($pair, $type, $orderType, $price = null, $volume = null)
134
    {
135 1
        $addOrderRequest = new AddOrderRequest($pair, $type, $orderType, $price, $volume);
136
137 1
        return $this->doRequest($addOrderRequest);
138
    }
139
140
    /**
141
     * @return ResponseInterface|AssetsResponse
142
     */
143 1
    public function getAssets()
144
    {
145 1
        $assetsRequest = new AssetsRequest();
146
147 1
        return $this->doRequest($assetsRequest);
148
    }
149
150
    /**
151
     * @param array $assetNames
152
     *
153
     * @return ResponseInterface|TickerResponse
154
     */
155 1
    public function getTicker(array $assetNames)
156
    {
157 1
        $tickerRequest = new TickerRequest($assetNames);
158
159 1
        return $this->doRequest($tickerRequest);
160
    }
161
162
    /**
163
     * @param string $assetPair
164
     * @param int|null $count
165
     *
166
     * @return ResponseInterface|OrderBookResponse
167
     */
168 1
    public function getOrderBook($assetPair, $count = null)
169
    {
170 1
        $orderBookRequest = new OrderBookRequest($assetPair, $count);
171
172 1
        return $this->doRequest($orderBookRequest);
173
    }
174
175
    /**
176
     * @param bool $trades
177
     * @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...
178
     *
179
     * @return ResponseInterface|OpenOrdersResponse
180
     */
181 1
    public function getOpenOrders($trades = false, $userref = null)
182
    {
183 1
        $orderBookRequest = new OpenOrdersRequest($trades, $userref);
184
185 1
        return $this->doRequest($orderBookRequest);
186
    }
187
188
    /**
189
     * @param bool $trades
190
     * @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...
191
     * @param null|string $start
192
     * @param null|string $end
193
     * @param null|int $ofs
194
     * @param null|string $closetime
195
     *
196
     * @return ClosedOrdersResponse|ResponseInterface
197
     */
198 1
    public function getClosedOrders(
199
        $trades = false,
200
        $userref = null,
201
        $start = null,
202
        $end = null,
203
        $ofs = null,
204
        $closetime = null
205
    )
206
    {
207 1
        $orderBookRequest = new ClosedOrdersRequest($trades, $userref, $start, $end, $ofs, $closetime);
208
209 1
        return $this->doRequest($orderBookRequest);
210
    }
211
212
    /**
213
     * @param string $assetPair
214
     * @param null|string $since
215
     *
216
     * @return ResponseInterface|RecentTradesResponse
217
     */
218 1
    public function getRecentTrades($assetPair, $since = null)
219
    {
220 1
        $recentTradeRequest = new RecentTradesRequest($assetPair, $since);
221
222 1
        return $this->doRequest($recentTradeRequest);
223
    }
224
225
    /**
226
     * @param string $assetPair
227
     * @param string $since
228
     * @return ResponseInterface|SpreadDataResponse
229
     */
230 1
    public function getSpreadData($assetPair, $since = null)
231
    {
232 1
        $spreadDataRequest = new SpreadDataRequest($assetPair, $since);
233
234 1
        return $this->doRequest($spreadDataRequest);
235
    }
236
237
    /**
238
     * @param string $info
239
     * @param array|null $assetPairs
240
     * @return ResponseInterface|TradableAssetPairsResponse
241
     */
242 1
    public function getTradableAssetPairs($info, array $assetPairs = null)
243
    {
244 1
        if (null !== $assetPairs) {
245 1
            $assetPairs = implode(',', $assetPairs);
246 1
        }
247 1
        $tradableAssetPairs = new TradableAssetPairsRequest($info, $assetPairs);
248
249 1
        return $this->doRequest($tradableAssetPairs);
250
    }
251
252
    /**
253
     * @param string $assetPair
254
     * @param null|int $interval
255
     * @param null|int $since
256
     * @return ResponseInterface|OHLCDataResponse
257
     */
258 1
    public function getOHLCData($assetPair, $interval = null, $since = null)
259
    {
260 1
        $ohlcDataRequest = new OHLCDataRequest($assetPair, $interval, $since);
261
262 1
        return $this->doRequest($ohlcDataRequest);
263
    }
264
265
    /**
266
     * @param array|null $txids
267
     * @param bool $trades
268
     * @param null|string $userref
269
     * @return ResponseInterface|OrdersInfoResponse
270
     */
271 1
    public function getOrdersInfo(array $txids, $trades = false, $userref = null)
272
    {
273 1
        $ordersInfoRequest = new OrdersInfoRequest($txids, $trades, $userref);
274
275 1
        return $this->doRequest($ordersInfoRequest);
276
    }
277
278
    /**
279
     * @param string $type
280
     * @param bool $trades
281
     * @param null $start
0 ignored issues
show
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...
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 $start is correct as it would always require null to be passed?
Loading history...
282
     * @param null $end
283
     * @param null $ofs
284
     * @return ResponseInterface|TradesHistoryResponse
285
     */
286 1
    public function getTradesHistory($type = 'all', $trades = false, $start = null, $end = null, $ofs = null)
287
    {
288 1
        $tradesHistoryRequest = new TradesHistoryRequest($type, $trades, $start, $end, $ofs);
289
290 1
        return $this->doRequest($tradesHistoryRequest);
291
    }
292
293
    /**
294
     * @param string $txid
295
     * @param bool $trades
296
     * @return ResponseInterface|TradesResponse
297
     */
298 1
    public function getTrades($txid, $trades = false)
299
    {
300 1
        $tradesHistoryRequest = new TradesRequest($txid, $trades);
301
302 1
        return $this->doRequest($tradesHistoryRequest);
303
    }
304
305
    /**
306
     * @param string $txid
307
     * @return ResponseInterface|CancelOpenOrderResponse
308
     */
309 1
    public function cancelOrder($txid)
310
    {
311 1
        $cancelOrderRequest = new CancelOpenOrderRequest($txid);
312
313 1
        return $this->doRequest($cancelOrderRequest);
314
    }
315
316
    /**
317
     * @param string|null $aclass
318
     * @param string|null $asset
319
     * @return ResponseInterface|TradeBalanceResponse
320
     */
321 1
    public function getTradeBalance($aclass = null, $asset = null)
322
    {
323 1
        $tradeBalanceRequest = new TradeBalanceRequest($aclass, $asset);
324
325 1
        return $this->doRequest($tradeBalanceRequest);
326
    }
327
328
    /**
329
     * @param RequestInterface $requestInterface
330
     *
331
     * @return ResponseInterface
332
     */
333 17
    private function doRequest(RequestInterface $requestInterface)
334
    {
335 17
        return $this->request->execute($requestInterface, $this->requestOptions, $this->header);
336
    }
337
}
338