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
ApiLayerService.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\MultiCurrency\ExchangeRateServices; |
| 4 | |
| 5 | use WPML\FP\Obj; |
| 6 | |
| 7 | abstract class ApiLayerService extends Service { |
| 8 | |
| 9 | abstract protected function getApiLayerUrl(); |
| 10 | |
| 11 | abstract protected function getApiLegacyUrl(); |
| 12 | |
| 13 | public function getApiUrl() { |
| 14 | return $this->getSelectedApiEndpoint(); |
| 15 | } |
| 16 | |
| 17 | public function resetConnectionCache() { |
| 18 | $this->setSelectedApiEndpoint( null ); |
| 19 | } |
| 20 | |
| 21 | private function setSelectedApiEndpoint( $endpoint ) { |
| 22 | $this->saveSetting( 'selected-endpoint', $endpoint ); |
| 23 | } |
| 24 | |
| 25 | private function getSelectedApiEndpoint() { |
| 26 | return $this->getSetting( 'selected-endpoint' ); |
| 27 | } |
| 28 | |
| 29 | protected function makeRequest( $from, $tos ) { |
| 30 | if ( $this->getSelectedApiEndpoint() ) { |
| 31 | $response = parent::makeRequest( $from, $tos ); |
| 32 | } else { |
| 33 | $this->setSelectedApiEndpoint( $this->getApiLayerUrl() ); |
| 34 | $response = parent::makeRequest( $from, $tos ); |
| 35 | |
| 36 | if ( $this->isWrongAuthenticationWithApiLayer( $response ) ) { |
| 37 | $this->setSelectedApiEndpoint( $this->getApiLegacyUrl() ); |
| 38 | $response = parent::makeRequest( $from, $tos ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return $response; |
| 43 | } |
| 44 | |
| 45 | private function isWrongAuthenticationWithApiLayer( $data ) { |
| 46 | return Obj::path( [ 'response', 'code' ], $data ) === 401; |
| 47 | } |
| 48 | |
| 49 | protected function getRequestHeaders() { |
| 50 | return [ 'apikey' => $this->getApiKey() ]; |
| 51 | } |
| 52 | |
| 53 | public function isKeyRequired() { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 |