Completed
Push — master ( 7ff602...7a1d1a )
by Fabian
02:16
created

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