AggregateCurrencies.php
2 years ago
BitcoinCurrencies.php
2 years ago
CachedCurrencies.php
2 years ago
CurrencyList.php
2 years ago
ISOCurrencies.php
2 years ago
CachedCurrencies.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Money\Currencies; |
| 4 | |
| 5 | use Cache\Taggable\TaggableItemInterface; |
| 6 | use Money\Currencies; |
| 7 | use Money\Currency; |
| 8 | use Psr\Cache\CacheItemPoolInterface; |
| 9 | |
| 10 | /** |
| 11 | * Cache the result of currency checking. |
| 12 | * |
| 13 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 14 | */ |
| 15 | final class CachedCurrencies implements Currencies |
| 16 | { |
| 17 | /** |
| 18 | * @var Currencies |
| 19 | */ |
| 20 | private $currencies; |
| 21 | |
| 22 | /** |
| 23 | * @var CacheItemPoolInterface |
| 24 | */ |
| 25 | private $pool; |
| 26 | |
| 27 | public function __construct(Currencies $currencies, CacheItemPoolInterface $pool) |
| 28 | { |
| 29 | $this->currencies = $currencies; |
| 30 | $this->pool = $pool; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * {@inheritdoc} |
| 35 | */ |
| 36 | public function contains(Currency $currency) |
| 37 | { |
| 38 | $item = $this->pool->getItem('currency|availability|'.$currency->getCode()); |
| 39 | |
| 40 | if (false === $item->isHit()) { |
| 41 | $item->set($this->currencies->contains($currency)); |
| 42 | |
| 43 | if ($item instanceof TaggableItemInterface) { |
| 44 | $item->addTag('currency.availability'); |
| 45 | } |
| 46 | |
| 47 | $this->pool->save($item); |
| 48 | } |
| 49 | |
| 50 | return $item->get(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritdoc} |
| 55 | */ |
| 56 | public function subunitFor(Currency $currency) |
| 57 | { |
| 58 | $item = $this->pool->getItem('currency|subunit|'.$currency->getCode()); |
| 59 | |
| 60 | if (false === $item->isHit()) { |
| 61 | $item->set($this->currencies->subunitFor($currency)); |
| 62 | |
| 63 | if ($item instanceof TaggableItemInterface) { |
| 64 | $item->addTag('currency.subunit'); |
| 65 | } |
| 66 | |
| 67 | $this->pool->save($item); |
| 68 | } |
| 69 | |
| 70 | return $item->get(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritdoc} |
| 75 | */ |
| 76 | #[\ReturnTypeWillChange] |
| 77 | public function getIterator() |
| 78 | { |
| 79 | return new \CallbackFilterIterator( |
| 80 | $this->currencies->getIterator(), |
| 81 | function (Currency $currency) { |
| 82 | $item = $this->pool->getItem('currency|availability|'.$currency->getCode()); |
| 83 | $item->set(true); |
| 84 | |
| 85 | if ($item instanceof TaggableItemInterface) { |
| 86 | $item->addTag('currency.availability'); |
| 87 | } |
| 88 | |
| 89 | $this->pool->save($item); |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 |