/** * {@inheritdoc} */ public function quote($currencyPair, $date = null) { if (is_string($currencyPair)) { $currencyPair = CurrencyPair::createFromString($currencyPair); } elseif (!$currencyPair instanceof CurrencyPair) { throw new \InvalidArgumentException('The currency pair must be either a string or an instance of CurrencyPair'); } //todo: abstract??? if (isset($date)) { // date cache check, first cache check if (null !== $this->dateCache && null !== ($rate = $this->dateCache->fetchRate($currencyPair, $date))) { return $rate; } $rate = $this->provider->fetchRate($currencyPair, $date); //store date and rate if (null !== $this->dateCache && isset($date)) { $this->dateCache->storeRate($currencyPair, $rate, $date); } return $rate; } // original cache check if (null !== $this->cache && null !== ($rate = $this->cache->fetchRate($currencyPair))) { return $rate; } $rate = $this->provider->fetchRate($currencyPair); // original cache set if (null !== $this->cache) { $this->cache->storeRate($currencyPair, $rate); } return $rate; }
/** * @test */ public function it_fetches_a_rate_from_scalars() { $arrayProvider = new ArrayProvider(['EUR/USD' => 1.5, 'USD/GBP' => '1.25', 'JPY/GBP' => 1]); $eurUsd = $arrayProvider->fetchRate(CurrencyPair::createFromString('EUR/USD')); $usdGbp = $arrayProvider->fetchRate(CurrencyPair::createFromString('USD/GBP')); $jpyGbp = $arrayProvider->fetchRate(CurrencyPair::createFromString('JPY/GBP')); $this->assertEquals('1.50', $eurUsd->getValue()); $this->assertEquals('1.25', $usdGbp->getValue()); $this->assertEquals('1', $jpyGbp->getValue()); }
/** * {@inheritdoc} */ public function quote($currencyPair) { if (is_string($currencyPair)) { $currencyPair = CurrencyPair::createFromString($currencyPair); } elseif (!$currencyPair instanceof CurrencyPair) { throw new \InvalidArgumentException('The currency pair must be either a string or an instance of CurrencyPair'); } if (null !== $this->cache && null !== ($rate = $this->cache->fetchRate($currencyPair))) { return $rate; } $rate = $this->provider->fetchRate($currencyPair); if (null !== $this->cache) { $this->cache->storeRate($currencyPair, $rate); } return $rate; }
/** * @test * @dataProvider invalidStringProvider * @expectedException \InvalidArgumentException */ public function it_throws_an_exception_when_creating_from_an_invalid_string($string) { CurrencyPair::createFromString($string); }