ApiLayerService.php
4 weeks ago
CurrencyLayer.php
4 weeks ago
ExchangeRatesApi.php
4 weeks ago
Fixerio.php
4 weeks ago
OpenExchangeRates.php
4 weeks ago
Service.php
4 weeks ago
Service.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\MultiCurrency\ExchangeRateServices; |
| 4 | |
| 5 | use WPML\FP\Obj; |
| 6 | |
| 7 | abstract class Service { |
| 8 | |
| 9 | private $settings; |
| 10 | |
| 11 | abstract public function getId(); |
| 12 | |
| 13 | abstract public function getName(); |
| 14 | |
| 15 | abstract public function getUrl(); |
| 16 | |
| 17 | abstract public function getApiUrl(); |
| 18 | |
| 19 | abstract public function isKeyRequired(); |
| 20 | |
| 21 | public function resetConnectionCache() { |
| 22 | |
| 23 | } |
| 24 | |
| 25 | public function getRates( $from, $tos ) { |
| 26 | $this->clearLastError(); |
| 27 | |
| 28 | $response = $this->makeRequest( $from, $tos ); |
| 29 | |
| 30 | if ( is_wp_error( $response ) ) { |
| 31 | $http_error = implode( "\n", $response->get_error_messages() ); |
| 32 | $this->saveLastError( $http_error ); |
| 33 | throw new \Exception( $http_error ); |
| 34 | } |
| 35 | |
| 36 | $data = json_decode( $response['body'] ); |
| 37 | |
| 38 | if ( $this->isInvalidResponse( $data ) ) { |
| 39 | $error = self::get_formatted_error( $data ); |
| 40 | $this->saveLastError( $error ); |
| 41 | throw new \Exception( $error ); |
| 42 | } |
| 43 | |
| 44 | return $this->extractRates( $data, $from, $tos ); |
| 45 | } |
| 46 | |
| 47 | protected function makeRequest( $from, $tos ) { |
| 48 | if ( $this->isKeyRequired() ) { |
| 49 | $url = sprintf( $this->getApiUrl(), $this->getApiKey(), $from, implode( ',', $tos ) ); |
| 50 | } else { |
| 51 | $url = sprintf( $this->getApiUrl(), $from, implode( ',', $tos ) ); |
| 52 | } |
| 53 | |
| 54 | return wp_safe_remote_get( $url, [ 'headers' => $this->getRequestHeaders() ] ); |
| 55 | } |
| 56 | |
| 57 | protected function getRequestHeaders() { |
| 58 | return []; |
| 59 | } |
| 60 | |
| 61 | protected function isInvalidResponse( $decodedData ) { |
| 62 | return empty( $decodedData->rates ); |
| 63 | } |
| 64 | |
| 65 | protected function extractRates( $validData, $from, $tos ) { |
| 66 | $rates = []; |
| 67 | |
| 68 | foreach ( $validData->rates as $to => $rate ) { |
| 69 | $rates[ $to ] = round( $rate, \WCML_Exchange_Rates::DIGITS_AFTER_DECIMAL_POINT ); |
| 70 | } |
| 71 | |
| 72 | return $rates; |
| 73 | } |
| 74 | |
| 75 | public static function get_formatted_error( $response ) { |
| 76 | $getFromPath = function( $path ) use ( $response ) { |
| 77 | try { |
| 78 | $value = Obj::path( $path, $response ); |
| 79 | return is_string( $value ) || is_int( $value ) ? $value : null; |
| 80 | } catch ( \Exception $e ) { |
| 81 | return null; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | $formattedError = wpml_collect( [ |
| 86 | 'error' => $getFromPath( [ 'error' ] ), |
| 87 | 'error_code' => $getFromPath( [ 'error', 'code' ] ), |
| 88 | 'error_type' => $getFromPath( [ 'error', 'type' ] ), |
| 89 | 'error_info' => $getFromPath( [ 'error', 'info' ] ), |
| 90 | 'error_message' => $getFromPath( [ 'error', 'message' ] ), |
| 91 | 'message' => $getFromPath( [ 'message' ] ), |
| 92 | 'description' => $getFromPath( [ 'description' ] ), |
| 93 | ] )->filter() |
| 94 | ->map( function( $value, $key ) { |
| 95 | return "$key: $value"; |
| 96 | } ) |
| 97 | ->implode( ' - ' ); |
| 98 | |
| 99 | return $formattedError |
| 100 | ? strip_tags( $formattedError ) |
| 101 | : esc_html__( 'Cannot get exchange rates. Connection failed.', 'woocommerce-multilingual' ); |
| 102 | } |
| 103 | |
| 104 | public function getSettings() { |
| 105 | if ( null === $this->settings ) { |
| 106 | $this->settings = get_option( 'wcml_exchange_rate_service_' . $this->getId(), [] ); |
| 107 | } |
| 108 | |
| 109 | return $this->settings; |
| 110 | } |
| 111 | |
| 112 | private function saveSettings() { |
| 113 | update_option( 'wcml_exchange_rate_service_' . $this->getId(), $this->getSettings() ); |
| 114 | } |
| 115 | |
| 116 | public function getSetting( $key ) { |
| 117 | return Obj::prop( $key, $this->getSettings() ); |
| 118 | } |
| 119 | |
| 120 | public function saveSetting( $key, $value ) { |
| 121 | $this->getSettings(); |
| 122 | $this->settings[ $key ] = $value; |
| 123 | $this->saveSettings(); |
| 124 | } |
| 125 | |
| 126 | public function saveLastError( $error_message ) { |
| 127 | $this->saveSetting( |
| 128 | 'last_error', |
| 129 | [ |
| 130 | 'text' => $error_message, |
| 131 | 'time' => date_i18n( 'F j, Y g:i a', false, true ), |
| 132 | ] |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | public function clearLastError() { |
| 137 | $this->saveSetting( 'last_error', false ); |
| 138 | } |
| 139 | |
| 140 | public function getLastError() { |
| 141 | return $this->getSetting( 'last_error' ); |
| 142 | } |
| 143 | |
| 144 | protected function getApiKey() { |
| 145 | return $this->getSetting( 'api-key' ); |
| 146 | } |
| 147 | } |
| 148 |