Completed
Push — master ( 371a9b...ee3472 )
by Fabian
02:23
created

KrakenApi   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 265
ccs 66
cts 66
cp 1
rs 10
c 0
b 0
f 0
wmc 20

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