Ejemplo n.º 1
0
 /**
  * Get loaded object. If object is not loaded yet, then load it and save it
  * locally. Otherwise, just return the pre-loaded object.
  *
  * Returns the default persisted Currency object
  *
  * The currency object is fetched from the repository using
  * default ISO code as the search criteria. Default ISO code
  * is passed to the CurrencyWrapper service definition as a
  * mandatory constructor parameter.
  *
  * @return CurrencyInterface Default currency
  *
  * @throws CurrencyNotAvailableException Currency not available
  */
 public function get()
 {
     $currency = $this->currencyRepository->findOneBy(['iso' => $this->defaultCurrencyIsoCode]);
     if (!$currency instanceof CurrencyInterface) {
         throw new CurrencyNotAvailableException(sprintf('Default currency object for ISO code "%s" not found', $this->defaultCurrencyIsoCode));
     }
     return $currency;
 }
 /**
  * Populates the exchange rates.
  *
  * @return $this Self object
  */
 public function populate()
 {
     $currenciesCodes = [];
     $currencies = $this->currencyRepository->findAll();
     /**
      * Create an array of all active currency codes.
      *
      * @var CurrencyInterface $currency
      */
     foreach ($currencies as $currency) {
         if ($currency->getIso() != $this->defaultCurrency) {
             $currenciesCodes[] = $currency->getIso();
         }
     }
     /**
      * Get rates for all of the enabled and active currencies.
      */
     $rates = $this->currencyExchangeRatesAdapter->getExchangeRates($this->defaultCurrency, $currenciesCodes);
     /**
      * @var CurrencyInterface $sourceCurrency
      */
     $sourceCurrency = $this->currencyRepository->findOneBy(['iso' => $this->defaultCurrency]);
     /**
      * [
      *      "EUR" => "1,378278",
      *      "YEN" => "0,784937",
      * ].
      */
     foreach ($rates as $code => $rate) {
         /**
          * @var CurrencyInterface $targetCurrency
          */
         $targetCurrency = $this->currencyRepository->findOneBy(['iso' => $code]);
         if (!$targetCurrency instanceof CurrencyInterface) {
             continue;
         }
         /**
          * check if this is a new exchange rate, or if we have to
          * create a new one.
          */
         $exchangeRate = $this->currencyExchangeRateObjectDirector->findOneBy(['sourceCurrency' => $sourceCurrency, 'targetCurrency' => $targetCurrency]);
         if (!$exchangeRate instanceof CurrencyExchangeRateInterface) {
             $exchangeRate = $this->currencyExchangeRateObjectDirector->create();
         }
         $exchangeRate->setExchangeRate($rate)->setSourceCurrency($sourceCurrency)->setTargetCurrency($targetCurrency);
         $this->currencyExchangeRateObjectDirector->save($exchangeRate);
     }
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * Return functions.
  *
  * @return ExpressionFunction[] An array of Function instances
  */
 public function getFunctions()
 {
     return [new ExpressionFunction('money', function () {
         throw new RuntimeException('Function "money" can\'t be compiled.');
     }, function (array $context, $amount, $currencyIso = null) {
         if ($currencyIso === null) {
             $currency = $this->defaultCurrencyWrapper->get();
         } else {
             /**
              * @var CurrencyInterface $currency
              */
             $currency = $this->currencyRepository->findOneBy(['iso' => $currencyIso]);
         }
         return Money::create($amount * 100, $currency);
     })];
 }
Ejemplo n.º 4
0
 /**
  * Given a the currency base, returns a list of all exchange rates.
  *
  * @return array Exchange rate list
  */
 public function getExchangeRateList()
 {
     if (!empty($this->exchangeRateList)) {
         return $this->exchangeRateList;
     }
     $this->exchangeRateList = [];
     $currencyBase = $this->currencyRepository->findOneBy(['iso' => $this->exchangeCurrencyIso]);
     $availableExchangeRates = $this->currencyExchangeRateRepository->findBy(['sourceCurrency' => $currencyBase]);
     /**
      * @var CurrencyExchangeRate $exchangeRate
      */
     foreach ($availableExchangeRates as $exchangeRate) {
         $targetCurrency = $exchangeRate->getTargetCurrency();
         $targetCurrencyIso = $targetCurrency->getIso();
         $this->exchangeRateList[$targetCurrencyIso] = ['rate' => $exchangeRate->getExchangeRate(), 'currency' => $targetCurrency];
     }
     return $this->exchangeRateList;
 }
Ejemplo n.º 5
0
 /**
  * Load currency from session
  *
  * @return CurrencyInterface|null Currency
  *
  * @throws CurrencyNotAvailableException No currency available
  */
 private function loadCurrencyFromSession()
 {
     $currencyIdInSession = $this->currencySessionManager->get();
     return $currencyIdInSession ? $this->currency = $this->currencyRepository->find($currencyIdInSession) : null;
 }