Skip to content

Commit

Permalink
Merge pull request ongr-archive#22 from tfe2012/add_price_extension
Browse files Browse the repository at this point in the history
Add price extension
  • Loading branch information
tautrimas committed Dec 29, 2014
2 parents 694516c + 0bd4418 commit 2bea0a9
Show file tree
Hide file tree
Showing 9 changed files with 1,297 additions and 3 deletions.
32 changes: 32 additions & 0 deletions Core/CurrencyExchangeGetterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ContentBundle\Core;

/**
* This interface defines structure for currency rates download driver.
*/
interface CurrencyExchangeGetterInterface
{
/**
* Returns array of currency rates. For example: <code>['USD' => 1, 'EUR' => '1.678']</code>.
*
* @return array
*/
public function getRates();

/**
* Returns default currency name.
*
* @return string
*/
public function getDefaultCurrencyName();
}
19 changes: 19 additions & 0 deletions Exception/Currency/RatesNotLoadedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ContentBundle\Exception\Currency;

/**
* This exception is thrown when we try to retrieve currency rates while it's not loaded.
*/
class RatesNotLoadedException extends \RuntimeException
{
}
19 changes: 19 additions & 0 deletions Exception/Currency/UndefinedCurrencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ContentBundle\Exception\Currency;

/**
* This exceptions is thrown when we request for currency that does not exists.
*/
class UndefinedCurrencyException extends \UnexpectedValueException
{
}
91 changes: 91 additions & 0 deletions Service/Currency/CurrencyExchangeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ContentBundle\Service\Currency;

use ONGR\ContentBundle\Exception\Currency\UndefinedCurrencyException;

/**
* This class handles currency rates download and exchange.
*/
class CurrencyExchangeService
{
/**
* @var CurrencyRatesService
*/
protected $rates = null;

/**
* @var string
*/
protected $defaultCurrency;

/**
* @param CurrencyRatesService $rates
* @param string $defaultCurrency
*/
public function __construct(CurrencyRatesService $rates, $defaultCurrency)
{
$this->rates = $rates;
$this->defaultCurrency = $defaultCurrency;
}

/**
* @param string $currency
*
* @return float
* @throws UndefinedCurrencyException
*/
public function getCurrencyRate($currency)
{
$rates = $this->rates->getRates();

if (isset($rates[$currency])) {
return $rates[$currency];
}

throw new UndefinedCurrencyException('Currency ' . $currency . ' not found.');
}

/**
* @return array|null
*/
public function getCurrencies()
{
return $this->rates->getRates();
}

/**
* Rate calculation.
*
* @param float|int $amount
* @param string $toCurrency
* @param null $fromCurrency
*
* @return float
*/
public function calculateRate($amount, $toCurrency, $fromCurrency = null)
{
if (!isset($fromCurrency)) {
$fromCurrency = $this->defaultCurrency;
}

if ($this->rates->getBaseCurrency() != $fromCurrency) {
$amount = $amount / $this->getCurrencyRate($fromCurrency);
}

if ($toCurrency == $this->rates->getBaseCurrency()) {
return $amount;
}

return $amount * $this->getCurrencyRate($toCurrency);
}
}
127 changes: 127 additions & 0 deletions Service/Currency/CurrencyRatesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ContentBundle\Service\Currency;

use ONGR\ContentBundle\Core\CurrencyExchangeGetterInterface;
use ONGR\ContentBundle\Exception\Currency\RatesNotLoadedException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Stash\Interfaces\ItemInterface;
use Stash\Interfaces\PoolInterface;

/**
* This class provides currency rates.
*/
class CurrencyRatesService implements LoggerAwareInterface
{
/**
* @var CurrencyExchangeGetterInterface
*/
protected $driver;

/**
* @var PoolInterface
*/
protected $pool;

/**
* @var bool
*/
protected $poorManLoad = true;

/**
* @var null|array
*/
protected $rates = null;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* @param CurrencyExchangeGetterInterface $driver Driver.
* @param PoolInterface $pool Cache pool.
* @param bool $poorManLoad Set to true if we want to load currencies on request.
*/
public function __construct(CurrencyExchangeGetterInterface $driver, PoolInterface $pool, $poorManLoad = true)
{
$this->driver = $driver;
$this->pool = $pool;
$this->poorManLoad = $poorManLoad;
}

/**
* @return ItemInterface
*/
protected function getCachePoolItem()
{
return $this->pool->getItem('fox_currency');
}

/**
* This method returns exchange rates.
*
* @throws RatesNotLoadedException
* @return array
*/
public function getRates()
{
if (isset($this->rates)) {
return $this->rates;
}

/** @var ItemInterface $item */
$item = $this->getCachePoolItem();

$this->rates = $item->get();
if (isset($this->rates)) {
return $this->rates;
}

if ($this->poorManLoad) {
$this->logger && $this->logger->notice('Auto reloaded currency rates on CurrencyRatesService');
$this->reloadRates();
} else {
throw new RatesNotLoadedException('Currency rates are not loaded and could not be loaded on demand');
}

return $this->rates;
}

/**
* Returns actual base currency name.
*
* @return string
*/
public function getBaseCurrency()
{
return $this->driver->getDefaultCurrencyName();
}

/**
* Reloads rates using given driver.
*/
public function reloadRates()
{
$this->rates = $this->driver->getRates();
$this->getCachePoolItem()->set($this->rates);
}

/**
* {@inheritdoc}
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
4 changes: 2 additions & 2 deletions Tests/Functional/Service/ContentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected function getDataArray()
'_id' => 3,
'slug' => 'awsome',
],
]
]
],
],
];
}

Expand Down
Loading

0 comments on commit 2bea0a9

Please sign in to comment.