vendor/florianv/exchanger/src/Service/SupportsHistoricalQueries.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Exchanger.
  5.  *
  6.  * (c) Florian Voutzinos <florian@voutzinos.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Exchanger\Service;
  12. use Exchanger\Contract\ExchangeRate as ExchangeRateContract;
  13. use Exchanger\Contract\ExchangeRateQuery as ExchangeRateQueryContract;
  14. use Exchanger\Contract\HistoricalExchangeRateQuery as HistoricalExchangeRateQueryContract;
  15. use Exchanger\Exception\UnsupportedCurrencyPairException;
  16. /**
  17.  * Trait to implement to add historical service support.
  18.  *
  19.  * @author Florian Voutzinos <florian@voutzinos.com>
  20.  */
  21. trait SupportsHistoricalQueries
  22. {
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public function getExchangeRate(ExchangeRateQueryContract $exchangeQuery): ExchangeRateContract
  27.     {
  28.         $currencyPair $exchangeQuery->getCurrencyPair();
  29.         if ($exchangeQuery instanceof HistoricalExchangeRateQueryContract) {
  30.             if ($rate $this->getHistoricalExchangeRate($exchangeQuery)) {
  31.                 return $rate;
  32.             }
  33.         } elseif ($rate $this->getLatestExchangeRate($exchangeQuery)) {
  34.             return $rate;
  35.         }
  36.         throw new UnsupportedCurrencyPairException($currencyPair$this);
  37.     }
  38.     /**
  39.      * Gets the latest rate.
  40.      *
  41.      * @param ExchangeRateQueryContract $exchangeQuery
  42.      *
  43.      * @return ExchangeRateContract
  44.      */
  45.     abstract protected function getLatestExchangeRate(ExchangeRateQueryContract $exchangeQuery): ExchangeRateContract;
  46.     /**
  47.      * Gets an historical rate.
  48.      *
  49.      * @param HistoricalExchangeRateQueryContract $exchangeQuery
  50.      *
  51.      * @return ExchangeRateContract
  52.      */
  53.     abstract protected function getHistoricalExchangeRate(HistoricalExchangeRateQueryContract $exchangeQuery): ExchangeRateContract;
  54. }