Open Monograph Press  3.3.0
CachedCurrencies.php
1 <?php
2 
3 namespace Money\Currencies;
4 
5 use Cache\Taggable\TaggableItemInterface;
8 use Psr\Cache\CacheItemPoolInterface;
9 
15 final class CachedCurrencies implements Currencies
16 {
20  private $currencies;
21 
25  private $pool;
26 
31  public function __construct(Currencies $currencies, CacheItemPoolInterface $pool)
32  {
33  $this->currencies = $currencies;
34  $this->pool = $pool;
35  }
36 
40  public function contains(Currency $currency)
41  {
42  $item = $this->pool->getItem('currency|availability|'.$currency->getCode());
43 
44  if (false === $item->isHit()) {
45  $item->set($this->currencies->contains($currency));
46 
47  if ($item instanceof TaggableItemInterface) {
48  $item->addTag('currency.availability');
49  }
50 
51  $this->pool->save($item);
52  }
53 
54  return $item->get();
55  }
56 
60  public function subunitFor(Currency $currency)
61  {
62  $item = $this->pool->getItem('currency|subunit|'.$currency->getCode());
63 
64  if (false === $item->isHit()) {
65  $item->set($this->currencies->subunitFor($currency));
66 
67  if ($item instanceof TaggableItemInterface) {
68  $item->addTag('currency.subunit');
69  }
70 
71  $this->pool->save($item);
72  }
73 
74  return $item->get();
75  }
76 
80  public function getIterator()
81  {
82  return new \CallbackFilterIterator(
83  $this->currencies->getIterator(),
84  function (Currency $currency) {
85  $item = $this->pool->getItem('currency|availability|'.$currency->getCode());
86  $item->set(true);
87 
88  if ($item instanceof TaggableItemInterface) {
89  $item->addTag('currency.availability');
90  }
91 
92  $this->pool->save($item);
93 
94  return true;
95  }
96  );
97  }
98 }
Money\Currencies
Definition: AggregateCurrencies.php:3
Money\Currencies\CachedCurrencies
Definition: CachedCurrencies.php:15
Money\Currencies\CachedCurrencies\__construct
__construct(Currencies $currencies, CacheItemPoolInterface $pool)
Definition: CachedCurrencies.php:37
Money\Currencies\CachedCurrencies\subunitFor
subunitFor(Currency $currency)
Definition: CachedCurrencies.php:66
Money\Currencies\CachedCurrencies\getIterator
getIterator()
Definition: CachedCurrencies.php:86
Money\Currency
Definition: vendor/moneyphp/money/src/Currency.php:14
Money\Currency\getCode
getCode()
Definition: vendor/moneyphp/money/src/Currency.php:47
Money\Currencies\CachedCurrencies\contains
contains(Currency $currency)
Definition: CachedCurrencies.php:46
Money\Currencies
Definition: Currencies.php:12
Currency
Basic class describing a currency.
Definition: Currency.inc.php:24