Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.92% |
10 / 13 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Bitstamp_API | |
76.92% |
10 / 13 |
|
50.00% |
1 / 2 |
5.31 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get_exchange_rate | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
4.25 |
1 | <?php |
2 | /** |
3 | * @see https://www.bitstamp.net/api/ |
4 | * |
5 | * Rate limit is "8000 requests per 10 minutes". |
6 | * |
7 | * @package brianhenryie/bh-wp-bitcoin-gateway |
8 | */ |
9 | |
10 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Exchange_Rate; |
11 | |
12 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Exchange_Rate_API_Interface; |
13 | use Psr\Log\LoggerAwareTrait; |
14 | use Psr\Log\LoggerInterface; |
15 | |
16 | class Bitstamp_API implements Exchange_Rate_API_Interface { |
17 | use LoggerAwareTrait; |
18 | |
19 | public function __construct( LoggerInterface $logger ) { |
20 | $this->setLogger( $logger ); |
21 | } |
22 | |
23 | /** |
24 | * Fetch the current exchange from a remote API. |
25 | * |
26 | * @return string |
27 | */ |
28 | public function get_exchange_rate( string $currency ): string { |
29 | |
30 | $between = strtolower( "btc{$currency}" ); |
31 | |
32 | $valid_exchanges = array( 'btcusd', 'btceur', 'btcgbp' ); |
33 | |
34 | if ( ! in_array( $between, $valid_exchanges, true ) ) { |
35 | throw new \Exception( 'Bitstamp only supports USD, EUR and GBP.' ); |
36 | } |
37 | |
38 | $url = "https://www.bitstamp.net/api/v2/ticker/{$between}/"; |
39 | |
40 | $request_response = wp_remote_get( $url ); |
41 | |
42 | if ( is_wp_error( $request_response ) ) { |
43 | throw new \Exception(); |
44 | } |
45 | |
46 | if ( 200 !== $request_response['response']['code'] ) { |
47 | throw new \Exception(); |
48 | } |
49 | |
50 | /** |
51 | * last Last BTC price. |
52 | * high Last 24 hours price high. |
53 | * low Last 24 hours price low. |
54 | * vwap Last 24 hours volume weighted average price. |
55 | * volume Last 24 hours volume. |
56 | * bid Highest buy order. |
57 | * ask Lowest sell order. |
58 | * timestamp Unix timestamp date and time. |
59 | * open First price of the day. |
60 | */ |
61 | $response = json_decode( $request_response['body'], true ); |
62 | |
63 | return $response['last']; |
64 | } |
65 | } |