vendor/florianv/exchanger/src/Exchanger.php line 102

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;
  12. use Exchanger\Contract\ExchangeRateProvider as ExchangeRateProviderContract;
  13. use Exchanger\Contract\ExchangeRateQuery as ExchangeRateQueryContract;
  14. use Exchanger\Contract\ExchangeRateService as ExchangeRateServiceContract;
  15. use Exchanger\Exception\CacheException;
  16. use Exchanger\Exception\UnsupportedExchangeQueryException;
  17. use Exchanger\Contract\ExchangeRate as ExchangeRateContract;
  18. use Psr\SimpleCache\CacheInterface;
  19. /**
  20.  * Default implementation of the exchange rate provider with PSR-6 caching support.
  21.  *
  22.  * @author Florian Voutzinos <florian@voutzinos.com>
  23.  */
  24. final class Exchanger implements ExchangeRateProviderContract
  25. {
  26.     /**
  27.      * The service.
  28.      *
  29.      * @var ExchangeRateServiceContract
  30.      */
  31.     private $service;
  32.     /**
  33.      * The cache item pool.
  34.      *
  35.      * @var CacheInterface
  36.      */
  37.     private $cache;
  38.     /**
  39.      * The options.
  40.      *
  41.      * @var array
  42.      */
  43.     private $options;
  44.     /**
  45.      * Constructor.
  46.      *
  47.      * @param ExchangeRateServiceContract $service
  48.      * @param CacheInterface|null         $cache
  49.      * @param array                       $options
  50.      */
  51.     public function __construct(ExchangeRateServiceContract $serviceCacheInterface $cache null, array $options = [])
  52.     {
  53.         $this->service $service;
  54.         $this->cache $cache;
  55.         $this->options $options;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function getExchangeRate(ExchangeRateQueryContract $exchangeQuery): ExchangeRateContract
  61.     {
  62.         $currencyPair $exchangeQuery->getCurrencyPair();
  63.         if ($currencyPair->isIdentical()) {
  64.             return new ExchangeRate($currencyPair1, new \DateTime(), 'null');
  65.         }
  66.         if (!$this->service->supportQuery($exchangeQuery)) {
  67.             throw new UnsupportedExchangeQueryException($exchangeQuery$this->service);
  68.         }
  69.         if (null === $this->cache || false === $exchangeQuery->getOption('cache')) {
  70.             return $this->service->getExchangeRate($exchangeQuery);
  71.         }
  72.         $cacheKeyPrefix = isset($this->options['cache_key_prefix']) ? $this->options['cache_key_prefix'] : '';
  73.         $cacheKeyPrefix $exchangeQuery->getOption('cache_key_prefix'$cacheKeyPrefix);
  74.         // Replace characters reserved in PSR-6
  75.         $cacheKeyPrefix preg_replace('#[\{\}\(\)/\\\@\:]#''-'$cacheKeyPrefix);
  76.         $cacheKey $cacheKeyPrefix.sha1(serialize($exchangeQuery));
  77.         if (\strlen($cacheKey) > 64) {
  78.             throw new CacheException("Cache key length exceeds 64 characters ('$cacheKey'). This violates PSR-6 standard");
  79.         }
  80.         $item $this->cache->get($cacheKey);
  81.         if (null !== $item && $item instanceof ExchangeRateContract) {
  82.             return $item;
  83.         }
  84.         $rate $this->service->getExchangeRate($exchangeQuery);
  85.         $ttl $exchangeQuery->getOption('cache_ttl', isset($this->options['cache_ttl']) ? $this->options['cache_ttl'] : null);
  86.         $this->cache->set($cacheKey$rate$ttl);
  87.         return $rate;
  88.     }
  89. }