GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 605adb...5dad57 )
by Elliot
04:20
created

PrivateClient::mapToKeyValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3.0987
1
<?php
2
3
namespace IndependentReserve;
4
5
use DateTime;
6
use IndependentReserve\Object\Account;
7
use IndependentReserve\Object\BitcoinDepositAddress;
8
use IndependentReserve\Object\ClosedOrder;
9
use IndependentReserve\Object\FiatWithdrawal;
10
use IndependentReserve\Object\OpenOrder;
11
use IndependentReserve\Object\Order;
12
use IndependentReserve\Object\PagedIterator;
13
use IndependentReserve\Object\Transaction;
14
use stdClass;
15
16
class PrivateClient extends PublicClient
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $apiKey;
22
23
    /**
24
     * @var string
25
     */
26
    protected $apiSecret;
27
28
    /**
29
     * @param string $apiKey
30
     * @param string $apiSecret
31
     */
32 5
    public function __construct($apiKey, $apiSecret)
33
    {
34 5
        parent::__construct();
35 5
        $this->apiKey = $apiKey;
36 5
        $this->apiSecret = $apiSecret;
37 5
    }
38
39
    /**
40
     * Get the components required to make private API calls.
41
     * @return array
42
     */
43 4
    public function getSignature($url, $params)
44
    {
45 4
        $nonce = str_pad(str_replace('.', '', microtime(true)), 19, 0);
46
47 4
        $params =  array_merge(['apiKey' => $this->apiKey, 'nonce' => $nonce], $params);
48 4
        $strToSign = $this->mapToKeyValue($url,$params);
49 4
        $signature = strtoupper(hash_hmac('sha256', utf8_encode($strToSign), utf8_encode($this->apiSecret)));
50
51
        return [
52 4
            'apiKey' => $this->apiKey,
53 4
            'nonce' => $nonce,
54 4
            'signature' => $signature,
55 4
        ];
56
    }
57
58 4
    private function mapToKeyValue($url, $data) {
59 4
        $res = $url;
60 4
        foreach ($data as $key => $value) {
61 4
            if (is_array($value)) {
62
                $res .= ",$key=".array_values($value)[0]; // works only for case when array has one value. And no docs how to use for array with more then one value
63
            } else {
64 4
                $res .= ",$key=$value";
65
            }
66 4
        }
67 4
        return $res;
68
    }
69
    /**
70
     * Fetch a private API.
71
     * @param string $endpoint
72
     * @param array $params
73
     * @return mixed
74
     */
75 4
    public function getPrivateEndpoint($endpoint, array $params = array())
76
    {
77 4
        return $this->getEndpoint($endpoint, $this->getSignature($this->getEndpointUrl($endpoint,'Private'), $params) + $params, 'Private', 'POST');
78
    }
79
80
    /**
81
     * Retrieves your currently Open and Partially Filled orders.
82
     * @param string $primaryCurrencyCode The primary currency of orders.
83
     * @param string $secondaryCurrencyCode The secondary currency of orders.
84
     * @return OpenOrder[]
85
     */
86 1
    public function getOpenOrders($primaryCurrencyCode, $secondaryCurrencyCode)
87
    {
88 1
        return new PagedIterator($this, 'GetOpenOrders', [
89 1
            'primaryCurrencyCode' => $primaryCurrencyCode,
90 1
            'secondaryCurrencyCode' => $secondaryCurrencyCode,
91 1
            'pageSize' => 25,
92
        ], function (stdClass $object) {
93
            return OpenOrder::createFromObject($object);
94 1
        });
95
    }
96
97
    /**
98
     * Places new limit bid / offer order. A Limit Bid is a buy order and a Limit Offer is a sell
99
     * order.
100
     * @param string $primaryCurrencyCode The digital currency code of limit order. Must be a valid
101
     *        primary currency, which can be checked via the getValidPrimaryCurrencyCodes() method.
102
     * @param string $secondaryCurrencyCode The fiat currency of limit order. Must be a valid
103
     *        secondary currency, which can be checked via the getValidSecondaryCurrencyCodes()
104
     *        method.
105
     * @param string $orderType The type of limit order. Must be a valid limit order type, which can
106
     *        be checked via the getValidLimitOrderTypes() method.
107
     * @param double $price The price in secondary currency to buy/sell.
108
     * @param double $volume The volume to buy/sell in primary currency.
109
     * @return Order
110
     */
