Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
10 / 12 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Bitfinex_API | |
83.33% |
10 / 12 |
|
50.00% |
1 / 2 |
4.07 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_exchange_rate | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
3.05 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @see https://docs.bitfinex.com/docs/rest-public |
| 4 | * @see https://docs.bitfinex.com/v2/reference#rest-public-ticker |
| 5 | * |
| 6 | * @see https://api-pub.bitfinex.com/v2/conf/pub:list:pair:exchange |
| 7 | * |
| 8 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 9 | */ |
| 10 | |
| 11 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Exchange_Rate; |
| 12 | |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Exchange_Rate_API_Interface; |
| 14 | use Psr\Log\LoggerAwareTrait; |
| 15 | use Psr\Log\LoggerInterface; |
| 16 | |
| 17 | class Bitfinex_API implements Exchange_Rate_API_Interface { |
| 18 | use LoggerAwareTrait; |
| 19 | |
| 20 | public function __construct( LoggerInterface $logger ) { |
| 21 | $this->setLogger( $logger ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Fetch the current exchange from a remote API. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public function get_exchange_rate( string $currency ): string { |
| 30 | |
| 31 | $trading_pair = 'tBTC' . strtoupper( $currency ); |
| 32 | |
| 33 | $url = "https://api-pub.bitfinex.com/v2/tickers?symbols={$trading_pair}"; |
| 34 | |
| 35 | $request_response = wp_remote_get( $url ); |
| 36 | |
| 37 | if ( is_wp_error( $request_response ) ) { |
| 38 | throw new \Exception(); |
| 39 | } |
| 40 | |
| 41 | if ( 200 !== $request_response['response']['code'] ) { |
| 42 | throw new \Exception(); |
| 43 | } |
| 44 | |
| 45 | $reponse_body = json_decode( $request_response['body'], true ); |
| 46 | |
| 47 | // Multiple rates can be queried at the same time. |
| 48 | |
| 49 | /** |
| 50 | * SYMBOL string The symbol of the requested ticker data, |
| 51 | * BID float Price of last highest bid, |
| 52 | * BID_SIZE float Sum of the 25 highest bid sizes, |
| 53 | * ASK float Price of last lowest ask, |
| 54 | * ASK_SIZE float Sum of the 25 lowest ask sizes, |
| 55 | * DAILY_CHANGE float Amount that the last price has changed since yesterday, |
| 56 | * DAILY_CHANGE_RELATIVE float Relative price change since yesterday (*100 for percentage change), |
| 57 | * LAST_PRICE float Price of the last trade, |
| 58 | * VOLUME float Daily volume, |
| 59 | * HIGH float Daily high, |
| 60 | * LOW float Daily low |
| 61 | */ |
| 62 | $trading_pair_response = $reponse_body[0]; |
| 63 | |
| 64 | $exchange_rate = $trading_pair_response[7]; |
| 65 | |
| 66 | return $exchange_rate; |
| 67 | } |
| 68 | } |