/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var CurrencyCaller $currencyCaller */ $currencyCaller = $this->getContainer()->get('app.caller.list_currencies'); $currencies = $currencyCaller->getCurrencies(); if (false === $currencies) { throw new \Exception('An error occured while trying to fetch the currencies.'); } $em = $this->getContainer()->get('doctrine.orm.default_entity_manager'); $currencyRepository = $this->getContainer()->get('app.repository.currency'); $output->writeln('<info>Start fetching the currencies. (' . count($currencies) . ' found)</info>'); $nbCurrenciesSkipped = 0; $nbCurrenciesInserted = 0; foreach ($currencies as $code => $name) { $existingCurrencyEntity = $currencyRepository->findOneBy(['code' => $code]); if (!$existingCurrencyEntity instanceof Currency) { $newCurrencyEntity = new Currency(); $newCurrencyEntity->setCode($code); $newCurrencyEntity->setName($name); $em->persist($newCurrencyEntity); $nbCurrenciesInserted++; } else { $nbCurrenciesSkipped++; } } $em->flush(); $output->writeln('<info>Finished.</info>'); $output->writeln('<info> - ' . $nbCurrenciesInserted . ' Inserted</info>'); $output->writeln('<info> - ' . $nbCurrenciesSkipped . ' Skipped</info>'); }
public function testCanAddRemoveCurrencies() { $this->assertEquals(1, count($this->source->getCurrencies())); $currencyUsd = new Currency(); $currencyUsd->setCurrency('USD')->setRate(0.9); $this->source->addCurrency($currencyUsd); $this->assertEquals(2, count($this->source->getCurrencies())); $this->source->removeCurrency($currencyUsd); $this->assertEquals(1, count($this->source->getCurrencies())); }
/** * Retrieve the rate for the given currencies. * Return false if something went wrong. * * @param Currency $from * @param Currency $to * * @return float|bool */ public function getRateFor(Currency $from, Currency $to) { $uri = '/get/?access_key=' . $this->apiKey . '&from=' . $from->getCode() . '&to=' . $to->getCode(); $response = $this->jsonRatesClient->get($uri); $reponseStatus = (int) $response->getStatusCode(); $responseBody = $response->getBody()->getContents(); if (Response::HTTP_OK === $reponseStatus) { $rate = (double) json_decode($responseBody, true); return $rate; } return false; }
/** * Save the current basket in session. * * @param Basket|null $basket */ public function saveBasketInSession(Basket $basket = null) { if (!$basket instanceof Basket) { $basket = $this->basket; } if ($basket instanceof Basket) { $serializedBasket = $this->serializeBasket($basket); $this->session->set(static::SESSION_KEY_BASKET, $serializedBasket); } if ($this->currencyWanted instanceof Currency) { $this->session->set(static::SESSION_KEY_CURRENCY_WANTED, $this->currencyWanted->getId()); } }
protected function createCurrency($rate, $currencyCode, Source $source) { $currency = new Currency(); $currency->setSource($source)->setRate($rate)->setCurrency($currencyCode); return $currency; }