111
    public function placeLimitOrder($primaryCurrencyCode, $secondaryCurrencyCode, $orderType,
112
        $price, $volume)
113
    {
114
        return Order::createFromObject(json_decode($this->getPrivateEndpoint('PlaceLimitOrder', [
115
            'primaryCurrencyCode' => $primaryCurrencyCode,
116
            'secondaryCurrencyCode' => $secondaryCurrencyCode,
117
            'price' => $price,
118
            'volume' => $volume,
119
            'orderType' => $orderType,
120
        ])));
121
    }
122
123
    /**
124
     * Place new market bid / offer order. A Market Bid is a buy order and a Market Offer is a sell
125
     * order.
126
     * @param string $primaryCurrencyCode The digital currency code of market order. Must be a valid
127
     *        primary currency, which can be checked via the getValidPrimaryCurrencyCodes() method.
128
     * @param string $secondaryCurrencyCode The fiat currency of market order. Must be a valid
129
     *        secondary currency, which can be checked via the getValidSecondaryCurrencyCodes()
130
     *        method.
131
     * @param string $orderType The type of market order. Must be a valid market order type, which
132
     *        can be checked via the getValidMarketOrderTypes() method.
133
     * @param double $volume The volume to buy/sell in primary currency.
134
     * @return Order
135
     */
136
    public function placeMarketOrder($primaryCurrencyCode, $secondaryCurrencyCode, $orderType,
137
        $volume)
138
    {
139
        return Order::createFromObject(json_decode($this->getPrivateEndpoint('PlaceMarketOrder', [
140
            'primaryCurrencyCode' => $primaryCurrencyCode,
141
            'secondaryCurrencyCode' => $secondaryCurrencyCode,
142
            'volume' => $volume,
143
            'orderType' => $orderType,
144
        ])));
145
    }
146
147
    /**
148
     * Cancels a previously placed order.
149
     * @note The order must be in either 'Open' or 'PartiallyFilled' status to be valid for
150
     *       cancellation. You can retrieve list of Open and Partially Filled orders via the
151
     *       getOpenOrders() method. You can also check an individual order's status by calling the
152
     *       getOrderDetails() method.
153
     * @param string $guid The guid of currently open or partially filled order.
154
     * @return Order
155
     */
156
    public function cancelOrder($guid)
157
    {
158
        return Order::createFromObject(json_decode($this->getPrivateEndpoint('CancelOrder', [
159
            'orderGuid' => $guid,
160
        ])));
161
    }
162
163
    /**
164
     * Retrieves your Closed and Cancelled orders.
165
     * @param string $primaryCurrencyCode The primary currency of orders.
166
     * @param string $secondaryCurrencyCode The secondary currency of orders.
167
     * @return ClosedOrder[]
168
     */
169
    public function getClosedOrders($primaryCurrencyCode, $secondaryCurrencyCode)
170
    {
171
        return new PagedIterator($this, 'GetClosedOrders', [
172
            'primaryCurrencyCode' => $primaryCurrencyCode,
173
            'secondaryCurrencyCode' => $secondaryCurrencyCode,
174
            'pageSize' => 25,
175
        ], function (stdClass $object) {
176
            return ClosedOrder::createFromObject($object);
177
        });
178
    }
179
180
    /**
181
     * Retrieves your Closed orders which have had some or all of their outstanding volume filled.
182
     * @param string $primaryCurrencyCode The primary currency of orders.
183
     * @param string $secondaryCurrencyCode The secondary currency of orders.
184
     * @return ClosedOrder[]
185
     */
186
    public function getClosedFilledOrders($primaryCurrencyCode, $secondaryCurrencyCode)
187
    {
188
        return new PagedIterator($this, 'GetClosedFilledOrders', [
189
            'primaryCurrencyCode' => $primaryCurrencyCode,
190
            'secondaryCurrencyCode' => $secondaryCurrencyCode,
191
            'pageSize' => 25,
192
        ], function (stdClass $object) {
193
            return ClosedOrder::createFromObject($object);
194
        });
195
    }
196
197
    /**
198
     * Retrieves a single order.
199
     * @param string $guid
200
     * @return Order
201
     */
202 1
    public function getOrder($guid)
203
    {
204 1
        return Order::createFromObject(json_decode($this->getPrivateEndpoint('GetOrderDetails', [
205 1
            'orderGuid' => $guid,
206 1
        ])));
207
    }
208
209
    /**
210
     * Retrieves information about your Independent Reserve accounts in digital and fiat currencies.
211
     * @return Account[]
212
     */
213 1
    public function getAccounts()
214
    {
215
        return array_map(function (stdClass $object) {
216 1
            return Account::createFromObject($object);
217 1
        }, json_decode($this->getPrivateEndpoint('GetAccounts')));
218
    }
219
220
    /**
221
     * @param $accountGuid Account GUID.
222
     * @param DateTime $from Optional start time.
223
     * @param DateTime $to Optional end time.
224
     * @return Transaction[]
225
     */
226 1
    public function getTransactions($accountGuid, DateTime $from = null, DateTime $to = null)
227
    {
228 1
        $format = 'Y-m-d\TH:i:s\Z';
229 1
        return new PagedIterator($this, 'GetTransactions', [
230 1
            'accountGuid' => $accountGuid,
231 1
            'fromTimestampUtc' => $from ? $from->format($format) : null,
232 1
            'toTimestampUtc' => $to ? $to->format($format) : null,
233 1
            'pageSize' => 25,
234 1
        ], function (stdClass $object) {
235 1
            return Transaction::createFromObject($object);
236 1
        });
237
    }
238
239
    /**
240
     * Retrieves the Bitcoin address which should be used for new Bitcoin deposits.
241
     * @return BitcoinDepositAddress
242
     */
243 1
    public function getBitcoinDepositAddress()
244
    {
245 1
        $result = json_decode($this->getPrivateEndpoint('GetBitcoinDepositAddress'));
246 1
        return BitcoinDepositAddress::createFromObject($result);
247
    }
248
249
    /**
250
     * Creates a withdrawal request for a Fiat currency withdrawal from your Independent Reserve
251
     * account to an external bank account.
252
     *
253
     * Withdrawals to Australian bank accounts will be converted into AUD by Independent Reserve at
254
     * a competitive exchange rate. International withdrawals will be transmitted in USD, and
255
     * converted into the appropriate currency by the receiving bank. Minimum withdrawal amount is
256
     * USD 50.00, except where the available balance is less than this amount. In all cases, the
257
     * withdrawal amount must be greater than the withdrawal fee (only applies to international
258
     * withdrawals). Withdrawals are manually approved. Please allow 2-3 business days for the funds
259
     * to arrive in your bank account. Withdrawals to Australian accounts are usually faster.
260
     * Independent Reserve can only process withdrawals to bank accounts that match the name of the
261
     * Independent Reserve account holder.
262
     *
263
     * @param string $secondaryCurrencyCode The Independent Reserve fiat currency account to
264
     *        withdraw from (currently only USD accounts are supported).
265
     * @param double $withdrawalAmount Amount of fiat currency to withdraw.
266
     * @param string $withdrawalBankAccountName A pre-configured bank account you've already linked
267
     *        to your Independent Reserve account.
268
     * @return FiatWithdrawal
269
     */
270
    public function requestFiatWithdrawal($secondaryCurrencyCode, $withdrawalAmount,
271
        $withdrawalBankAccountName)
272
    {
273
        $result = json_decode($this->getPrivateEndpoint('GetBitcoinDepositAddress', [
274
            'secondaryCurrencyCode' => $secondaryCurrencyCode,
275
            'withdrawalAmount' => $withdrawalAmount,
276
            'withdrawalBankAccountName' => $withdrawalBankAccountName,
277
        ]));
278
        return FiatWithdrawal::createFromObject($result);
279
    }
280
}
281